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 $bIncludeKeywords = $oParams->getBool('keywords');
23 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
24 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', $sOutputFormat == 'html');
25 $bIncludeChildPlaces = $oParams->getBool('childplaces', $sOutputFormat == 'html');
26 $bGroupParents = $oParams->getBool('group_parents', false);
30 if ($sOsmType && $iOsmId > 0) {
32 "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
36 // osm_type and osm_id are not unique enough
38 $sSQL .= " AND class='".$sClass."'";
40 $sSQL .= ' ORDER BY class ASC';
41 $sPlaceId = chksql($oDB->getOne($sSQL));
43 // Be nice about our error messages for broken geometry
47 $sSQL .= ' osm_type, ';
49 $sSQL .= ' errormessage, ';
52 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
53 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
54 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
55 $sSQL .= ' FROM import_polygon_error ';
56 $sSQL .= " WHERE osm_type = '".$sOsmType."'";
57 $sSQL .= ' AND osm_id = '.$iOsmId;
58 $sSQL .= ' ORDER BY updated DESC';
60 $aPointDetails = chksql($oDB->getRow($sSQL));
61 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
62 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
63 $aPointDetails['error_x'] = $aMatches[1];
64 $aPointDetails['error_y'] = $aMatches[2];
66 $aPointDetails['error_x'] = 0;
67 $aPointDetails['error_y'] = 0;
69 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
76 if (!$sPlaceId) userError('Please select a place id');
78 $iPlaceID = (int)$sPlaceId;
80 if (CONST_Use_US_Tiger_Data) {
81 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
82 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
85 // interpolated house numbers
86 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
87 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
89 // artificial postcodes
90 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
91 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
93 if (CONST_Use_Aux_Location_data) {
94 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
95 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
98 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
100 // Get the details for this point
101 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
102 $sSQL .= ' housenumber, postcode, country_code,';
103 $sSQL .= ' importance, wikipedia,';
104 $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') AS indexed_date,";
105 $sSQL .= ' parent_place_id, ';
106 $sSQL .= ' rank_address, ';
107 $sSQL .= ' rank_search, ';
108 $sSQL .= ' get_searchrank_label(rank_search) AS rank_search_label,';
109 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
110 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
111 //$sSQL .= " ST_Area(geometry::geography) AS area, ";
112 $sSQL .= ' ST_y(centroid) AS lat, ';
113 $sSQL .= ' ST_x(centroid) AS lon,';
115 $sSQL .= ' WHEN importance = 0 OR importance IS NULL THEN 0.75-(rank_search::float/40) ';
116 $sSQL .= ' ELSE importance ';
117 $sSQL .= ' END as calculated_importance, ';
118 $sSQL .= ' ST_AsGeoJSON(CASE ';
119 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
120 $sSQL .= ' ELSE geometry ';
121 $sSQL .= ' END) as asgeojson';
122 $sSQL .= ' FROM placex ';
123 $sSQL .= " WHERE place_id = $iPlaceID";
125 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
127 if (!$aPointDetails) {
128 userError('Unknown place id.');
131 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
133 $aClassType = getClassTypesWithImportance();
135 $sPointClassType = $aPointDetails['class'].':'.$aPointDetails['type'];
136 if (isset($aClassType[$sPointClassType]) && $aClassType[$sPointClassType]['icon']) {
137 $aPointDetails['icon'] = $aClassType[$sPointClassType]['icon'];
139 $aPointDetails['icon'] = false;
142 // Get all alternative names (languages, etc)
143 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
144 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
145 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
146 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
147 $aPointDetails['aNames'] = [];
151 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
152 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
153 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
154 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
155 $aPointDetails['aAddressTags'] = [];
159 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
160 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
161 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
162 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
163 $aPointDetails['aExtraTags'] = [];
167 $aAddressLines = false;
168 if ($bIncludeAddressDetails) {
169 $aAddressLines = getAddressDetails(
171 $sLanguagePrefArraySQL,
173 $aPointDetails['country_code'],
180 $aLinkedLines = false;
181 if ($bIncludeLinkedPlaces) {
182 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
183 $sSQL .= ' admin_level, rank_address, ';
184 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
185 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
186 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
187 $sSQL .= ' length(name::text) AS namelength ';
189 $sSQL .= ' placex, ';
191 $sSQL .= ' SELECT centroid AS placegeometry ';
192 $sSQL .= ' FROM placex ';
193 $sSQL .= " WHERE place_id = $iPlaceID ";
195 $sSQL .= " WHERE linked_place_id = $iPlaceID";
196 $sSQL .= ' ORDER BY ';
197 $sSQL .= ' rank_address ASC, ';
198 $sSQL .= ' rank_search ASC, ';
199 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
200 $sSQL .= ' housenumber';
201 $aLinkedLines = $oDB->getAll($sSQL);
202 if (PEAR::isError($aLinkedLines)) { // possible timeout
207 // All places this is an imediate parent of
208 $aParentOfLines = false;
209 if ($bIncludeChildPlaces) {
210 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
211 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
212 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
213 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
214 $sSQL .= ' length(name::text) AS namelength ';
217 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
218 $sSQL .= ' FROM placex ';
219 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
220 $sSQL .= ' ORDER BY ';
221 $sSQL .= ' rank_address ASC, ';
222 $sSQL .= ' rank_search ASC ';
223 $sSQL .= ' LIMIT 500 ';
224 $sSQL .= ' ) AS obj,';
226 $sSQL .= ' SELECT centroid AS placegeometry ';
227 $sSQL .= ' FROM placex ';
228 $sSQL .= " WHERE place_id = $iPlaceID ";
230 $sSQL .= ' ORDER BY ';
231 $sSQL .= ' rank_address ASC, ';
232 $sSQL .= ' rank_search ASC, ';
233 $sSQL .= ' localname, ';
234 $sSQL .= ' housenumber';
235 $aParentOfLines = $oDB->getAll($sSQL);
236 if (PEAR::isError($aParentOfLines)) { // possible timeout
237 $aParentOfLines = [];
241 $aPlaceSearchNameKeywords = false;
242 $aPlaceSearchAddressKeywords = false;
243 if ($bIncludeKeywords) {
244 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
245 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
246 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
247 $aPlaceSearchName = [];
250 if (!empty($aPlaceSearchName)) {
251 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
252 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
253 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
254 $aPlaceSearchNameKeywords = [];
257 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
258 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
259 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
260 $aPlaceSearchAddressKeywords = [];
265 logEnd($oDB, $hLog, 1);
267 if ($sOutputFormat=='html') {
268 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
269 $sDataDate = chksql($oDB->getOne($sSQL));
270 $sTileURL = CONST_Map_Tile_URL;
271 $sTileAttribution = CONST_Map_Tile_Attribution;
274 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');