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();
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');
21 $bIncludeKeywords = $oParams->getBool('keywords', false);
22 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
23 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
24 $bIncludeHierarchy = $oParams->getBool('hierarchy', $sOutputFormat == 'html');
25 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
26 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', $sOutputFormat == 'html');
28 $oDB = new Nominatim\DB();
31 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
33 if ($sOsmType && $iOsmId > 0) {
34 $sSQL = 'SELECT place_id FROM placex WHERE osm_type = :type AND osm_id = :id';
35 // osm_type and osm_id are not unique enough
37 $sSQL .= " AND class='".$sClass."'";
39 $sSQL .= ' ORDER BY class ASC';
40 $sPlaceId = $oDB->getOne($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
42 // Be nice about our error messages for broken geometry
46 $sSQL .= ' osm_type, ';
48 $sSQL .= ' errormessage, ';
51 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
52 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
53 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
54 $sSQL .= ' FROM import_polygon_error ';
55 $sSQL .= ' WHERE osm_type = :type';
56 $sSQL .= ' AND osm_id = :id';
57 $sSQL .= ' ORDER BY updated DESC';
59 $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
61 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
62 $aPointDetails['error_x'] = $aMatches[1];
63 $aPointDetails['error_y'] = $aMatches[2];
65 $aPointDetails['error_x'] = 0;
66 $aPointDetails['error_y'] = 0;
68 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
75 if ($sPlaceId === false) userError('Please select a place id');
77 $iPlaceID = (int)$sPlaceId;
79 if (CONST_Use_US_Tiger_Data) {
80 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
81 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
84 // interpolated house numbers
85 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
86 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
88 // artificial postcodes
89 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
90 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
92 if (CONST_Use_Aux_Location_data) {
93 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID);
94 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
97 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
99 // Get the details for this point
100 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
101 $sSQL .= ' housenumber, postcode, country_code,';
102 $sSQL .= ' importance, wikipedia,';
103 $sSQL .= ' ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
104 $sSQL .= ' parent_place_id, ';
105 $sSQL .= ' rank_address, ';
106 $sSQL .= ' rank_search, ';
107 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
108 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
109 $sSQL .= ' ST_y(centroid) AS lat, ';
110 $sSQL .= ' ST_x(centroid) AS lon, ';
112 $sSQL .= ' WHEN importance = 0 OR importance IS NULL ';
113 $sSQL .= ' THEN 0.75-(rank_search::float/40) ';
114 $sSQL .= ' ELSE importance ';
115 $sSQL .= ' END as calculated_importance, ';
116 if ($bIncludePolygonAsGeoJSON) {
117 $sSQL .= ' ST_AsGeoJSON(CASE ';
118 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 ';
119 $sSQL .= ' THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
120 $sSQL .= ' ELSE geometry ';
121 $sSQL .= ' END) as asgeojson';
123 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
125 $sSQL .= ' FROM placex ';
126 $sSQL .= " WHERE place_id = $iPlaceID";
128 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
130 if (!$aPointDetails) {
131 userError('Unknown place id.');
134 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
135 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
136 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
138 // Get all alternative names (languages, etc)
139 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
140 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
141 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
144 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
145 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
146 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
149 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
150 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
151 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
154 $aAddressLines = false;
155 if ($bIncludeAddressDetails) {
156 $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
157 $aAddressLines = $oDetails->getAddressDetails(true);
161 $aLinkedLines = false;
162 if ($bIncludeLinkedPlaces) {
163 $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
164 $sSQL .= ' admin_level, rank_address, ';
165 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
166 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
167 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
168 $sSQL .= ' length(name::text) AS namelength ';
170 $sSQL .= ' placex, ';
172 $sSQL .= ' SELECT centroid AS placegeometry ';
173 $sSQL .= ' FROM placex ';
174 $sSQL .= " WHERE place_id = $iPlaceID ";
176 $sSQL .= " WHERE linked_place_id = $iPlaceID";
177 $sSQL .= ' ORDER BY ';
178 $sSQL .= ' rank_address ASC, ';
179 $sSQL .= ' rank_search ASC, ';
180 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
181 $sSQL .= ' housenumber';
182 $aLinkedLines = $oDB->getAll($sSQL);
185 // All places this is an imediate parent of
186 $aHierarchyLines = false;
187 if ($bIncludeHierarchy) {
188 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
189 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
190 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
191 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
192 $sSQL .= ' length(name::text) AS namelength ';
195 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
196 $sSQL .= ' FROM placex ';
197 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
198 $sSQL .= ' ORDER BY ';
199 $sSQL .= ' rank_address ASC, ';
200 $sSQL .= ' rank_search ASC ';
201 $sSQL .= ' LIMIT 500 ';
202 $sSQL .= ' ) AS obj,';
204 $sSQL .= ' SELECT centroid AS placegeometry ';
205 $sSQL .= ' FROM placex ';
206 $sSQL .= " WHERE place_id = $iPlaceID ";
208 $sSQL .= ' ORDER BY ';
209 $sSQL .= ' rank_address ASC, ';
210 $sSQL .= ' rank_search ASC, ';
211 $sSQL .= ' localname, ';
212 $sSQL .= ' housenumber';
213 $aHierarchyLines = $oDB->getAll($sSQL);
216 $aPlaceSearchNameKeywords = false;
217 $aPlaceSearchAddressKeywords = false;
218 if ($bIncludeKeywords) {
219 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
220 $aPlaceSearchName = $oDB->getRow($sSQL);
222 if (!empty($aPlaceSearchName)) {
223 $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
224 if (!empty($sWordIds)) {
225 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
226 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
229 $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
230 if (!empty($sWordIds)) {
231 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
232 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
237 logEnd($oDB, $hLog, 1);
239 if ($sOutputFormat=='html') {
240 $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
241 $sDataDate = $oDB->getOne($sSQL);
242 $sTileURL = CONST_Map_Tile_URL;
243 $sTileAttribution = CONST_Map_Tile_Attribution;
246 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');