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