]> git.openstreetmap.org Git - nominatim.git/blob - website/details.php
set exception handler by request format, not always HTML
[nominatim.git] / website / details.php
1 <?php
2 @define('CONST_ConnectionBucket_PageType', 'Details');
3
4 require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5 require_once(CONST_BasePath.'/lib/init-website.php');
6 require_once(CONST_BasePath.'/lib/log.php');
7 require_once(CONST_BasePath.'/lib/output.php');
8 require_once(CONST_BasePath.'/lib/AddressDetails.php');
9 ini_set('memory_limit', '200M');
10
11 $oParams = new Nominatim\ParameterParser();
12
13 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
14 set_exception_handler_by_format($sOutputFormat);
15
16 $aLangPrefOrder = $oParams->getPreferredLanguages();
17 $sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
18
19 $sPlaceId = $oParams->getString('place_id');
20 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
21 $iOsmId = $oParams->getInt('osmid', -1);
22 $sClass = $oParams->getString('class');
23
24 $bIncludeKeywords = $oParams->getBool('keywords', false);
25 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
26 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
27 $bIncludeHierarchy = $oParams->getBool('hierarchy', $sOutputFormat == 'html');
28 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
29 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', $sOutputFormat == 'html');
30
31 $oDB =& getDB();
32
33 if ($sOsmType && $iOsmId > 0) {
34     $sSQL = sprintf(
35         "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
36         $sOsmType,
37         $iOsmId
38     );
39     // osm_type and osm_id are not unique enough
40     if ($sClass) {
41         $sSQL .= " AND class='".$sClass."'";
42     }
43     $sSQL .= ' ORDER BY class ASC';
44     $sPlaceId = chksql($oDB->getOne($sSQL));
45
46     // Be nice about our error messages for broken geometry
47
48     if (!$sPlaceId) {
49         $sSQL = 'SELECT ';
50         $sSQL .= '    osm_type, ';
51         $sSQL .= '    osm_id, ';
52         $sSQL .= '    errormessage, ';
53         $sSQL .= '    class, ';
54         $sSQL .= '    type, ';
55         $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
56         $sSQL .= '    ST_AsText(prevgeometry) AS prevgeom, ';
57         $sSQL .= '    ST_AsText(newgeometry) AS newgeom';
58         $sSQL .= ' FROM import_polygon_error ';
59         $sSQL .= " WHERE osm_type = '".$sOsmType."'";
60         $sSQL .= '  AND osm_id = '.$iOsmId;
61         $sSQL .= ' ORDER BY updated DESC';
62         $sSQL .= ' LIMIT 1';
63         $aPointDetails = chksql($oDB->getRow($sSQL));
64         if (!PEAR::isError($aPointDetails) && $aPointDetails) {
65             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
66                 $aPointDetails['error_x'] = $aMatches[1];
67                 $aPointDetails['error_y'] = $aMatches[2];
68             } else {
69                 $aPointDetails['error_x'] = 0;
70                 $aPointDetails['error_y'] = 0;
71             }
72             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
73             exit;
74         }
75     }
76 }
77
78
79 if (!$sPlaceId) userError('Please select a place id');
80
81 $iPlaceID = (int)$sPlaceId;
82
83 if (CONST_Use_US_Tiger_Data) {
84     $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
85     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
86 }
87
88 // interpolated house numbers
89 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
90 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
91
92 // artificial postcodes
93 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
94 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
95
96 if (CONST_Use_Aux_Location_data) {
97     $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
98     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
99 }
100
101 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
102
103 // Get the details for this point
104 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
105 $sSQL .= '    housenumber, postcode, country_code,';
106 $sSQL .= '    importance, wikipedia,';
107 $sSQL .= '    ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
108 $sSQL .= '    parent_place_id, ';
109 $sSQL .= '    rank_address, ';
110 $sSQL .= '    rank_search, ';
111 $sSQL .= '    get_searchrank_label(rank_search) AS rank_search_label,'; // only used in HTML output
112 $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
113 $sSQL .= "    ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
114 $sSQL .= '    ST_y(centroid) AS lat, ';
115 $sSQL .= '    ST_x(centroid) AS lon, ';
116 $sSQL .= '    CASE ';
117 $sSQL .= '       WHEN importance = 0 OR importance IS NULL ';
118 $sSQL .= '       THEN 0.75-(rank_search::float/40) ';
119 $sSQL .= '       ELSE importance ';
120 $sSQL .= '       END as calculated_importance, ';
121 if ($bIncludePolygonAsGeoJSON) {
122     $sSQL .= '    ST_AsGeoJSON(CASE ';
123     $sSQL .= '                WHEN ST_NPoints(geometry) > 5000 ';
124     $sSQL .= '                THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
125     $sSQL .= '                ELSE geometry ';
126     $sSQL .= '                END) as asgeojson';
127 } else {
128     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
129 }
130 $sSQL .= ' FROM placex ';
131 $sSQL .= " WHERE place_id = $iPlaceID";
132
133 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
134
135 if (!$aPointDetails) {
136     userError('Unknown place id.');
137 }
138
139 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
140 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
141
142 // Get all alternative names (languages, etc)
143 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
144 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
145 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
146 if (PEAR::isError($aPointDetails['aNames'])) { // possible timeout
147     $aPointDetails['aNames'] = array();
148 }
149
150 // Address tags
151 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
152 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
153 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
154 if (PEAR::isError($aPointDetails['aAddressTags'])) { // possible timeout
155     $aPointDetails['aAddressTags'] = array();
156 }
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 if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
163     $aPointDetails['aExtraTags'] = array();
164 }
165
166 // Address
167 $aAddressLines = false;
168 if ($bIncludeAddressDetails) {
169     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
170     $aAddressLines = $oDetails->getAddressDetails(true);
171 }
172
173 // Linked places
174 $aLinkedLines = false;
175 if ($bIncludeLinkedPlaces) {
176     $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
177     $sSQL .= ' admin_level, rank_address, ';
178     $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
179     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
180     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
181     $sSQL .= ' length(name::text) AS namelength ';
182     $sSQL .= ' FROM ';
183     $sSQL .= '    placex, ';
184     $sSQL .= '    ( ';
185     $sSQL .= '      SELECT centroid AS placegeometry ';
186     $sSQL .= '      FROM placex ';
187     $sSQL .= "      WHERE place_id = $iPlaceID ";
188     $sSQL .= '    ) AS x';
189     $sSQL .= " WHERE linked_place_id = $iPlaceID";
190     $sSQL .= ' ORDER BY ';
191     $sSQL .= '   rank_address ASC, ';
192     $sSQL .= '   rank_search ASC, ';
193     $sSQL .= "   get_name_by_language(name, $sLanguagePrefArraySQL), ";
194     $sSQL .= '   housenumber';
195     $aLinkedLines = $oDB->getAll($sSQL);
196     if (PEAR::isError($aLinkedLines)) { // possible timeout
197         $aLinkedLines = array();
198     }
199 }
200
201 // All places this is an imediate parent of
202 $aHierarchyLines = false;
203 if ($bIncludeHierarchy) {
204     $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
205     $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
206     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
207     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
208     $sSQL .= ' length(name::text) AS namelength ';
209     $sSQL .= ' FROM ';
210     $sSQL .= '    ( ';
211     $sSQL .= '      SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
212     $sSQL .= '      FROM placex ';
213     $sSQL .= "      WHERE parent_place_id = $iPlaceID ";
214     $sSQL .= '      ORDER BY ';
215     $sSQL .= '         rank_address ASC, ';
216     $sSQL .= '         rank_search ASC ';
217     $sSQL .= '      LIMIT 500 ';
218     $sSQL .= '    ) AS obj,';
219     $sSQL .= '    ( ';
220     $sSQL .= '      SELECT centroid AS placegeometry ';
221     $sSQL .= '      FROM placex ';
222     $sSQL .= "      WHERE place_id = $iPlaceID ";
223     $sSQL .= '    ) AS x';
224     $sSQL .= ' ORDER BY ';
225     $sSQL .= '    rank_address ASC, ';
226     $sSQL .= '    rank_search ASC, ';
227     $sSQL .= '    localname, ';
228     $sSQL .= '    housenumber';
229     $aHierarchyLines = $oDB->getAll($sSQL);
230     if (PEAR::isError($aHierarchyLines)) { // possible timeout
231         $aHierarchyLines = array();
232     }
233 }
234
235 $aPlaceSearchNameKeywords = false;
236 $aPlaceSearchAddressKeywords = false;
237 if ($bIncludeKeywords) {
238     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
239     $aPlaceSearchName = $oDB->getRow($sSQL); // can be null
240     if (!$aPlaceSearchName || PEAR::isError($aPlaceSearchName)) { // possible timeout
241         $aPlaceSearchName = array();
242     }
243
244     if (!empty($aPlaceSearchName)) {
245         $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
246         $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
247         if (PEAR::isError($aPlaceSearchNameKeywords)) { // possible timeout
248             $aPlaceSearchNameKeywords = array();
249         }
250
251         $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
252         $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
253         if (PEAR::isError($aPlaceSearchAddressKeywords)) { // possible timeout
254             $aPlaceSearchAddressKeywords = array();
255         }
256     }
257 }
258
259 logEnd($oDB, $hLog, 1);
260
261 if ($sOutputFormat=='html') {
262     $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
263     $sDataDate = chksql($oDB->getOne($sSQL));
264     $sTileURL = CONST_Map_Tile_URL;
265     $sTileAttribution = CONST_Map_Tile_Attribution;
266 }
267
268 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');