3 require_once(CONST_BasePath.'/lib/init-website.php');
4 require_once(CONST_BasePath.'/lib/log.php');
5 require_once(CONST_BasePath.'/lib/output.php');
6 require_once(CONST_BasePath.'/lib/AddressDetails.php');
7 ini_set('memory_limit', '200M');
9 $oParams = new Nominatim\ParameterParser();
11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
12 set_exception_handler_by_format($sOutputFormat);
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_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
110 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
111 $sSQL .= ' ST_y(centroid) AS lat, ';
112 $sSQL .= ' ST_x(centroid) AS lon, ';
114 $sSQL .= ' WHEN importance = 0 OR importance IS NULL ';
115 $sSQL .= ' THEN 0.75-(rank_search::float/40) ';
116 $sSQL .= ' ELSE importance ';
117 $sSQL .= ' END as calculated_importance, ';
118 if ($bIncludePolygonAsGeoJSON) {
119 $sSQL .= ' ST_AsGeoJSON(CASE ';
120 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 ';
121 $sSQL .= ' THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
122 $sSQL .= ' ELSE geometry ';
123 $sSQL .= ' END) as asgeojson';
125 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
127 $sSQL .= ' FROM placex ';
128 $sSQL .= " WHERE place_id = $iPlaceID";
130 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
132 if (!$aPointDetails) {
133 userError('Unknown place id.');
136 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
137 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
138 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
140 // Get all alternative names (languages, etc)
141 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
142 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
143 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
144 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
145 $aPointDetails['aNames'] = array();
149 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
150 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
151 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
152 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
153 $aPointDetails['aAddressTags'] = array();
157 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
158 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
159 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
160 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
161 $aPointDetails['aExtraTags'] = array();
165 $aAddressLines = false;
166 if ($bIncludeAddressDetails) {
167 $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
168 $aAddressLines = $oDetails->getAddressDetails(true);
172 $aLinkedLines = false;
173 if ($bIncludeLinkedPlaces) {
174 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
175 $sSQL .= ' admin_level, rank_address, ';
176 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
177 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
178 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
179 $sSQL .= ' length(name::text) AS namelength ';
181 $sSQL .= ' placex, ';
183 $sSQL .= ' SELECT centroid AS placegeometry ';
184 $sSQL .= ' FROM placex ';
185 $sSQL .= " WHERE place_id = $iPlaceID ";
187 $sSQL .= " WHERE linked_place_id = $iPlaceID";
188 $sSQL .= ' ORDER BY ';
189 $sSQL .= ' rank_address ASC, ';
190 $sSQL .= ' rank_search ASC, ';
191 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
192 $sSQL .= ' housenumber';
193 $aLinkedLines = $oDB->getAll($sSQL);
194 if (PEAR::isError($aLinkedLines)) { // possible timeout
195 $aLinkedLines = array();
199 // All places this is an imediate parent of
200 $aHierarchyLines = false;
201 if ($bIncludeHierarchy) {
202 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
203 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
204 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
205 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
206 $sSQL .= ' length(name::text) AS namelength ';
209 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
210 $sSQL .= ' FROM placex ';
211 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
212 $sSQL .= ' ORDER BY ';
213 $sSQL .= ' rank_address ASC, ';
214 $sSQL .= ' rank_search ASC ';
215 $sSQL .= ' LIMIT 500 ';
216 $sSQL .= ' ) AS obj,';
218 $sSQL .= ' SELECT centroid AS placegeometry ';
219 $sSQL .= ' FROM placex ';
220 $sSQL .= " WHERE place_id = $iPlaceID ";
222 $sSQL .= ' ORDER BY ';
223 $sSQL .= ' rank_address ASC, ';
224 $sSQL .= ' rank_search ASC, ';
225 $sSQL .= ' localname, ';
226 $sSQL .= ' housenumber';
227 $aHierarchyLines = $oDB->getAll($sSQL);
228 if (PEAR::isError($aHierarchyLines)) { // possible timeout
229 $aHierarchyLines = array();
233 $aPlaceSearchNameKeywords = false;
234 $aPlaceSearchAddressKeywords = false;
235 if ($bIncludeKeywords) {
236 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
237 $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
238 if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
239 $aPlaceSearchName = array();
242 if (!empty($aPlaceSearchName)) {
243 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
244 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
245 if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
246 $aPlaceSearchNameKeywords = array();
249 $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
250 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
251 if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
252 $aPlaceSearchAddressKeywords = array();
257 logEnd($oDB, $hLog, 1);
259 if ($sOutputFormat=='html') {
260 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
261 $sDataDate = chksql($oDB->getOne($sSQL));
262 $sTileURL = CONST_Map_Tile_URL;
263 $sTileAttribution = CONST_Map_Tile_Attribution;
266 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');