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');
23 $bIncludeAddressDetails = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('addressdetails');
24 $bIncludeLinkedPlaces = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('linkedplaces');
25 $bIncludeChildPlaces = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('childplaces');
26 $bGroupParents = $oParams->getBool('group_parents', 0);
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 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 WHERE place_id = $iPlaceID ORDER BY key";
151 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
152 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
153 $aPointDetails['aAddressTags'] = [];
157 $sSQL = "SELECT (each(extratags)).key,(each(extratags)).value FROM placex WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
158 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
159 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
160 $aPointDetails['aExtraTags'] = [];
164 $aAddressLines = false;
165 if ($bIncludeAddressDetails) {
166 $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], -1, true);
170 $aLinkedLines = false;
171 if ($bIncludeLinkedPlaces) {
172 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
173 $sSQL .= ' admin_level, rank_address, ';
174 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
175 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
176 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
177 $sSQL .= ' length(name::text) AS namelength ';
179 $sSQL .= ' placex, ';
181 $sSQL .= ' SELECT centroid AS placegeometry ';
182 $sSQL .= ' FROM placex ';
183 $sSQL .= " WHERE place_id = $iPlaceID ";
185 $sSQL .= " WHERE linked_place_id = $iPlaceID";
186 $sSQL .= ' ORDER BY ';
187 $sSQL .= ' rank_address ASC, ';
188 $sSQL .= ' rank_search ASC, ';
189 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
190 $sSQL .= ' housenumber';
191 $aLinkedLines = $oDB->getAll($sSQL);
192 if (PEAR::isError($aLinkedLines)) { // possible timeout
197 // All places this is an imediate parent of
198 $aParentOfLines = false;
199 if ($bIncludeChildPlaces) {
200 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
201 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
202 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
203 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
204 $sSQL .= ' length(name::text) AS namelength ';
207 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
208 $sSQL .= ' FROM placex ';
209 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
210 $sSQL .= ' ORDER BY ';
211 $sSQL .= ' rank_address ASC, ';
212 $sSQL .= ' rank_search ASC ';
213 $sSQL .= ' LIMIT 500 ';
214 $sSQL .= ' ) AS obj,';
216 $sSQL .= ' SELECT centroid AS placegeometry ';
217 $sSQL .= ' FROM placex ';
218 $sSQL .= " WHERE place_id = $iPlaceID ";
220 $sSQL .= ' ORDER BY ';
221 $sSQL .= ' rank_address ASC, ';
222 $sSQL .= ' rank_search ASC, ';
223 $sSQL .= ' localname, ';
224 $sSQL .= ' housenumber';
225 $aParentOfLines = $oDB->getAll($sSQL);
226 if (PEAR::isError($aParentOfLines)) { // possible timeout
227 $aParentOfLines = [];
231 $aPlaceSearchNameKeywords = false;
232 $aPlaceSearchAddressKeywords = false;
233 if ($oParams->getBool('keywords')) {
234 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
235 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
236 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
237 $aPlaceSearchName = [];
240 if (!empty($aPlaceSearchName)) {
241 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
242 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
243 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
244 $aPlaceSearchNameKeywords = [];
247 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
248 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
249 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
250 $aPlaceSearchAddressKeywords = [];
255 logEnd($oDB, $hLog, 1);
257 if ($sOutputFormat=='html') {
258 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
259 $sDataDate = chksql($oDB->getOne($sSQL));
260 $sTileURL = CONST_Map_Tile_URL;
261 $sTileAttribution = CONST_Map_Tile_Attribution;
264 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');