]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
apply rank restrictions already when scanning search_name table
[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         $aBreadcrums = array();
68         foreach($aPlaceAddress as $i => $aPlace)
69         {
70                 if (!$aPlace['place_id']) continue;
71                 $aBreadcrums[] = array('placeId'=>$aPlace['place_id'], 'osmType'=>$aPlace['osm_type'], 'osmId'=>$aPlace['osm_id'], 'localName'=>$aPlace['localname']);
72                 $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
73                 $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
74                 $sOSMUrl = 'http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aPlace['osm_id'];
75                 if ($sOutputFormat == 'html') if ($i) echo " > ";
76                 if ($sOutputFormat == 'html') echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> (<a href="'.$sOSMUrl.'">osm</a>)';
77         }
78
79         $aDetails = array();
80         $aDetails['breadcrumbs'] = $aBreadcrums;
81
82         if ($sOutputFormat == 'json')
83         {
84                 header("content-type: application/json; charset=UTF-8");
85                 javascript_renderData($aDetails);
86                 exit;
87         }
88
89         $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
90
91         $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, ";
92         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
93         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
94         $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";
95         $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
96         $aParentOfLines = $oDB->getAll($sSQL);
97
98         if (sizeof($aParentOfLines))
99         {
100                 echo '<h2>Parent Of:</h2>';
101                 $aClassType = getClassTypesWithImportance();
102                 $aGroupedAddressLines = array();
103                 foreach($aParentOfLines as $aAddressLine)
104                 {
105                         if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
106                                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
107                                 {
108                                         $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
109                                 }
110                         elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
111                                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
112                                 {
113                                         $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
114                                 }
115                         else $aAddressLine['label'] = ucwords($aAddressLine['type']);
116
117                         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
118                         $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
119                 }
120                 foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
121                 {
122                         echo "<h3>$sGroupHeading</h3>";
123                 foreach($aParentOfLines as $aAddressLine)
124                 {
125                         $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
126                         $sOSMType = ($aAddressLine['osm_type'] == 'N'?'node':($aAddressLine['osm_type'] == 'W'?'way':($aAddressLine['osm_type'] == 'R'?'relation':'')));
127
128                         echo '<div class="line">';
129                         echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
130                         echo ' (';
131                         echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
132                         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>';
133                         echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
134                         echo ', '.$aAddressLine['area'];
135                         echo ')';
136                         echo '</div>';
137                 }
138                 }
139                 if (sizeof($aParentOfLines) >= 500) {
140                         echo '<p>There are more child objects which are not shown.</p>';
141                 }
142                 echo '</div>';
143         }
144 exit;
145
146
147         $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
148
149         // Make sure the point we are reporting on is fully indexed
150         //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
151         //$oDB->query($sSQL);
152
153         // Get the details for this point
154         $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,";
155         $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, ";
156         $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, ";
157         //$sSQL .= " ST_Area(geometry::geography) as area, ";
158         $sSQL .= " ST_y(centroid) as lat, ST_x(centroid) as lon,";
159         $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
160         $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
161         $sSQL .= " from placex where place_id = $iPlaceID";
162         $aPointDetails = $oDB->getRow($sSQL);
163         if (PEAR::IsError($aPointDetails))
164         {
165                 failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
166         }
167         $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
168
169         $aClassType = getClassTypesWithImportance();
170         $aPointDetails['icon'] = $aClassType[$aPointDetails['class'].':'.$aPointDetails['type']]['icon'];
171
172         // Get all alternative names (languages, etc)
173         $sSQL = "select (each(name)).key,(each(name)).value from placex where place_id = $iPlaceID order by (each(name)).key";
174         $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
175
176         // Extra tags
177         $sSQL = "select (each(extratags)).key,(each(extratags)).value from placex where place_id = $iPlaceID order by (each(extratags)).key";
178         $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
179
180         // Address
181         $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], true);
182
183         // Linked places
184         $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, ";
185         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
186         $sSQL .= " from placex, (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
187         $sSQL .= " where linked_place_id = $iPlaceID";
188         $sSQL .= " order by rank_address asc,rank_search asc,get_name_by_language(name,$sLanguagePrefArraySQL),housenumber";
189         $aLinkedLines = $oDB->getAll($sSQL);
190
191         // All places this is an imediate parent of
192         $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, ";
193         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
194         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
195         $sSQL .= " where parent_place_id = $iPlaceID order by rank_address asc,rank_search asc limit 500) as obj,";
196         $sSQL .= " (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
197         $sSQL .= " order by rank_address asc,rank_search asc,localname,housenumber";
198         $aParentOfLines = $oDB->getAll($sSQL);
199
200         $aPlaceSearchNameKeywords = false;
201         $aPlaceSearchAddressKeywords = false;
202         if (isset($_GET['keywords']) && $_GET['keywords'])
203         {
204                 $sSQL = "select * from search_name where place_id = $iPlaceID";
205                 $aPlaceSearchName = $oDB->getRow($sSQL);
206                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['name_vector'],1,-1).")";
207                 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
208                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['nameaddress_vector'],1,-1).")";
209                 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
210         }
211
212         logEnd($oDB, $hLog, 1);
213
214         include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');