]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
Merge pull request #522 from mtmail/PSR2-arrays
[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     $sPlaceId = chksql($oDB->getOne("select place_id from placex where osm_type = '".$sOsmType."' and osm_id = ".$iOsmId." order by type = 'postcode' asc"));
25
26     // Be nice about our error messages for broken geometry
27     if (!$sPlaceId) {
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                 $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     $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
46     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
47 }
48
49 if (CONST_Use_Aux_Location_data) {
50     $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID));
51     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
52 }
53
54 $oPlaceLookup = new PlaceLookup($oDB);
55 $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
56 $oPlaceLookup->setIncludeAddressDetails(true);
57
58 $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails($iPlaceID));
59
60 if (!sizeof($aPlaceAddress)) userError("Unknown place id.");
61
62 $aBreadcrums = array();
63 foreach ($aPlaceAddress as $i => $aPlace) {
64     if (!$aPlace['place_id']) continue;
65     $aBreadcrums[] = array(
66                       'placeId'   => $aPlace['place_id'],
67                       'osmType'   => $aPlace['osm_type'],
68                       'osmId'     => $aPlace['osm_id'],
69                       'localName' => $aPlace['localname']
70                      );
71
72     if ($sOutputFormat == 'html') {
73         $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
74         if ($i) echo " &gt; ";
75         echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> ('.osmLink($aPlace).')';
76     }
77 }
78
79
80 if ($sOutputFormat == 'json') {
81     header("content-type: application/json; charset=UTF-8");
82     $aDetails = array();
83     $aDetails['breadcrumbs'] = $aBreadcrums;
84     javascript_renderData($aDetails);
85     exit;
86 }
87
88 $aRelatedPlaceIDs = chksql($oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID"));
89
90 $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, ";
91 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
92 $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
93 $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";
94 $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
95 $aParentOfLines = chksql($oDB->getAll($sSQL));
96
97 if (sizeof($aParentOfLines)) {
98     echo '<h2>Parent Of:</h2>';
99     $aClassType = getClassTypesWithImportance();
100     $aGroupedAddressLines = array();
101     foreach ($aParentOfLines as $aAddressLine) {
102         if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
103               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label']
104         ) {
105             $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
106         } elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
107                 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label']
108         ) {
109             $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
110         } else $aAddressLine['label'] = ucwords($aAddressLine['type']);
111
112         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
113             $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
114     }
115
116     foreach ($aGroupedAddressLines as $sGroupHeading => $aParentOfLines) {
117         echo "<h3>$sGroupHeading</h3>";
118         foreach ($aParentOfLines as $aAddressLine) {
119             $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
120             $sOSMType = formatOSMType($aAddressLine['osm_type'], false);
121
122             echo '<div class="line">';
123             echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
124             echo ' (';
125             echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
126             if ($sOSMType) echo ', <span class="osm"><span class="label"></span>'.$sOSMType.' '.osmLink($aAddressLine).'</span>';
127             echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
128             echo ', '.$aAddressLine['area'];
129             echo ')';
130             echo '</div>';
131         }
132     }
133     if (sizeof($aParentOfLines) >= 500) {
134         echo '<p>There are more child objects which are not shown.</p>';
135     }
136     echo '</div>';
137 }