]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/details.php
55a088d1994ab8ee9555211c4d9927d78c910d6b
[nominatim.git] / lib-php / website / details.php
1 <?php
2
3 require_once(CONST_LibDir.'/init-website.php');
4 require_once(CONST_LibDir.'/log.php');
5 require_once(CONST_LibDir.'/output.php');
6 require_once(CONST_LibDir.'/AddressDetails.php');
7 ini_set('memory_limit', '200M');
8
9 $oParams = new Nominatim\ParameterParser();
10
11 $sOutputFormat = $oParams->getSet('format', array('json'), 'json');
12 set_exception_handler_by_format($sOutputFormat);
13
14 $aLangPrefOrder = $oParams->getPreferredLanguages();
15
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');
20
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);
27
28 $oDB = new Nominatim\DB(CONST_Database_DSN);
29 $oDB->connect();
30
31 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
32
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
37     if ($sClass) {
38         $sSQL .= ' AND class= :class';
39         $aSQLParams[':class'] = $sClass;
40     }
41     $sSQL .= ' ORDER BY class ASC';
42     $sPlaceId = $oDB->getOne($sSQL, $aSQLParams);
43
44
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));
52     }
53
54     // Be nice about our error messages for broken geometry
55
56     if (!$sPlaceId && $oDB->tableExists('import_polygon_error')) {
57         $sSQL = 'SELECT ';
58         $sSQL .= '    osm_type, ';
59         $sSQL .= '    osm_id, ';
60         $sSQL .= '    errormessage, ';
61         $sSQL .= '    class, ';
62         $sSQL .= '    type, ';
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';
70         $sSQL .= ' LIMIT 1';
71         $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
72         if ($aPointDetails) {
73             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
74                 $aPointDetails['error_x'] = $aMatches[1];
75                 $aPointDetails['error_y'] = $aMatches[2];
76             } else {
77                 $aPointDetails['error_x'] = 0;
78                 $aPointDetails['error_y'] = 0;
79             }
80             include(CONST_LibDir.'/template/details-error-'.$sOutputFormat.'.php');
81             exit;
82         }
83     }
84
85     if ($sPlaceId === false) {
86         throw new Exception('No place with that OSM ID found.', 404);
87     }
88 } else {
89     if ($sPlaceId === false) {
90         userError('Required parameters missing. Need either osmtype/osmid or place_id.');
91     }
92 }
93
94 $iPlaceID = (int)$sPlaceId;
95
96 if (CONST_Use_US_Tiger_Data) {
97     $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
98     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
99 }
100
101 // interpolated house numbers
102 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
103 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
104
105 // artificial postcodes
106 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
107 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
108
109 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
110
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, ';
123 $sSQL .= '    CASE ';
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';
134 } else {
135     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
136 }
137 $sSQL .= ' FROM placex ';
138 $sSQL .= " WHERE place_id = $iPlaceID";
139
140 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
141
142 if (!$aPointDetails) {
143     throw new Exception('No place with that place ID found.', 404);
144 }
145
146 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
147
148 // Get all alternative names (languages, etc)
149 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
150 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
151 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
152
153 // Address tags
154 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
155 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
156 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
157
158 // Extra tags
159 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
160 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
161 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
162
163 // Address
164 $aAddressLines = false;
165 if ($bIncludeAddressDetails) {
166     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
167     $aAddressLines = $oDetails->getAddressDetails(true);
168 }
169
170 // Linked places
171 $aLinkedLines = false;
172 if ($bIncludeLinkedPlaces) {
173     $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
174     $sSQL .= ' admin_level, rank_address, ';
175     $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
176     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
177     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
178     $sSQL .= ' length(name::text) AS namelength ';
179     $sSQL .= ' FROM ';
180     $sSQL .= '    placex, ';
181     $sSQL .= '    ( ';
182     $sSQL .= '      SELECT centroid AS placegeometry ';
183     $sSQL .= '      FROM placex ';
184     $sSQL .= "      WHERE place_id = $iPlaceID ";
185     $sSQL .= '    ) AS x';
186     $sSQL .= " WHERE linked_place_id = $iPlaceID";
187     $sSQL .= ' ORDER BY ';
188     $sSQL .= '   rank_address ASC, ';
189     $sSQL .= '   rank_search ASC, ';
190     $sSQL .= "   get_name_by_language(name, $sLanguagePrefArraySQL), ";
191     $sSQL .= '   housenumber';
192     $aLinkedLines = $oDB->getAll($sSQL);
193 }
194
195 // All places this is an imediate parent of
196 $aHierarchyLines = false;
197 if ($bIncludeHierarchy) {
198     $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
199     $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
200     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
201     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
202     $sSQL .= ' length(name::text) AS namelength ';
203     $sSQL .= ' FROM ';
204     $sSQL .= '    ( ';
205     $sSQL .= '      SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
206     $sSQL .= '      FROM placex ';
207     $sSQL .= "      WHERE parent_place_id = $iPlaceID ";
208     $sSQL .= '      ORDER BY ';
209     $sSQL .= '         rank_address ASC, ';
210     $sSQL .= '         rank_search ASC ';
211     $sSQL .= '      LIMIT 500 ';
212     $sSQL .= '    ) AS obj,';
213     $sSQL .= '    ( ';
214     $sSQL .= '      SELECT centroid AS placegeometry ';
215     $sSQL .= '      FROM placex ';
216     $sSQL .= "      WHERE place_id = $iPlaceID ";
217     $sSQL .= '    ) AS x';
218     $sSQL .= ' ORDER BY ';
219     $sSQL .= '    rank_address ASC, ';
220     $sSQL .= '    rank_search ASC, ';
221     $sSQL .= '    localname, ';
222     $sSQL .= '    housenumber';
223     $aHierarchyLines = $oDB->getAll($sSQL);
224 }
225
226 $aPlaceSearchNameKeywords = false;
227 $aPlaceSearchAddressKeywords = false;
228 if ($bIncludeKeywords) {
229     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
230     $aPlaceSearchName = $oDB->getRow($sSQL);
231
232     if (!empty($aPlaceSearchName)) {
233         $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
234         if (!empty($sWordIds)) {
235             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
236             $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
237         }
238
239         $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
240         if (!empty($sWordIds)) {
241             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
242             $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
243         }
244     }
245 }
246
247 logEnd($oDB, $hLog, 1);
248
249 include(CONST_LibDir.'/template/details-'.$sOutputFormat.'.php');