]> git.openstreetmap.org Git - nominatim.git/blob - website/details.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / website / details.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/output.php');
6 require_once(CONST_BasePath.'/lib/AddressDetails.php');
7 ini_set('memory_limit', '200M');
8
9 $oParams = new Nominatim\ParameterParser();
10
11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
12 set_exception_handler_by_format($sOutputFormat);
13
14 $aLangPrefOrder = $oParams->getPreferredLanguages();
15
16 $sPlaceId = $oParams->getString('place_id');
17 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
18 $iOsmId = $oParams->getInt('osmid', -1);
19 $sClass = $oParams->getString('class');
20
21 $bIncludeKeywords = $oParams->getBool('keywords', false);
22 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
23 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
24 $bIncludeHierarchy = $oParams->getBool('hierarchy', $sOutputFormat == 'html');
25 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
26 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', $sOutputFormat == 'html');
27
28 $oDB = new Nominatim\DB();
29 $oDB->connect();
30
31 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
32
33 if ($sOutputFormat == 'html' && !$sPlaceId && !$sOsmType) {
34     include(CONST_BasePath.'/lib/template/details-index-html.php');
35     exit;
36 }
37
38 if ($sOsmType && $iOsmId > 0) {
39     $sSQL = 'SELECT place_id FROM placex WHERE osm_type = :type AND osm_id = :id';
40     $aSQLParams = array(':type' => $sOsmType, ':id' => $iOsmId);
41     // osm_type and osm_id are not unique enough
42     if ($sClass) {
43         $sSQL .= ' AND class= :class';
44         $aSQLParams[':class'] = $sClass;
45     }
46     $sSQL .= ' ORDER BY class ASC';
47     $sPlaceId = $oDB->getOne($sSQL, $aSQLParams);
48
49
50     // Nothing? Maybe it's an interpolation.
51     // XXX Simply returns the first parent street it finds. It should
52     //     get a house number and get the right interpolation.
53     if (!$sPlaceId && $sOsmType == 'W' && (!$sClass || $sClass == 'place')) {
54         $sSQL = 'SELECT place_id FROM location_property_osmline'
55                 .' WHERE osm_id = :id LIMIT 1';
56         $sPlaceId = $oDB->getOne($sSQL, array(':id' => $iOsmId));
57     }
58
59     // Be nice about our error messages for broken geometry
60
61     if (!$sPlaceId) {
62         $sSQL = 'SELECT ';
63         $sSQL .= '    osm_type, ';
64         $sSQL .= '    osm_id, ';
65         $sSQL .= '    errormessage, ';
66         $sSQL .= '    class, ';
67         $sSQL .= '    type, ';
68         $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
69         $sSQL .= '    ST_AsText(prevgeometry) AS prevgeom, ';
70         $sSQL .= '    ST_AsText(newgeometry) AS newgeom';
71         $sSQL .= ' FROM import_polygon_error ';
72         $sSQL .= ' WHERE osm_type = :type';
73         $sSQL .= '   AND osm_id = :id';
74         $sSQL .= ' ORDER BY updated DESC';
75         $sSQL .= ' LIMIT 1';
76         $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
77         if ($aPointDetails) {
78             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
79                 $aPointDetails['error_x'] = $aMatches[1];
80                 $aPointDetails['error_y'] = $aMatches[2];
81             } else {
82                 $aPointDetails['error_x'] = 0;
83                 $aPointDetails['error_y'] = 0;
84             }
85             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
86             exit;
87         }
88     }
89 }
90
91
92 if ($sPlaceId === false) userError('Please select a place id');
93
94 $iPlaceID = (int)$sPlaceId;
95
96 if (CONST_Use_US_Tiger_Data) {
97     $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
98     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
99 }
100
101 // interpolated house numbers
102 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
103 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
104
105 // artificial postcodes
106 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
107 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
108
109 if (CONST_Use_Aux_Location_data) {
110     $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID);
111     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
112 }
113
114 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
115
116 // Get the details for this point
117 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
118 $sSQL .= '    housenumber, postcode, country_code,';
119 $sSQL .= '    importance, wikipedia,';
120 $sSQL .= '    ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
121 $sSQL .= '    parent_place_id, ';
122 $sSQL .= '    rank_address, ';
123 $sSQL .= '    rank_search, ';
124 $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
125 $sSQL .= "    ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
126 $sSQL .= '    ST_y(centroid) AS lat, ';
127 $sSQL .= '    ST_x(centroid) AS lon, ';
128 $sSQL .= '    CASE ';
129 $sSQL .= '       WHEN importance = 0 OR importance IS NULL ';
130 $sSQL .= '       THEN 0.75-(rank_search::float/40) ';
131 $sSQL .= '       ELSE importance ';
132 $sSQL .= '       END as calculated_importance, ';
133 if ($bIncludePolygonAsGeoJSON) {
134     $sSQL .= '    ST_AsGeoJSON(CASE ';
135     $sSQL .= '                WHEN ST_NPoints(geometry) > 5000 ';
136     $sSQL .= '                THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
137     $sSQL .= '                ELSE geometry ';
138     $sSQL .= '                END) as asgeojson';
139 } else {
140     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
141 }
142 $sSQL .= ' FROM placex ';
143 $sSQL .= " WHERE place_id = $iPlaceID";
144
145 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
146
147 if (!$aPointDetails) {
148     userError('Unknown place id.');
149 }
150
151 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
152 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
153
154 // Get all alternative names (languages, etc)
155 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
156 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
157 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
158
159 // Address tags
160 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
161 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
162 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
163
164 // Extra tags
165 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
166 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
167 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
168
169 // Address
170 $aAddressLines = false;
171 if ($bIncludeAddressDetails) {
172     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
173     $aAddressLines = $oDetails->getAddressDetails(true);
174 }
175
176 // Linked places
177 $aLinkedLines = false;
178 if ($bIncludeLinkedPlaces) {
179     $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
180     $sSQL .= ' admin_level, rank_address, ';
181     $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
182     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
183     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
184     $sSQL .= ' length(name::text) AS namelength ';
185     $sSQL .= ' FROM ';
186     $sSQL .= '    placex, ';
187     $sSQL .= '    ( ';
188     $sSQL .= '      SELECT centroid AS placegeometry ';
189     $sSQL .= '      FROM placex ';
190     $sSQL .= "      WHERE place_id = $iPlaceID ";
191     $sSQL .= '    ) AS x';
192     $sSQL .= " WHERE linked_place_id = $iPlaceID";
193     $sSQL .= ' ORDER BY ';
194     $sSQL .= '   rank_address ASC, ';
195     $sSQL .= '   rank_search ASC, ';
196     $sSQL .= "   get_name_by_language(name, $sLanguagePrefArraySQL), ";
197     $sSQL .= '   housenumber';
198     $aLinkedLines = $oDB->getAll($sSQL);
199 }
200
201 // All places this is an imediate parent of
202 $aHierarchyLines = false;
203 if ($bIncludeHierarchy) {
204     $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
205     $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
206     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
207     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
208     $sSQL .= ' length(name::text) AS namelength ';
209     $sSQL .= ' FROM ';
210     $sSQL .= '    ( ';
211     $sSQL .= '      SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
212     $sSQL .= '      FROM placex ';
213     $sSQL .= "      WHERE parent_place_id = $iPlaceID ";
214     $sSQL .= '      ORDER BY ';
215     $sSQL .= '         rank_address ASC, ';
216     $sSQL .= '         rank_search ASC ';
217     $sSQL .= '      LIMIT 500 ';
218     $sSQL .= '    ) AS obj,';
219     $sSQL .= '    ( ';
220     $sSQL .= '      SELECT centroid AS placegeometry ';
221     $sSQL .= '      FROM placex ';
222     $sSQL .= "      WHERE place_id = $iPlaceID ";
223     $sSQL .= '    ) AS x';
224     $sSQL .= ' ORDER BY ';
225     $sSQL .= '    rank_address ASC, ';
226     $sSQL .= '    rank_search ASC, ';
227     $sSQL .= '    localname, ';
228     $sSQL .= '    housenumber';
229     $aHierarchyLines = $oDB->getAll($sSQL);
230 }
231
232 $aPlaceSearchNameKeywords = false;
233 $aPlaceSearchAddressKeywords = false;
234 if ($bIncludeKeywords) {
235     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
236     $aPlaceSearchName = $oDB->getRow($sSQL);
237
238     if (!empty($aPlaceSearchName)) {
239         $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
240         if (!empty($sWordIds)) {
241             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
242             $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
243         }
244
245         $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
246         if (!empty($sWordIds)) {
247             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
248             $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
249         }
250     }
251 }
252
253 logEnd($oDB, $hLog, 1);
254
255 if ($sOutputFormat=='html') {
256     $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
257     $sDataDate = $oDB->getOne($sSQL);
258     $sTileURL = CONST_Map_Tile_URL;
259     $sTileAttribution = CONST_Map_Tile_Attribution;
260 }
261
262 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');