]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/reverse.php
enable PHP 7.2 for Ubuntu 18 CI
[nominatim.git] / lib-php / website / reverse.php
1 <?php
2
3 require_once(CONST_LibDir.'/init-website.php');
4 require_once(CONST_LibDir.'/log.php');
5 require_once(CONST_LibDir.'/PlaceLookup.php');
6 require_once(CONST_LibDir.'/ReverseGeocode.php');
7 require_once(CONST_LibDir.'/output.php');
8 ini_set('memory_limit', '200M');
9
10 $oParams = new Nominatim\ParameterParser();
11
12 // Format for output
13 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
14 set_exception_handler_by_format($sOutputFormat);
15
16 // Preferred language
17 $aLangPrefOrder = $oParams->getPreferredLanguages();
18
19 $oDB = new Nominatim\DB(CONST_Database_DSN);
20 $oDB->connect();
21
22 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
23
24 $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
25 $oPlaceLookup->loadParamArray($oParams);
26 $oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true));
27
28 $sOsmType = $oParams->getSet('osm_type', array('N', 'W', 'R'));
29 $iOsmId = $oParams->getInt('osm_id', -1);
30 $fLat = $oParams->getFloat('lat');
31 $fLon = $oParams->getFloat('lon');
32 $iZoom = $oParams->getInt('zoom', 18);
33
34 if ($sOsmType && $iOsmId > 0) {
35     $aPlace = $oPlaceLookup->lookupOSMID($sOsmType, $iOsmId);
36 } elseif ($fLat !== false && $fLon !== false) {
37     $oReverseGeocode = new Nominatim\ReverseGeocode($oDB);
38     $oReverseGeocode->setZoom($iZoom);
39
40     $oLookup = $oReverseGeocode->lookup($fLat, $fLon);
41
42     if ($oLookup) {
43         $aPlaces = $oPlaceLookup->lookup(array($oLookup->iId => $oLookup));
44         if (!empty($aPlaces)) {
45             $aPlace = reset($aPlaces);
46         }
47     }
48 } else {
49     userError('Need coordinates or OSM object to lookup.');
50 }
51
52 if (isset($aPlace)) {
53     $aOutlineResult = $oPlaceLookup->getOutlines(
54         $aPlace['place_id'],
55         $aPlace['lon'],
56         $aPlace['lat'],
57         Nominatim\ClassTypes\getDefRadius($aPlace),
58         $fLat,
59         $fLon
60     );
61
62     if ($aOutlineResult) {
63         $aPlace = array_merge($aPlace, $aOutlineResult);
64     }
65 } else {
66     $aPlace = array();
67 }
68
69 logEnd($oDB, $hLog, count($aPlace) ? 1 : 0);
70
71 if (CONST_Debug) {
72     var_dump($aPlace);
73     exit;
74 }
75
76 if ($sOutputFormat == 'geocodejson') {
77     $sQuery = $fLat.','.$fLon;
78     if (isset($aPlace['place_id'])) {
79         $fDistance = $oDB->getOne(
80             'SELECT ST_Distance(ST_SetSRID(ST_Point(:lon,:lat),4326), centroid) FROM placex where place_id = :placeid',
81             array(':lon' => $fLon, ':lat' => $fLat, ':placeid' => $aPlace['place_id'])
82         );
83     }
84 }
85
86 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
87 include(CONST_LibDir.'/template/address-'.$sOutputTemplate.'.php');