2 @define('CONST_ConnectionBucket_PageType', 'Details');
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');
10 $oParams = new Nominatim\ParameterParser();
12 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
14 $aLangPrefOrder = $oParams->getPreferredLanguages();
15 $sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
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');
22 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
23 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', $sOutputFormat == 'html');
24 $bIncludeChildPlaces = $oParams->getBool('childplaces', $sOutputFormat == 'html');
25 $bGroupParents = $oParams->getBool('group_parents', false);
29 if ($sOsmType && $iOsmId > 0) {
31 "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
35 // osm_type and osm_id are not unique enough
37 $sSQL .= " AND class='".$sClass."'";
39 $sSQL .= ' ORDER BY class ASC';
40 $sPlaceId = chksql($oDB->getOne($sSQL));
42 // Be nice about our error messages for broken geometry
46 $sSQL .= ' osm_type, ';
48 $sSQL .= ' errormessage, ';
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 = '".$sOsmType."'";
56 $sSQL .= ' AND osm_id = '.$iOsmId;
57 $sSQL .= ' ORDER BY updated DESC';
59 $aPointDetails = chksql($oDB->getRow($sSQL));
60 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
61 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
62 $aPointDetails['error_x'] = $aMatches[1];
63 $aPointDetails['error_y'] = $aMatches[2];
65 $aPointDetails['error_x'] = 0;
66 $aPointDetails['error_y'] = 0;
68 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
75 if (!$sPlaceId) userError('Please select a place id');
77 $iPlaceID = (int)$sPlaceId;
79 if (CONST_Use_US_Tiger_Data) {
80 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
81 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
84 // interpolated house numbers
85 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
86 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
88 // artificial postcodes
89 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
90 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
92 if (CONST_Use_Aux_Location_data) {
93 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
94 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
97 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
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 .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') AS indexed_date,";
104 $sSQL .= ' parent_place_id, ';
105 $sSQL .= ' rank_address, ';
106 $sSQL .= ' rank_search, ';
107 $sSQL .= ' get_searchrank_label(rank_search) AS rank_search_label,';
108 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
109 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
110 //$sSQL .= " ST_Area(geometry::geography) AS area, ";
111 $sSQL .= ' ST_y(centroid) AS lat, ';
112 $sSQL .= ' ST_x(centroid) AS lon,';
114 $sSQL .= ' WHEN importance = 0 OR importance IS NULL THEN 0.75-(rank_search::float/40) ';
115 $sSQL .= ' ELSE importance ';
116 $sSQL .= ' END as calculated_importance, ';
117 $sSQL .= ' ST_AsGeoJSON(CASE ';
118 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
119 $sSQL .= ' ELSE geometry ';
120 $sSQL .= ' END) as asgeojson';
121 $sSQL .= ' FROM placex ';
122 $sSQL .= " WHERE place_id = $iPlaceID";
124 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
126 if (!$aPointDetails) {
127 userError('Unknown place id.');
130 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
132 $aClassType = getClassTypesWithImportance();
134 $sPointClassType = $aPointDetails['class'].':'.$aPointDetails['type'];
135 if (isset($aClassType[$sPointClassType]) && $aClassType[$sPointClassType]['icon']) {
136 $aPointDetails['icon'] = $aClassType[$sPointClassType]['icon'];
138 $aPointDetails['icon'] = false;
141 // Get all alternative names (languages, etc)
142 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
143 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
144 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
145 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
146 $aPointDetails['aNames'] = [];
150 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
151 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
152 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
153 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
154 $aPointDetails['aAddressTags'] = [];
158 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
159 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
160 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
161 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
162 $aPointDetails['aExtraTags'] = [];
166 $aAddressLines = false;
167 if ($bIncludeAddressDetails) {
168 $aAddressLines = getAddressDetails(
170 $sLanguagePrefArraySQL,
172 $aPointDetails['country_code'],
179 $aLinkedLines = false;
180 if ($bIncludeLinkedPlaces) {
181 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
182 $sSQL .= ' admin_level, rank_address, ';
183 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
184 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
185 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
186 $sSQL .= ' length(name::text) AS namelength ';
188 $sSQL .= ' placex, ';
190 $sSQL .= ' SELECT centroid AS placegeometry ';
191 $sSQL .= ' FROM placex ';
192 $sSQL .= " WHERE place_id = $iPlaceID ";
194 $sSQL .= " WHERE linked_place_id = $iPlaceID";
195 $sSQL .= ' ORDER BY ';
196 $sSQL .= ' rank_address ASC, ';
197 $sSQL .= ' rank_search ASC, ';
198 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
199 $sSQL .= ' housenumber';
200 $aLinkedLines = $oDB->getAll($sSQL);
201 if (PEAR::isError($aLinkedLines)) { // possible timeout
206 // All places this is an imediate parent of
207 $aParentOfLines = false;
208 if ($bIncludeChildPlaces) {
209 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
210 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
211 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
212 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
213 $sSQL .= ' length(name::text) AS namelength ';
216 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
217 $sSQL .= ' FROM placex ';
218 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
219 $sSQL .= ' ORDER BY ';
220 $sSQL .= ' rank_address ASC, ';
221 $sSQL .= ' rank_search ASC ';
222 $sSQL .= ' LIMIT 500 ';
223 $sSQL .= ' ) AS obj,';
225 $sSQL .= ' SELECT centroid AS placegeometry ';
226 $sSQL .= ' FROM placex ';
227 $sSQL .= " WHERE place_id = $iPlaceID ";
229 $sSQL .= ' ORDER BY ';
230 $sSQL .= ' rank_address ASC, ';
231 $sSQL .= ' rank_search ASC, ';
232 $sSQL .= ' localname, ';
233 $sSQL .= ' housenumber';
234 $aParentOfLines = $oDB->getAll($sSQL);
235 if (PEAR::isError($aParentOfLines)) { // possible timeout
236 $aParentOfLines = [];
240 $aPlaceSearchNameKeywords = false;
241 $aPlaceSearchAddressKeywords = false;
242 if ($oParams->getBool('keywords')) {
243 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
244 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
245 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
246 $aPlaceSearchName = [];
249 if (!empty($aPlaceSearchName)) {
250 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
251 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
252 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
253 $aPlaceSearchNameKeywords = [];
256 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
257 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
258 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
259 $aPlaceSearchAddressKeywords = [];
264 logEnd($oDB, $hLog, 1);
266 if ($sOutputFormat=='html') {
267 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
268 $sDataDate = chksql($oDB->getOne($sSQL));
269 $sTileURL = CONST_Map_Tile_URL;
270 $sTileAttribution = CONST_Map_Tile_Attribution;
273 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');