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