]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
Merge pull request #506 from mtmail/primary-vagrant-vm
[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         require_once(CONST_BasePath.'/lib/output.php');
9         ini_set('memory_limit', '200M');
10
11         $oDB =& getDB();
12
13         $sOutputFormat = getParamSet('format', array('html', 'json'), 'html');
14
15         $aLangPrefOrder = getPreferredLanguages();
16         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
17
18         $sPlaceId = getParamString('place_id');
19         $sOsmType = getParamSet('osmtype', array('N', 'W', 'R'));
20         $iOsmId = getParamInt('osmid', -1);
21         if ($sOsmType && $iOsmId > 0)
22         {
23                 $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
24
25                 // Be nice about our error messages for broken geometry
26                 if (!$sPlaceId)
27                 {
28                         $aPointDetails = chksql($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 = '".$sOsmType."' and osm_id = ".$iOsmId." order by updated desc limit 1"));
29                         if ($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 (!$sPlaceId) userError("Please select a place id");
42
43         $iPlaceID = (int)$sPlaceId;
44
45         if (CONST_Use_US_Tiger_Data)
46         {
47                 $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
48                 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
49         }
50
51         if (CONST_Use_Aux_Location_data)
52         {
53                 $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
54                 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
55         }
56
57         $oPlaceLookup = new PlaceLookup($oDB);
58         $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
59         $oPlaceLookup->setIncludeAddressDetails(true);
60
61         $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails($iPlaceID));
62
63         if (!sizeof($aPlaceAddress)) userError("Unknown place id.");
64
65         $aBreadcrums = array();
66         foreach($aPlaceAddress as $i => $aPlace)
67         {
68                 if (!$aPlace['place_id']) continue;
69                 $aBreadcrums[] = array('placeId'   => $aPlace['place_id'],
70                                        'osmType'   => $aPlace['osm_type'],
71                                        'osmId'     => $aPlace['osm_id'],
72                                        'localName' => $aPlace['localname']);
73
74                 if ($sOutputFormat == 'html')
75                 {
76                         $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
77                         if ($i) echo " &gt; ";
78                         echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> ('.osmLink($aPlace).')';
79                 }
80         }
81
82
83         if ($sOutputFormat == 'json')
84         {
85                 header("content-type: application/json; charset=UTF-8");
86                 $aDetails = array();
87                 $aDetails['breadcrumbs'] = $aBreadcrums;
88                 javascript_renderData($aDetails);
89                 exit;
90         }
91
92         $aRelatedPlaceIDs = chksql($oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID"));
93
94         $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, ";
95         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
96         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
97         $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";
98         $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
99         $aParentOfLines = chksql($oDB->getAll($sSQL));
100
101         if (sizeof($aParentOfLines))
102         {
103                 echo '<h2>Parent Of:</h2>';
104                 $aClassType = getClassTypesWithImportance();
105                 $aGroupedAddressLines = array();
106                 foreach($aParentOfLines as $aAddressLine)
107                 {
108                         if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
109                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
110                         {
111                                 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
112                         }
113                         elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
114                                 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
115                         {
116                                 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
117                         }
118                         else $aAddressLine['label'] = ucwords($aAddressLine['type']);
119
120                         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
121                                 $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
122                 }
123
124                 foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
125                 {
126                         echo "<h3>$sGroupHeading</h3>";
127                         foreach($aParentOfLines as $aAddressLine)
128                         {
129                                 $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
130                                 $sOSMType = formatOSMType($aAddressLine['osm_type'], false);
131
132                                 echo '<div class="line">';
133                                 echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
134                                 echo ' (';
135                                 echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
136                                 if ($sOSMType) echo ', <span class="osm"><span class="label"></span>'.$sOSMType.' '.osmLink($aAddressLine).'</span>';
137                                 echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
138                                 echo ', '.$aAddressLine['area'];
139                                 echo ')';
140                                 echo '</div>';
141                         }
142                 }
143                 if (sizeof($aParentOfLines) >= 500) {
144                         echo '<p>There are more child objects which are not shown.</p>';
145                 }
146                 echo '</div>';
147         }