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