]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
Nominatim::DB support input variables, custom error messages
[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 = new Nominatim\DB();
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     if (CONST_Debug) var_dump($oLookup);
42
43     if ($oLookup) {
44         $aPlaces = $oPlaceLookup->lookup(array($oLookup->iId => $oLookup));
45         if (!empty($aPlaces)) {
46             $aPlace = reset($aPlaces);
47         }
48     }
49 } elseif ($sOutputFormat != 'html') {
50     userError('Need coordinates or OSM object to lookup.');
51 }
52
53 if (isset($aPlace)) {
54     $aOutlineResult = $oPlaceLookup->getOutlines(
55         $aPlace['place_id'],
56         $aPlace['lon'],
57         $aPlace['lat'],
58         Nominatim\ClassTypes\getProperty($aPlace, 'defdiameter', 0.0001),
59         $fLat,
60         $fLon
61     );
62
63     if ($aOutlineResult) {
64         $aPlace = array_merge($aPlace, $aOutlineResult);
65     }
66 } else {
67     $aPlace = array();
68 }
69
70 logEnd($oDB, $hLog, count($aPlace) ? 1 : 0);
71
72 if (CONST_Debug) {
73     var_dump($aPlace);
74     exit;
75 }
76
77 if ($sOutputFormat == 'html') {
78     $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
79     $sTileURL = CONST_Map_Tile_URL;
80     $sTileAttribution = CONST_Map_Tile_Attribution;
81 } elseif ($sOutputFormat == 'geocodejson') {
82     $sQuery = $fLat.','.$fLon;
83     if (isset($aPlace['place_id'])) {
84         $fDistance = $oDB->getOne(
85             'SELECT ST_Distance(ST_SetSRID(ST_Point(:lon,:lat),4326), centroid) FROM placex where place_id = :placeid',
86             array(':lon' => $fLon, ':lat' => $fLat, ':placeid' => $aPlace['place_id'])
87         );
88     }
89 }
90
91 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
92 include(CONST_BasePath.'/lib/template/address-'.$sOutputTemplate.'.php');