]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
clean up docs for lookup call
[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/AddressDetails.php');
8 require_once(CONST_BasePath.'/lib/output.php');
9 ini_set('memory_limit', '200M');
10
11 $oParams = new Nominatim\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         $sSQL = 'select osm_type, osm_id, errormessage, class, type,';
29         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname,";
30         $sSQL .= ' ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom';
31         $sSQL .= " from import_polygon_error where osm_type = '".$sOsmType;
32         $sSQL .= "' and osm_id = ".$iOsmId.' order by updated desc limit 1';
33         $aPointDetails = chksql($oDB->getRow($sSQL));
34         if ($aPointDetails) {
35             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
36                 $aPointDetails['error_x'] = $aMatches[1];
37                 $aPointDetails['error_y'] = $aMatches[2];
38             }
39             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
40             exit;
41         }
42     }
43 }
44
45 if (!$sPlaceId) userError('Please select a place id');
46
47 $iPlaceID = (int)$sPlaceId;
48
49 if (CONST_Use_US_Tiger_Data) {
50     $iParentPlaceID = chksql($oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID));
51     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
52 }
53
54 if (CONST_Use_Aux_Location_data) {
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
60 $oAddressLookup = new AddressDetails($oDB, $iPlaceID, -1, $aLangPrefOrder);
61 $aPlaceAddress = array_reverse($oAddressLookup->getAddressDetails());
62
63 if (empty($aPlaceAddress)) userError('Unknown place id.');
64
65 $aBreadcrums = array();
66 foreach ($aPlaceAddress as $i => $aPlace) {
67     if (!$aPlace['place_id']) continue;
68     $aBreadcrums[] = array(
69                       'placeId'   => $aPlace['place_id'],
70                       'osmType'   => $aPlace['osm_type'],
71                       'osmId'     => $aPlace['osm_id'],
72                       'localName' => $aPlace['localname']
73                      );
74
75     if ($sOutputFormat == 'html') {
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     header('content-type: application/json; charset=UTF-8');
85     $aDetails = array();
86     $aDetails['breadcrumbs'] = $aBreadcrums;
87     javascript_renderData($aDetails);
88     exit;
89 }
90
91 $aRelatedPlaceIDs = chksql($oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID"));
92
93 $sSQL = 'select obj.place_id, osm_type, osm_id, class, type, housenumber, admin_level,';
94 $sSQL .= " 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 (!empty($aParentOfLines)) {
102     echo '<h2>Parent Of:</h2>';
103     $aGroupedAddressLines = array();
104     foreach ($aParentOfLines as $aAddressLine) {
105         $aAddressLine['label'] = Nominatim\ClassTypes\getProperty($aAddressLine, 'label');
106         if (!$aAddressLine['label']) {
107             $aAddressLine['label'] = ucwords($aAddressLine['type']);
108         }
109
110         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
111             $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
112     }
113
114     foreach ($aGroupedAddressLines as $sGroupHeading => $aParentOfLines) {
115         echo "<h3>$sGroupHeading</h3>";
116         foreach ($aParentOfLines as $aAddressLine) {
117             $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
118             $sOSMType = formatOSMType($aAddressLine['osm_type'], false);
119
120             echo '<div class="line">';
121             echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
122             echo ' (';
123             echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
124             if ($sOSMType) echo ', <span class="osm"><span class="label"></span>'.$sOSMType.' '.osmLink($aAddressLine).'</span>';
125             echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
126             echo ', '.$aAddressLine['area'];
127             echo ')';
128             echo '</div>';
129         }
130     }
131     if (count($aParentOfLines) >= 500) {
132         echo '<p>There are more child objects which are not shown.</p>';
133     }
134     echo '</div>';
135 }