3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 require_once(CONST_LibDir.'/init-website.php');
12 require_once(CONST_LibDir.'/log.php');
13 require_once(CONST_LibDir.'/PlaceLookup.php');
14 require_once(CONST_LibDir.'/ReverseGeocode.php');
15 require_once(CONST_LibDir.'/output.php');
16 ini_set('memory_limit', '200M');
18 $oParams = new Nominatim\ParameterParser();
21 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
22 set_exception_handler_by_format($sOutputFormat);
25 $aLangPrefOrder = $oParams->getPreferredLanguages();
27 $oDB = new Nominatim\DB(CONST_Database_DSN);
30 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
32 $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
33 $oPlaceLookup->loadParamArray($oParams);
34 $oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true));
36 $sOsmType = $oParams->getSet('osm_type', array('N', 'W', 'R'));
37 $iOsmId = $oParams->getInt('osm_id', -1);
38 $fLat = $oParams->getFloat('lat');
39 $fLon = $oParams->getFloat('lon');
40 $iZoom = $oParams->getInt('zoom', 18);
42 if ($sOsmType && $iOsmId > 0) {
43 $aPlace = $oPlaceLookup->lookupOSMID($sOsmType, $iOsmId);
44 } elseif ($fLat !== false && $fLon !== false) {
45 $oReverseGeocode = new Nominatim\ReverseGeocode($oDB);
46 $oReverseGeocode->setZoom($iZoom);
48 $oLookup = $oReverseGeocode->lookup($fLat, $fLon);
51 $aPlaces = $oPlaceLookup->lookup(array($oLookup->iId => $oLookup));
52 if (!empty($aPlaces)) {
53 $aPlace = reset($aPlaces);
57 userError('Need coordinates or OSM object to lookup.');
61 $aOutlineResult = $oPlaceLookup->getOutlines(
65 Nominatim\ClassTypes\getDefRadius($aPlace),
70 if ($aOutlineResult) {
71 $aPlace = array_merge($aPlace, $aOutlineResult);
77 logEnd($oDB, $hLog, count($aPlace) ? 1 : 0);
84 if ($sOutputFormat == 'geocodejson') {
85 $sQuery = $fLat.','.$fLon;
86 if (isset($aPlace['place_id'])) {
87 $fDistance = $oDB->getOne(
88 'SELECT ST_Distance(ST_SetSRID(ST_Point(:lon,:lat),4326), centroid) FROM placex where place_id = :placeid',
89 array(':lon' => $fLon, ':lat' => $fLat, ':placeid' => $aPlace['place_id'])
94 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
95 include(CONST_LibDir.'/template/address-'.$sOutputTemplate.'.php');