]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
second argument of array_merge can be empty
[nominatim.git] / website / hierarchy.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Details');
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5         require_once(CONST_BasePath.'/lib/log.php');
6         require_once(CONST_BasePath.'/lib/PlaceLookup.php');
7
8         $sOutputFormat = 'html';
9         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
10         {
11                 $sOutputFormat = $_GET['format'];
12         }
13
14         ini_set('memory_limit', '200M');
15
16         $oDB =& getDB();
17
18         $aLangPrefOrder = getPreferredLanguages();
19         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
20
21         if (isset($_GET['osmtype']) && isset($_GET['osmid']) && (int)$_GET['osmid'] && ($_GET['osmtype'] == 'N' || $_GET['osmtype'] == 'W' || $_GET['osmtype'] == 'R'))
22         {
23                 $_GET['place_id'] = $oDB->getOne("select place_id from placex where osm_type = '".$_GET['osmtype']."' and osm_id = ".(int)$_GET['osmid']." order by type = 'postcode' asc");
24
25                 // Be nice about our error messages for broken geometry
26                 if (!$_GET['place_id'])
27                 {
28                         $aPointDetails = $oDB->getRow("select osm_type, osm_id, errormessage, class, type, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom from import_polygon_error where osm_type = '".$_GET['osmtype']."' and osm_id = ".(int)$_GET['osmid']." order by updated desc limit 1");
29                         if (!PEAR::isError($aPointDetails) && $aPointDetails) {
30                                 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
31                                 {
32                                         $aPointDetails['error_x'] = $aMatches[1];
33                                         $aPointDetails['error_y'] = $aMatches[2];
34                                 }
35                                 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
36                                 exit;
37                         }
38                 }
39         }
40
41         if (!isset($_GET['place_id']))
42         {
43                 echo "Please select a place id";
44                 exit;
45         }
46
47         $iPlaceID = (int)$_GET['place_id'];
48
49         $sAuxHouseNumber = false;
50         $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
51         if ($iParentPlaceID)
52         {
53                 $iPlaceID = $iParentPlaceID;
54         }
55         else
56         {
57                 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
58                 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
59         }
60
61         $oPlaceLookup = new PlaceLookup($oDB);
62         $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
63         $oPlaceLookup->setIncludeAddressDetails(true);
64         $oPlaceLookup->setPlaceId($iPlaceID);
65
66         $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails());
67
68         if (!sizeof($aPlaceAddress))
69         {
70                 echo "Unknown place id.";
71                 exit;
72         }
73
74         $aBreadcrums = array();
75         foreach($aPlaceAddress as $i => $aPlace)
76         {
77                 if (!$aPlace['place_id']) continue;
78                 $aBreadcrums[] = array('placeId'=>$aPlace['place_id'], 'osmType'=>$aPlace['osm_type'], 'osmId'=>$aPlace['osm_id'], 'localName'=>$aPlace['localname']);
79                 $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
80                 $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
81                                 $sOSMUrl = 'http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aPlace['osm_id'];
82                 if ($sOutputFormat == 'html') if ($i) echo " > ";
83                 if ($sOutputFormat == 'html') echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> (<a href="'.$sOSMUrl.'">osm</a>)';
84         }
85
86         $aDetails = array();
87         $aDetails['breadcrumbs'] = $aBreadcrums;
88
89         if ($sOutputFormat == 'json')
90         {
91                 header("content-type: application/json; charset=UTF-8");
92                 javascript_renderData($aDetails);
93                 exit;
94         }
95
96         $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
97
98         $sSQL = "select obj.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea,  st_area(geometry) as area, ";
99         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
100         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
101         $sSQL .= " where parent_place_id in (".join(',',$aRelatedPlaceIDs).") and name is not null order by rank_address asc,rank_search asc limit 500) as obj";
102         $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
103         $aParentOfLines = $oDB->getAll($sSQL);
104
105         if (sizeof($aParentOfLines))
106         {
107                 echo '<h2>Parent Of:</h2>';
108                 $aClassType = getClassTypesWithImportance();
109                 $aGroupedAddressLines = array();
110                 foreach($aParentOfLines as $aAddressLine)
111                 {
112                         if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
113                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
114                         {
115                                 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
116                         }
117                         elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
118                                 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
119                         {
120                                 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
121                         }
122                         else $aAddressLine['label'] = ucwords($aAddressLine['type']);
123
124                         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
125                                 $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
126                         }
127                         foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
128                         {
129                                 echo "<h3>$sGroupHeading</h3>";
130                                 foreach($aParentOfLines as $aAddressLine)
131                                 {
132                                         $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
133                                         $sOSMType = ($aAddressLine['osm_type'] == 'N'?'node':($aAddressLine['osm_type'] == 'W'?'way':($aAddressLine['osm_type'] == 'R'?'relation':'')));
134
135                                         echo '<div class="line">';
136                                         echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
137                                         echo ' (';
138                                         echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
139                                         if ($sOSMType) echo ', <span class="osm"><span class="label"></span>'.$sOSMType.' <a href="http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aAddressLine['osm_id'].'">'.$aAddressLine['osm_id'].'</a></span>';
140                                         echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
141                                         echo ', '.$aAddressLine['area'];
142                                         echo ')';
143                                         echo '</div>';
144                                 }
145                         }
146                         if (sizeof($aParentOfLines) >= 500) {
147                                 echo '<p>There are more child objects which are not shown.</p>';
148                         }
149                         echo '</div>';
150                 }
151 exit;
152
153
154         $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
155
156         // Make sure the point we are reporting on is fully indexed
157         //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
158         //$oDB->query($sSQL);
159
160         // Get the details for this point
161         $sSQL = "select place_id, osm_type, osm_id, class, type, name, admin_level, housenumber, street, isin, postcode, calculated_country_code as country_code, importance, wikipedia,";
162         $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') as indexed_date, parent_place_id, rank_address, rank_search, get_searchrank_label(rank_search) as rank_search_label, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ";
163         $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, ";
164         //$sSQL .= " ST_Area(geometry::geography) as area, ";
165         $sSQL .= " ST_y(centroid) as lat, ST_x(centroid) as lon,";
166         $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
167         $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
168         $sSQL .= " from placex where place_id = $iPlaceID";
169         $aPointDetails = $oDB->getRow($sSQL);
170         if (PEAR::IsError($aPointDetails))
171         {
172                 failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
173         }
174         $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
175
176         $aClassType = getClassTypesWithImportance();
177         $aPointDetails['icon'] = $aClassType[$aPointDetails['class'].':'.$aPointDetails['type']]['icon'];
178
179         // Get all alternative names (languages, etc)
180         $sSQL = "select (each(name)).key,(each(name)).value from placex where place_id = $iPlaceID order by (each(name)).key";
181         $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
182
183         // Extra tags
184         $sSQL = "select (each(extratags)).key,(each(extratags)).value from placex where place_id = $iPlaceID order by (each(extratags)).key";
185         $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
186
187         // Address
188         $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], true);
189
190         // Linked places
191         $sSQL = "select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, st_distance(geometry, placegeometry) as distance, ";
192         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
193         $sSQL .= " from placex, (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
194         $sSQL .= " where linked_place_id = $iPlaceID";
195         $sSQL .= " order by rank_address asc,rank_search asc,get_name_by_language(name,$sLanguagePrefArraySQL),housenumber";
196         $aLinkedLines = $oDB->getAll($sSQL);
197
198         // All places this is an imediate parent of
199         $sSQL = "select obj.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, st_distance(geometry, placegeometry) as distance, ";
200         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
201         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
202         $sSQL .= " where parent_place_id = $iPlaceID order by rank_address asc,rank_search asc limit 500) as obj,";
203         $sSQL .= " (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
204         $sSQL .= " order by rank_address asc,rank_search asc,localname,housenumber";
205         $aParentOfLines = $oDB->getAll($sSQL);
206
207         $aPlaceSearchNameKeywords = false;
208         $aPlaceSearchAddressKeywords = false;
209         if (isset($_GET['keywords']) && $_GET['keywords'])
210         {
211                 $sSQL = "select * from search_name where place_id = $iPlaceID";
212                 $aPlaceSearchName = $oDB->getRow($sSQL);
213                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['name_vector'],1,-1).")";
214                 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
215                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['nameaddress_vector'],1,-1).")";
216                 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
217         }
218
219         logEnd($oDB, $hLog, 1);
220
221         include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');