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