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