]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
README: tiny markdown syntax error
[nominatim.git] / website / reverse.php
1 <?php
2
3 require_once(CONST_BasePath.'/lib/init-website.php');
4 require_once(CONST_BasePath.'/lib/log.php');
5 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
6 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
7 require_once(CONST_BasePath.'/lib/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('html', 'xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
14 set_exception_handler_by_format($sOutputFormat);
15
16 // Preferred language
17 $aLangPrefOrder = $oParams->getPreferredLanguages();
18
19 $oDB =& getDB();
20
21 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
22
23 $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
24 $oPlaceLookup->loadParamArray($oParams);
25 $oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true));
26
27 $sOsmType = $oParams->getSet('osm_type', array('N', 'W', 'R'));
28 $iOsmId = $oParams->getInt('osm_id', -1);
29 $fLat = $oParams->getFloat('lat');
30 $fLon = $oParams->getFloat('lon');
31 $iZoom = $oParams->getInt('zoom', 18);
32
33 if ($sOsmType && $iOsmId > 0) {
34     $aPlace = $oPlaceLookup->lookupOSMID($sOsmType, $iOsmId);
35 } elseif ($fLat !== false && $fLon !== false) {
36     $oReverseGeocode = new Nominatim\ReverseGeocode($oDB);
37     $oReverseGeocode->setZoom($iZoom);
38
39     $oLookup = $oReverseGeocode->lookup($fLat, $fLon);
40     if (CONST_Debug) var_dump($oLookup);
41
42     if ($oLookup) {
43         $aPlaces = $oPlaceLookup->lookup(array($oLookup->iId => $oLookup));
44         if (!empty($aPlaces)) {
45             $aPlace = reset($aPlaces);
46         }
47     }
48 } elseif ($sOutputFormat != 'html') {
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\getProperty($aPlace, 'defdiameter', 0.0001),
58         $fLat,
59         $fLon
60     );
61
62     if ($aOutlineResult) {
63         $aPlace = array_merge($aPlace, $aOutlineResult);
64     }
65 } else {
66     $aPlace = array();
67 }
68
69
70 if (CONST_Debug) {
71     var_dump($aPlace);
72     exit;
73 }
74
75 if ($sOutputFormat == 'html') {
76     $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
77     $sTileURL = CONST_Map_Tile_URL;
78     $sTileAttribution = CONST_Map_Tile_Attribution;
79 } elseif ($sOutputFormat == 'geocodejson') {
80     $sQuery = $fLat.','.$fLon;
81     if (isset($aPlace['place_id'])) {
82         $fDistance = chksql($oDB->getOne('SELECT ST_Distance(ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326), centroid) FROM placex where place_id='.$aPlace['place_id']));
83     }
84 }
85
86 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
87 include(CONST_BasePath.'/lib/template/address-'.$sOutputTemplate.'.php');