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