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('json'), 'json');
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', false);
23 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
24 $bIncludeHierarchy = $oParams->getBool('hierarchy', false);
25 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
26 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', false);
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 $aSQLParams = array(':type' => $sOsmType, ':id' => $iOsmId);
36 // osm_type and osm_id are not unique enough
38 $sSQL .= ' AND class= :class';
39 $aSQLParams[':class'] = $sClass;
41 $sSQL .= ' ORDER BY class ASC';
42 $sPlaceId = $oDB->getOne($sSQL, $aSQLParams);
45 // Nothing? Maybe it's an interpolation.
46 // XXX Simply returns the first parent street it finds. It should
47 // get a house number and get the right interpolation.
48 if (!$sPlaceId && $sOsmType == 'W' && (!$sClass || $sClass == 'place')) {
49 $sSQL = 'SELECT place_id FROM location_property_osmline'
50 .' WHERE osm_id = :id LIMIT 1';
51 $sPlaceId = $oDB->getOne($sSQL, array(':id' => $iOsmId));
54 // Be nice about our error messages for broken geometry
58 $sSQL .= ' osm_type, ';
60 $sSQL .= ' errormessage, ';
63 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
64 $sSQL .= ' ST_AsText(prevgeometry) AS prevgeom, ';
65 $sSQL .= ' ST_AsText(newgeometry) AS newgeom';
66 $sSQL .= ' FROM import_polygon_error ';
67 $sSQL .= ' WHERE osm_type = :type';
68 $sSQL .= ' AND osm_id = :id';
69 $sSQL .= ' ORDER BY updated DESC';
71 $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
73 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
74 $aPointDetails['error_x'] = $aMatches[1];
75 $aPointDetails['error_y'] = $aMatches[2];
77 $aPointDetails['error_x'] = 0;
78 $aPointDetails['error_y'] = 0;
80 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
87 if ($sPlaceId === false) userError('Please select a place id');
89 $iPlaceID = (int)$sPlaceId;
91 if (CONST_Use_US_Tiger_Data) {
92 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
93 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
96 // interpolated house numbers
97 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
98 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
100 // artificial postcodes
101 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
102 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
104 if (CONST_Use_Aux_Location_data) {
105 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID);
106 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
109 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
111 // Get the details for this point
112 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
113 $sSQL .= ' housenumber, postcode, country_code,';
114 $sSQL .= ' importance, wikipedia,';
115 $sSQL .= ' ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
116 $sSQL .= ' parent_place_id, ';
117 $sSQL .= ' rank_address, ';
118 $sSQL .= ' rank_search, ';
119 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
120 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
121 $sSQL .= ' ST_y(centroid) AS lat, ';
122 $sSQL .= ' ST_x(centroid) AS lon, ';
124 $sSQL .= ' WHEN importance = 0 OR importance IS NULL ';
125 $sSQL .= ' THEN 0.75-(rank_search::float/40) ';
126 $sSQL .= ' ELSE importance ';
127 $sSQL .= ' END as calculated_importance, ';
128 if ($bIncludePolygonAsGeoJSON) {
129 $sSQL .= ' ST_AsGeoJSON(CASE ';
130 $sSQL .= ' WHEN ST_NPoints(geometry) > 5000 ';
131 $sSQL .= ' THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
132 $sSQL .= ' ELSE geometry ';
133 $sSQL .= ' END) as asgeojson';
135 $sSQL .= ' ST_AsGeoJSON(centroid) as asgeojson';
137 $sSQL .= ' FROM placex ';
138 $sSQL .= " WHERE place_id = $iPlaceID";
140 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
142 if (!$aPointDetails) {
143 userError('Unknown place id.');
146 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
147 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
149 // Get all alternative names (languages, etc)
150 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
151 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
152 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
155 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
156 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
157 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
160 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
161 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
162 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
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);
196 // All places this is an imediate parent of
197 $aHierarchyLines = false;
198 if ($bIncludeHierarchy) {
199 $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
200 $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
201 $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
202 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
203 $sSQL .= ' length(name::text) AS namelength ';
206 $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
207 $sSQL .= ' FROM placex ';
208 $sSQL .= " WHERE parent_place_id = $iPlaceID ";
209 $sSQL .= ' ORDER BY ';
210 $sSQL .= ' rank_address ASC, ';
211 $sSQL .= ' rank_search ASC ';
212 $sSQL .= ' LIMIT 500 ';
213 $sSQL .= ' ) AS obj,';
215 $sSQL .= ' SELECT centroid AS placegeometry ';
216 $sSQL .= ' FROM placex ';
217 $sSQL .= " WHERE place_id = $iPlaceID ";
219 $sSQL .= ' ORDER BY ';
220 $sSQL .= ' rank_address ASC, ';
221 $sSQL .= ' rank_search ASC, ';
222 $sSQL .= ' localname, ';
223 $sSQL .= ' housenumber';
224 $aHierarchyLines = $oDB->getAll($sSQL);
227 $aPlaceSearchNameKeywords = false;
228 $aPlaceSearchAddressKeywords = false;
229 if ($bIncludeKeywords) {
230 $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
231 $aPlaceSearchName = $oDB->getRow($sSQL);
233 if (!empty($aPlaceSearchName)) {
234 $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
235 if (!empty($sWordIds)) {
236 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
237 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
240 $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
241 if (!empty($sWordIds)) {
242 $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
243 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
248 logEnd($oDB, $hLog, 1);
250 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');