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');
9 $oParams = new Nominatim\ParameterParser();
11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
12 set_exception_handler_by_format($sOutputFormat);
14 $aLangPrefOrder = $oParams->getPreferredLanguages();
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');
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');
28 $oDB = new Nominatim\DB();
31 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
33 if ($sOutputFormat == 'html' && !$sPlaceId && !$sOsmType) {
34 include(CONST_BasePath.'/lib/template/details-index-html.php');
38 if ($sOsmType && $iOsmId > 0) {
39 $sSQL = 'SELECT place_id FROM placex WHERE osm_type = :type AND osm_id = :id';
40 // osm_type and osm_id are not unique enough
42 $sSQL .= " AND class='".$sClass."'";
44 $sSQL .= ' ORDER BY class ASC';
45 $sPlaceId = $oDB->getOne($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
48 // Nothing? Maybe it's an interpolation.
49 // XXX Simply returns the first parent street it finds. It should
50 // get a house number and get the right interpolation.
51 if (!$sPlaceId && $sOsmType == 'W' && (!$sClass || $sClass == 'place')) {
52 $sSQL = 'SELECT place_id FROM location_property_osmline'
53 .' WHERE osm_id = :id LIMIT 1';
54 $sPlaceId = $oDB->getOne($sSQL, array(':id' => $iOsmId));
57 // Be nice about our error messages for broken geometry
61 $sSQL .= ' osm_type, ';
63 $sSQL .= ' errormessage, ';
66 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
67 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
68 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
69 $sSQL .= ' FROM import_polygon_error ';
70 $sSQL .= ' WHERE osm_type = :type';
71 $sSQL .= ' AND osm_id = :id';
72 $sSQL .= ' ORDER BY updated DESC';
74 $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
76 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
77 $aPointDetails['error_x'] = $aMatches[1];
78 $aPointDetails['error_y'] = $aMatches[2];
80 $aPointDetails['error_x'] = 0;
81 $aPointDetails['error_y'] = 0;
83 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
90 if ($sPlaceId === false) userError('Please select a place id');
92 $iPlaceID = (int)$sPlaceId;
94 if (CONST_Use_US_Tiger_Data) {
95 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
96 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
99 // interpolated house numbers
100 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
101 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
103 // artificial postcodes
104 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
105 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
107 if (CONST_Use_Aux_Location_data) {
108 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID);
109 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
112 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
114 // Get the details for this point
115 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
116 $sSQL .= ' housenumber, postcode, country_code,';
117 $sSQL .= ' importance, wikipedia,';
118 $sSQL .= ' ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
119 $sSQL .= ' parent_place_id, ';
120 $sSQL .= ' rank_address, ';
121 $sSQL .= ' rank_search, ';
122 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
123 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
124 $sSQL .= ' ST_y(centroid) AS lat, ';
125 $sSQL .= ' ST_x(centroid) AS lon, ';
127 $sSQL .= ' WHEN importance = 0 OR importance IS NULL ';
128 $sSQL .= ' THEN 0.75-(rank_search::float/40) ';
129 $sSQL .= ' ELSE importance ';
130 $sSQL .= ' END as calculated_importance, ';
131 if ($bIncludePolygonAsGeoJSON) {
132 $sSQL .= ' ST_AsGeoJSON(CASE ';
133 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 ';
134 $sSQL .= ' THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
135 $sSQL .= ' ELSE geometry ';
136 $sSQL .= ' END) as asgeojson';
138 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
140 $sSQL .= ' FROM placex ';
141 $sSQL .= " WHERE place_id = $iPlaceID";
143 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
145 if (!$aPointDetails) {
146 userError('Unknown place id.');
149 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
150 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
151 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
153 // Get all alternative names (languages, etc)
154 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
155 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
156 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
159 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
160 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
161 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
164 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
165 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
166 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
169 $aAddressLines = false;
170 if ($bIncludeAddressDetails) {
171 $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
172 $aAddressLines = $oDetails->getAddressDetails(true);
176 $aLinkedLines = false;
177 if ($bIncludeLinkedPlaces) {
178 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
179 $sSQL .= ' admin_level, rank_address, ';
180 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
181 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
182 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
183 $sSQL .= ' length(name::text) AS namelength ';
185 $sSQL .= ' placex, ';
187 $sSQL .= ' SELECT centroid AS placegeometry ';
188 $sSQL .= ' FROM placex ';
189 $sSQL .= " WHERE place_id = $iPlaceID ";
191 $sSQL .= " WHERE linked_place_id = $iPlaceID";
192 $sSQL .= ' ORDER BY ';
193 $sSQL .= ' rank_address ASC, ';
194 $sSQL .= ' rank_search ASC, ';
195 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
196 $sSQL .= ' housenumber';
197 $aLinkedLines = $oDB->getAll($sSQL);
200 // All places this is an imediate parent of
201 $aHierarchyLines = false;
202 if ($bIncludeHierarchy) {
203 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
204 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
205 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
206 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
207 $sSQL .= ' length(name::text) AS namelength ';
210 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
211 $sSQL .= ' FROM placex ';
212 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
213 $sSQL .= ' ORDER BY ';
214 $sSQL .= ' rank_address ASC, ';
215 $sSQL .= ' rank_search ASC ';
216 $sSQL .= ' LIMIT 500 ';
217 $sSQL .= ' ) AS obj,';
219 $sSQL .= ' SELECT centroid AS placegeometry ';
220 $sSQL .= ' FROM placex ';
221 $sSQL .= " WHERE place_id = $iPlaceID ";
223 $sSQL .= ' ORDER BY ';
224 $sSQL .= ' rank_address ASC, ';
225 $sSQL .= ' rank_search ASC, ';
226 $sSQL .= ' localname, ';
227 $sSQL .= ' housenumber';
228 $aHierarchyLines = $oDB->getAll($sSQL);
231 $aPlaceSearchNameKeywords = false;
232 $aPlaceSearchAddressKeywords = false;
233 if ($bIncludeKeywords) {
234 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
235 $aPlaceSearchName = $oDB->getRow($sSQL);
237 if (!empty($aPlaceSearchName)) {
238 $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
239 if (!empty($sWordIds)) {
240 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
241 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
244 $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
245 if (!empty($sWordIds)) {
246 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
247 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
252 logEnd($oDB, $hLog, 1);
254 if ($sOutputFormat=='html') {
255 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
256 $sDataDate = $oDB->getOne($sSQL);
257 $sTileURL = CONST_Map_Tile_URL;
258 $sTileAttribution = CONST_Map_Tile_Attribution;
261 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');