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