]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/details.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / website / details.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
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');
16
17 $oParams = new Nominatim\ParameterParser();
18
19 $sOutputFormat = $oParams->getSet('format', array('json'), 'json');
20 set_exception_handler_by_format($sOutputFormat);
21
22 $aLangPrefOrder = $oParams->getPreferredLanguages();
23
24 $sPlaceId = $oParams->getString('place_id');
25 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
26 $iOsmId = $oParams->getInt('osmid', 0);
27 $sClass = $oParams->getString('class');
28
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);
35
36 $oDB = new Nominatim\DB(CONST_Database_DSN);
37 $oDB->connect();
38
39 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
40
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
45     if ($sClass) {
46         $sSQL .= ' AND class= :class';
47         $aSQLParams[':class'] = $sClass;
48     }
49     $sSQL .= ' ORDER BY class ASC';
50     $sPlaceId = $oDB->getOne($sSQL, $aSQLParams);
51
52
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));
60     }
61
62     // Be nice about our error messages for broken geometry
63
64     if (!$sPlaceId && $oDB->tableExists('import_polygon_error')) {
65         $sSQL = 'SELECT ';
66         $sSQL .= '    osm_type, ';
67         $sSQL .= '    osm_id, ';
68         $sSQL .= '    errormessage, ';
69         $sSQL .= '    class, ';
70         $sSQL .= '    type, ';
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';
78         $sSQL .= ' LIMIT 1';
79         $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
80         if ($aPointDetails) {
81             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
82                 $aPointDetails['error_x'] = $aMatches[1];
83                 $aPointDetails['error_y'] = $aMatches[2];
84             } else {
85                 $aPointDetails['error_x'] = 0;
86                 $aPointDetails['error_y'] = 0;
87             }
88             include(CONST_LibDir.'/template/details-error-'.$sOutputFormat.'.php');
89             exit;
90         }
91     }
92
93     if ($sPlaceId === false) {
94         throw new \Exception('No place with that OSM ID found.', 404);
95     }
96 } else {
97     if ($sPlaceId === false) {
98         userError('Required parameters missing. Need either osmtype/osmid or place_id.');
99     }
100 }
101
102 $iPlaceID = (int)$sPlaceId;
103
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;
108     }
109 }
110
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;
115 }
116
117 // artificial postcodes
118 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
119 if ($iParentPlaceID) {
120     $iPlaceID = $iParentPlaceID;
121 }
122
123 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
124
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, ';
137 $sSQL .= '    CASE ';
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';
148 } else {
149     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
150 }
151 $sSQL .= ' FROM placex ';
152 $sSQL .= " WHERE place_id = $iPlaceID";
153
154 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
155
156 if (!$aPointDetails) {
157     throw new \Exception('No place with that place ID found.', 404);
158 }
159
160 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
161
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);
166
167 // Address tags
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);
171
172 // Extra tags
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);
176
177 // Address
178 $aAddressLines = false;
179 if ($bIncludeAddressDetails) {
180     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
181     $aAddressLines = $oDetails->getAddressDetails(true);
182 }
183
184 // Linked places
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 ';
193     $sSQL .= ' FROM ';
194     $sSQL .= '    placex, ';
195     $sSQL .= '    ( ';
196     $sSQL .= '      SELECT centroid AS placegeometry ';
197     $sSQL .= '      FROM placex ';
198     $sSQL .= "      WHERE place_id = $iPlaceID ";
199     $sSQL .= '    ) AS x';
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);
207 }
208
209 // All places this is an immediate 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 ';
217     $sSQL .= ' FROM ';
218     $sSQL .= '    ( ';
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,';
227     $sSQL .= '    ( ';
228     $sSQL .= '      SELECT centroid AS placegeometry ';
229     $sSQL .= '      FROM placex ';
230     $sSQL .= "      WHERE place_id = $iPlaceID ";
231     $sSQL .= '    ) AS x';
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);
238 }
239
240 $aPlaceSearchNameKeywords = false;
241 $aPlaceSearchAddressKeywords = false;
242 if ($bIncludeKeywords) {
243     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
244     $aPlaceSearchName = $oDB->getRow($sSQL);
245
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);
251         }
252
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);
257         }
258     }
259 }
260
261 logEnd($oDB, $hLog, 1);
262
263 include(CONST_LibDir.'/template/details-'.$sOutputFormat.'.php');