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);
22 if ($sOsmType && $iOsmId > 0) {
24 "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d ORDER BY type='postcode' ASC",
28 $sPlaceId = chksql($oDB->getOne($sSQL));
30 // Be nice about our error messages for broken geometry
34 $sSQL .= ' osm_type, ';
36 $sSQL .= ' errormessage, ';
39 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
40 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
41 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
42 $sSQL .= ' FROM import_polygon_error ';
43 $sSQL .= " WHERE osm_type = '".$sOsmType."'";
44 $sSQL .= ' AND osm_id = '.$iOsmId;
45 $sSQL .= ' ORDER BY updated DESC';
47 $aPointDetails = chksql($oDB->getRow($sSQL));
48 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
49 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
50 $aPointDetails['error_x'] = $aMatches[1];
51 $aPointDetails['error_y'] = $aMatches[2];
53 $aPointDetails['error_x'] = 0;
54 $aPointDetails['error_y'] = 0;
56 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
63 if (!$sPlaceId) userError('Please select a place id');
65 $iPlaceID = (int)$sPlaceId;
67 if (CONST_Use_US_Tiger_Data) {
68 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
69 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
72 // interpolated house numbers
73 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
74 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
76 // artificial postcodes
77 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
78 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
80 if (CONST_Use_Aux_Location_data) {
81 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
82 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
85 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
87 // Get the details for this point
88 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
89 $sSQL .= ' housenumber, postcode, country_code,';
90 $sSQL .= ' importance, wikipedia,';
91 $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') AS indexed_date,";
92 $sSQL .= ' parent_place_id, ';
93 $sSQL .= ' rank_address, ';
94 $sSQL .= ' rank_search, ';
95 $sSQL .= ' get_searchrank_label(rank_search) AS rank_search_label,';
96 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
97 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
98 //$sSQL .= " ST_Area(geometry::geography) AS area, ";
99 $sSQL .= ' ST_y(centroid) AS lat, ';
100 $sSQL .= ' ST_x(centroid) AS lon,';
102 $sSQL .= ' WHEN importance = 0 OR importance IS NULL THEN 0.75-(rank_search::float/40) ';
103 $sSQL .= ' ELSE importance ';
104 $sSQL .= ' END as calculated_importance, ';
105 $sSQL .= ' ST_AsGeoJSON(CASE ';
106 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
107 $sSQL .= ' ELSE geometry ';
108 $sSQL .= ' END) as asgeojson';
109 $sSQL .= ' FROM placex ';
110 $sSQL .= " WHERE place_id = $iPlaceID";
112 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
114 if (!$aPointDetails) {
115 userError('Unknown place id.');
118 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
120 $aClassType = getClassTypesWithImportance();
122 $sPointClassType = $aPointDetails['class'].':'.$aPointDetails['type'];
123 if (isset($aClassType[$sPointClassType]) && $aClassType[$sPointClassType]['icon']) {
124 $aPointDetails['icon'] = $aClassType[$sPointClassType]['icon'];
126 $aPointDetails['icon'] = false;
129 // Get all alternative names (languages, etc)
130 $sSQL = "SELECT (each(name)).key,(each(name)).value FROM placex WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
131 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
132 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
133 $aPointDetails['aNames'] = [];
137 $sSQL = "SELECT (each(address)).key as key,(each(address)).value FROM placex WHERE place_id = $iPlaceID ORDER BY key";
138 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
139 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
140 $aPointDetails['aAddressTags'] = [];
144 $sSQL = "SELECT (each(extratags)).key,(each(extratags)).value FROM placex WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
145 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
146 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
147 $aPointDetails['aExtraTags'] = [];
151 $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], -1, true);
154 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
155 $sSQL .= ' admin_level, rank_address, ';
156 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
157 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
158 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
159 $sSQL .= ' length(name::text) AS namelength ';
161 $sSQL .= ' placex, ';
163 $sSQL .= ' SELECT centroid AS placegeometry ';
164 $sSQL .= ' FROM placex ';
165 $sSQL .= " WHERE place_id = $iPlaceID ";
167 $sSQL .= " WHERE linked_place_id = $iPlaceID";
168 $sSQL .= ' ORDER BY ';
169 $sSQL .= ' rank_address ASC, ';
170 $sSQL .= ' rank_search ASC, ';
171 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
172 $sSQL .= ' housenumber';
173 $aLinkedLines = $oDB->getAll($sSQL);
174 if (PEAR::isError($aLinkedLines)) { // possible timeout
178 // All places this is an imediate parent of
179 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
180 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
181 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
182 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
183 $sSQL .= ' length(name::text) AS namelength ';
186 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
187 $sSQL .= ' FROM placex ';
188 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
189 $sSQL .= ' ORDER BY ';
190 $sSQL .= ' rank_address ASC, ';
191 $sSQL .= ' rank_search ASC ';
192 $sSQL .= ' LIMIT 500 ';
193 $sSQL .= ' ) AS obj,';
195 $sSQL .= ' SELECT centroid AS placegeometry ';
196 $sSQL .= ' FROM placex ';
197 $sSQL .= " WHERE place_id = $iPlaceID ";
199 $sSQL .= ' ORDER BY ';
200 $sSQL .= ' rank_address ASC, ';
201 $sSQL .= ' rank_search ASC, ';
202 $sSQL .= ' localname, ';
203 $sSQL .= ' housenumber';
204 $aParentOfLines = $oDB->getAll($sSQL);
205 if (PEAR::isError($aParentOfLines)) { // possible timeout
206 $aParentOfLines = [];
209 $aPlaceSearchNameKeywords = false;
210 $aPlaceSearchAddressKeywords = false;
211 if ($oParams->getBool('keywords')) {
212 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
213 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
214 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
215 $aPlaceSearchName = [];
218 if (!empty($aPlaceSearchName)) {
219 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
220 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
221 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
222 $aPlaceSearchNameKeywords = [];
225 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
226 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
227 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
228 $aPlaceSearchAddressKeywords = [];
233 logEnd($oDB, $hLog, 1);
235 if ($sOutputFormat=='html') {
236 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
237 $sDataDate = chksql($oDB->getOne($sSQL));
238 $sTileURL = CONST_Map_Tile_URL;
239 $sTileAttribution = CONST_Map_Tile_Attribution;
242 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');