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 = 'html';
13 $aLangPrefOrder = $oParams->getPreferredLanguages();
14 $sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
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');
23 if ($sOsmType && $iOsmId > 0) {
25 "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
29 // osm_type and osm_id are not unique enough
31 $sSQL .= " AND class='".$sClass."'";
33 $sSQL .= ' ORDER BY class ASC';
34 $sPlaceId = chksql($oDB->getOne($sSQL));
36 // Be nice about our error messages for broken geometry
40 $sSQL .= ' osm_type, ';
42 $sSQL .= ' errormessage, ';
45 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
46 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
47 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
48 $sSQL .= ' FROM import_polygon_error ';
49 $sSQL .= " WHERE osm_type = '".$sOsmType."'";
50 $sSQL .= ' AND osm_id = '.$iOsmId;
51 $sSQL .= ' ORDER BY updated DESC';
53 $aPointDetails = chksql($oDB->getRow($sSQL));
54 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
55 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
56 $aPointDetails['error_x'] = $aMatches[1];
57 $aPointDetails['error_y'] = $aMatches[2];
59 $aPointDetails['error_x'] = 0;
60 $aPointDetails['error_y'] = 0;
62 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
69 if (!$sPlaceId) userError('Please select a place id');
71 $iPlaceID = (int)$sPlaceId;
73 if (CONST_Use_US_Tiger_Data) {
74 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
75 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
78 // interpolated house numbers
79 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
80 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
82 // artificial postcodes
83 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
84 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
86 if (CONST_Use_Aux_Location_data) {
87 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
88 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
91 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
93 // Get the details for this point
94 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
95 $sSQL .= ' housenumber, postcode, country_code,';
96 $sSQL .= ' importance, wikipedia,';
97 $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') AS indexed_date,";
98 $sSQL .= ' parent_place_id, ';
99 $sSQL .= ' rank_address, ';
100 $sSQL .= ' rank_search, ';
101 $sSQL .= ' get_searchrank_label(rank_search) AS rank_search_label,';
102 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
103 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
104 //$sSQL .= " ST_Area(geometry::geography) AS area, ";
105 $sSQL .= ' ST_y(centroid) AS lat, ';
106 $sSQL .= ' ST_x(centroid) AS lon,';
108 $sSQL .= ' WHEN importance = 0 OR importance IS NULL THEN 0.75-(rank_search::float/40) ';
109 $sSQL .= ' ELSE importance ';
110 $sSQL .= ' END as calculated_importance, ';
111 $sSQL .= ' ST_AsGeoJSON(CASE ';
112 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
113 $sSQL .= ' ELSE geometry ';
114 $sSQL .= ' END) as asgeojson';
115 $sSQL .= ' FROM placex ';
116 $sSQL .= " WHERE place_id = $iPlaceID";
118 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
120 if (!$aPointDetails) {
121 userError('Unknown place id.');
124 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
126 $aClassType = getClassTypesWithImportance();
128 $sPointClassType = $aPointDetails['class'].':'.$aPointDetails['type'];
129 if (isset($aClassType[$sPointClassType]) && $aClassType[$sPointClassType]['icon']) {
130 $aPointDetails['icon'] = $aClassType[$sPointClassType]['icon'];
132 $aPointDetails['icon'] = false;
135 // Get all alternative names (languages, etc)
136 $sSQL = "SELECT (each(name)).key,(each(name)).value FROM placex WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
137 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
138 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
139 $aPointDetails['aNames'] = [];
143 $sSQL = "SELECT (each(address)).key as key,(each(address)).value FROM placex WHERE place_id = $iPlaceID ORDER BY key";
144 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
145 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
146 $aPointDetails['aAddressTags'] = [];
150 $sSQL = "SELECT (each(extratags)).key,(each(extratags)).value FROM placex WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
151 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
152 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
153 $aPointDetails['aExtraTags'] = [];
157 $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], -1, true);
160 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
161 $sSQL .= ' admin_level, rank_address, ';
162 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
163 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
164 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
165 $sSQL .= ' length(name::text) AS namelength ';
167 $sSQL .= ' placex, ';
169 $sSQL .= ' SELECT centroid AS placegeometry ';
170 $sSQL .= ' FROM placex ';
171 $sSQL .= " WHERE place_id = $iPlaceID ";
173 $sSQL .= " WHERE linked_place_id = $iPlaceID";
174 $sSQL .= ' ORDER BY ';
175 $sSQL .= ' rank_address ASC, ';
176 $sSQL .= ' rank_search ASC, ';
177 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
178 $sSQL .= ' housenumber';
179 $aLinkedLines = $oDB->getAll($sSQL);
180 if (PEAR::isError($aLinkedLines)) { // possible timeout
184 // All places this is an imediate parent of
185 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
186 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
187 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
188 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
189 $sSQL .= ' length(name::text) AS namelength ';
192 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
193 $sSQL .= ' FROM placex ';
194 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
195 $sSQL .= ' ORDER BY ';
196 $sSQL .= ' rank_address ASC, ';
197 $sSQL .= ' rank_search ASC ';
198 $sSQL .= ' LIMIT 500 ';
199 $sSQL .= ' ) AS obj,';
201 $sSQL .= ' SELECT centroid AS placegeometry ';
202 $sSQL .= ' FROM placex ';
203 $sSQL .= " WHERE place_id = $iPlaceID ";
205 $sSQL .= ' ORDER BY ';
206 $sSQL .= ' rank_address ASC, ';
207 $sSQL .= ' rank_search ASC, ';
208 $sSQL .= ' localname, ';
209 $sSQL .= ' housenumber';
210 $aParentOfLines = $oDB->getAll($sSQL);
211 if (PEAR::isError($aParentOfLines)) { // possible timeout
212 $aParentOfLines = [];
215 $aPlaceSearchNameKeywords = false;
216 $aPlaceSearchAddressKeywords = false;
217 if ($oParams->getBool('keywords')) {
218 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
219 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
220 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
221 $aPlaceSearchName = [];
224 if (!empty($aPlaceSearchName)) {
225 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
226 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
227 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
228 $aPlaceSearchNameKeywords = [];
231 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
232 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
233 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
234 $aPlaceSearchAddressKeywords = [];
239 logEnd($oDB, $hLog, 1);
241 if ($sOutputFormat=='html') {
242 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
243 $sDataDate = chksql($oDB->getOne($sSQL));
244 $sTileURL = CONST_Map_Tile_URL;
245 $sTileAttribution = CONST_Map_Tile_Attribution;
248 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');