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.'/output.php');
14 require_once(CONST_LibDir.'/AddressDetails.php');
15 ini_set('memory_limit', '200M');
17 $oParams = new Nominatim\ParameterParser();
19 $sOutputFormat = $oParams->getSet('format', array('json'), 'json');
20 set_exception_handler_by_format($sOutputFormat);
22 $aLangPrefOrder = $oParams->getPreferredLanguages();
24 $sPlaceId = $oParams->getString('place_id');
25 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
26 $iOsmId = $oParams->getInt('osmid', -1);
27 $sClass = $oParams->getString('class');
29 $bIncludeKeywords = $oParams->getBool('keywords', false);
30 $bIncludeAddressDetails = $oParams->getBool('addressdetails', false);
31 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
32 $bIncludeHierarchy = $oParams->getBool('hierarchy', false);
33 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
34 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', false);
36 $oDB = new Nominatim\DB(CONST_Database_DSN);
39 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
41 if ($sOsmType && $iOsmId > 0) {
42 $sSQL = 'SELECT place_id FROM placex WHERE osm_type = :type AND osm_id = :id';
43 $aSQLParams = array(':type' => $sOsmType, ':id' => $iOsmId);
44 // osm_type and osm_id are not unique enough
46 $sSQL .= ' AND class= :class';
47 $aSQLParams[':class'] = $sClass;
49 $sSQL .= ' ORDER BY class ASC';
50 $sPlaceId = $oDB->getOne($sSQL, $aSQLParams);
53 // Nothing? Maybe it's an interpolation.
54 // XXX Simply returns the first parent street it finds. It should
55 // get a house number and get the right interpolation.
56 if (!$sPlaceId && $sOsmType == 'W' && (!$sClass || $sClass == 'place')) {
57 $sSQL = 'SELECT place_id FROM location_property_osmline'
58 .' WHERE osm_id = :id LIMIT 1';
59 $sPlaceId = $oDB->getOne($sSQL, array(':id' => $iOsmId));
62 // Be nice about our error messages for broken geometry
64 if (!$sPlaceId && $oDB->tableExists('import_polygon_error')) {
66 $sSQL .= ' osm_type, ';
68 $sSQL .= ' errormessage, ';
71 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
72 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
73 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
74 $sSQL .= ' FROM import_polygon_error ';
75 $sSQL .= ' WHERE osm_type = :type';
76 $sSQL .= ' AND osm_id = :id';
77 $sSQL .= ' ORDER BY updated DESC';
79 $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
81 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
82 $aPointDetails['error_x'] = $aMatches[1];
83 $aPointDetails['error_y'] = $aMatches[2];
85 $aPointDetails['error_x'] = 0;
86 $aPointDetails['error_y'] = 0;
88 include(CONST_LibDir.'/template/details-error-'.$sOutputFormat.'.php');
93 if ($sPlaceId === false) {
94 throw new \Exception('No place with that OSM ID found.', 404);
97 if ($sPlaceId === false) {
98 userError('Required parameters missing. Need either osmtype/osmid or place_id.');
102 $iPlaceID = (int)$sPlaceId;
104 if (CONST_Use_US_Tiger_Data) {
105 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
106 if ($iParentPlaceID) {
107 $iPlaceID = $iParentPlaceID;
111 // interpolated house numbers
112 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
113 if ($iParentPlaceID) {
114 $iPlaceID = $iParentPlaceID;
117 // artificial postcodes
118 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
119 if ($iParentPlaceID) {
120 $iPlaceID = $iParentPlaceID;
123 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
125 // Get the details for this point
126 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
127 $sSQL .= ' housenumber, postcode, country_code,';
128 $sSQL .= ' importance, wikipedia,';
129 $sSQL .= ' ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
130 $sSQL .= ' parent_place_id, ';
131 $sSQL .= ' rank_address, ';
132 $sSQL .= ' rank_search, ';
133 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
134 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
135 $sSQL .= ' ST_y(centroid) AS lat, ';
136 $sSQL .= ' ST_x(centroid) AS lon, ';
138 $sSQL .= ' WHEN importance = 0 OR importance IS NULL ';
139 $sSQL .= ' THEN 0.75-(rank_search::float/40) ';
140 $sSQL .= ' ELSE importance ';
141 $sSQL .= ' END as calculated_importance, ';
142 if ($bIncludePolygonAsGeoJSON) {
143 $sSQL .= ' ST_AsGeoJSON(CASE ';
144 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 ';
145 $sSQL .= ' THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
146 $sSQL .= ' ELSE geometry ';
147 $sSQL .= ' END) as asgeojson';
149 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
151 $sSQL .= ' FROM placex ';
152 $sSQL .= " WHERE place_id = $iPlaceID";
154 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
156 if (!$aPointDetails) {
157 throw new \Exception('No place with that place ID found.', 404);
160 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
162 // Get all alternative names (languages, etc)
163 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
164 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
165 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
168 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
169 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
170 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
173 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
174 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
175 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
178 $aAddressLines = false;
179 if ($bIncludeAddressDetails) {
180 $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
181 $aAddressLines = $oDetails->getAddressDetails(true);
185 $aLinkedLines = false;
186 if ($bIncludeLinkedPlaces) {
187 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
188 $sSQL .= ' admin_level, rank_address, ';
189 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
190 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
191 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
192 $sSQL .= ' length(name::text) AS namelength ';
194 $sSQL .= ' placex, ';
196 $sSQL .= ' SELECT centroid AS placegeometry ';
197 $sSQL .= ' FROM placex ';
198 $sSQL .= " WHERE place_id = $iPlaceID ";
200 $sSQL .= " WHERE linked_place_id = $iPlaceID";
201 $sSQL .= ' ORDER BY ';
202 $sSQL .= ' rank_address ASC, ';
203 $sSQL .= ' rank_search ASC, ';
204 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
205 $sSQL .= ' housenumber';
206 $aLinkedLines = $oDB->getAll($sSQL);
209 // All places this is an imediate parent of
210 $aHierarchyLines = false;
211 if ($bIncludeHierarchy) {
212 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
213 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
214 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
215 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
216 $sSQL .= ' length(name::text) AS namelength ';
219 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
220 $sSQL .= ' FROM placex ';
221 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
222 $sSQL .= ' ORDER BY ';
223 $sSQL .= ' rank_address ASC, ';
224 $sSQL .= ' rank_search ASC ';
225 $sSQL .= ' LIMIT 500 ';
226 $sSQL .= ' ) AS obj,';
228 $sSQL .= ' SELECT centroid AS placegeometry ';
229 $sSQL .= ' FROM placex ';
230 $sSQL .= " WHERE place_id = $iPlaceID ";
232 $sSQL .= ' ORDER BY ';
233 $sSQL .= ' rank_address ASC, ';
234 $sSQL .= ' rank_search ASC, ';
235 $sSQL .= ' localname, ';
236 $sSQL .= ' housenumber';
237 $aHierarchyLines = $oDB->getAll($sSQL);
240 $aPlaceSearchNameKeywords = false;
241 $aPlaceSearchAddressKeywords = false;
242 if ($bIncludeKeywords) {
243 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
244 $aPlaceSearchName = $oDB->getRow($sSQL);
246 if (!empty($aPlaceSearchName)) {
247 $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
248 if (!empty($sWordIds)) {
249 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
250 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
253 $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
254 if (!empty($sWordIds)) {
255 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
256 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
261 logEnd($oDB, $hLog, 1);
263 include(CONST_LibDir.'/template/details-'.$sOutputFormat.'.php');