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', 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');
31 if ($sOsmType && $iOsmId > 0) {
33 "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
37 // osm_type and osm_id are not unique enough
39 $sSQL .= " AND class='".$sClass."'";
41 $sSQL .= ' ORDER BY class ASC';
42 $sPlaceId = chksql($oDB->getOne($sSQL));
44 // Be nice about our error messages for broken geometry
48 $sSQL .= ' osm_type, ';
50 $sSQL .= ' errormessage, ';
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';
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];
67 $aPointDetails['error_x'] = 0;
68 $aPointDetails['error_y'] = 0;
70 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
77 if (!$sPlaceId) userError('Please select a place id');
79 $iPlaceID = (int)$sPlaceId;
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;
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;
90 // artificial postcodes
91 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
92 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
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;
99 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
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, ';
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';
126 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
128 $sSQL .= ' FROM placex ';
129 $sSQL .= " WHERE place_id = $iPlaceID";
131 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
133 if (!$aPointDetails) {
134 userError('Unknown place id.');
137 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
139 $aClassType = getClassTypesWithImportance();
141 $sPointClassType = $aPointDetails['class'].':'.$aPointDetails['type'];
142 if (isset($aClassType[$sPointClassType]) && $aClassType[$sPointClassType]['icon']) {
143 $aPointDetails['icon'] = $aClassType[$sPointClassType]['icon'];
145 $aPointDetails['icon'] = false;
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();
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();
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();
173 $aAddressLines = false;
174 if ($bIncludeAddressDetails) {
175 $aAddressLines = getAddressDetails(
177 $sLanguagePrefArraySQL,
179 $aPointDetails['country_code'],
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 ';
195 $sSQL .= ' placex, ';
197 $sSQL .= ' SELECT centroid AS placegeometry ';
198 $sSQL .= ' FROM placex ';
199 $sSQL .= " WHERE place_id = $iPlaceID ";
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();
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 ';
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,';
232 $sSQL .= ' SELECT centroid AS placegeometry ';
233 $sSQL .= ' FROM placex ';
234 $sSQL .= " WHERE place_id = $iPlaceID ";
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();
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();
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();
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();
271 logEnd($oDB, $hLog, 1);
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;
280 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');