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