]> git.openstreetmap.org Git - nominatim.git/blob - website/details.php
README: tiny markdown syntax error
[nominatim.git] / website / details.php
1 <?php
2
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');
8
9 $oParams = new Nominatim\ParameterParser();
10
11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
12 set_exception_handler_by_format($sOutputFormat);
13
14 $aLangPrefOrder = $oParams->getPreferredLanguages();
15 $sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
16
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');
21
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');
28
29 $oDB =& getDB();
30
31 if ($sOsmType && $iOsmId > 0) {
32     $sSQL = sprintf(
33         "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
34         $sOsmType,
35         $iOsmId
36     );
37     // osm_type and osm_id are not unique enough
38     if ($sClass) {
39         $sSQL .= " AND class='".$sClass."'";
40     }
41     $sSQL .= ' ORDER BY class ASC';
42     $sPlaceId = chksql($oDB->getOne($sSQL));
43
44     // Be nice about our error messages for broken geometry
45
46     if (!$sPlaceId) {
47         $sSQL = 'SELECT ';
48         $sSQL .= '    osm_type, ';
49         $sSQL .= '    osm_id, ';
50         $sSQL .= '    errormessage, ';
51         $sSQL .= '    class, ';
52         $sSQL .= '    type, ';
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';
60         $sSQL .= ' LIMIT 1';
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];
66             } else {
67                 $aPointDetails['error_x'] = 0;
68                 $aPointDetails['error_y'] = 0;
69             }
70             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
71             exit;
72         }
73     }
74 }
75
76
77 if (!$sPlaceId) userError('Please select a place id');
78
79 $iPlaceID = (int)$sPlaceId;
80
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;
84 }
85
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;
89
90 // artificial postcodes
91 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
92 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
93
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;
97 }
98
99 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
100
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_searchrank_label(rank_search) AS rank_search_label,'; // only used in HTML output
110 $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
111 $sSQL .= "    ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
112 $sSQL .= '    ST_y(centroid) AS lat, ';
113 $sSQL .= '    ST_x(centroid) AS lon, ';
114 $sSQL .= '    CASE ';
115 $sSQL .= '       WHEN importance = 0 OR importance IS NULL ';
116 $sSQL .= '       THEN 0.75-(rank_search::float/40) ';
117 $sSQL .= '       ELSE importance ';
118 $sSQL .= '       END as calculated_importance, ';
119 if ($bIncludePolygonAsGeoJSON) {
120     $sSQL .= '    ST_AsGeoJSON(CASE ';
121     $sSQL .= '                WHEN ST_NPoints(geometry) > 5000 ';
122     $sSQL .= '                THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
123     $sSQL .= '                ELSE geometry ';
124     $sSQL .= '                END) as asgeojson';
125 } else {
126     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
127 }
128 $sSQL .= ' FROM placex ';
129 $sSQL .= " WHERE place_id = $iPlaceID";
130
131 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
132
133 if (!$aPointDetails) {
134     userError('Unknown place id.');
135 }
136
137 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
138 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
139
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();
146 }
147
148 // Address tags
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();
154 }
155
156 // Extra tags
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();
162 }
163
164 // Address
165 $aAddressLines = false;
166 if ($bIncludeAddressDetails) {
167     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
168     $aAddressLines = $oDetails->getAddressDetails(true);
169 }
170
171 // Linked places
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 ';
180     $sSQL .= ' FROM ';
181     $sSQL .= '    placex, ';
182     $sSQL .= '    ( ';
183     $sSQL .= '      SELECT centroid AS placegeometry ';
184     $sSQL .= '      FROM placex ';
185     $sSQL .= "      WHERE place_id = $iPlaceID ";
186     $sSQL .= '    ) AS x';
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();
196     }
197 }
198
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 ';
207     $sSQL .= ' FROM ';
208     $sSQL .= '    ( ';
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,';
217     $sSQL .= '    ( ';
218     $sSQL .= '      SELECT centroid AS placegeometry ';
219     $sSQL .= '      FROM placex ';
220     $sSQL .= "      WHERE place_id = $iPlaceID ";
221     $sSQL .= '    ) AS x';
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();
230     }
231 }
232
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();
240     }
241
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();
247         }
248
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();
253         }
254     }
255 }
256
257 logEnd($oDB, $hLog, 1);
258
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;
264 }
265
266 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');