]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
Nominatim::DB support input variables, custom error messages
[nominatim.git] / website / hierarchy.php
1 <?php
2
3 require_once(CONST_BasePath.'/lib/init-website.php');
4 require_once(CONST_BasePath.'/lib/log.php');
5 require_once(CONST_BasePath.'/lib/AddressDetails.php');
6 require_once(CONST_BasePath.'/lib/output.php');
7 ini_set('memory_limit', '200M');
8
9 $oParams = new Nominatim\ParameterParser();
10
11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
12 $aLangPrefOrder = $oParams->getPreferredLanguages();
13
14
15 $sPlaceId = $oParams->getString('place_id');
16 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
17 $iOsmId = $oParams->getInt('osmid', -1);
18
19 $oDB = new Nominatim\DB();
20 $oDB->connect();
21
22 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
23
24 if ($sOsmType && $iOsmId > 0) {
25     $sPlaceId = $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         $sSQL = 'select osm_type, osm_id, errormessage, class, type,';
30         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname,";
31         $sSQL .= ' ST_AsText(prevgeometry) as prevgeom, ST_AsText(newgeometry) as newgeom';
32         $sSQL .= " from import_polygon_error where osm_type = '".$sOsmType;
33         $sSQL .= "' and osm_id = ".$iOsmId.' order by updated desc limit 1';
34         $aPointDetails = $oDB->getRow($sSQL);
35         if ($aPointDetails) {
36             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
37                 $aPointDetails['error_x'] = $aMatches[1];
38                 $aPointDetails['error_y'] = $aMatches[2];
39             }
40             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
41             exit;
42         }
43     }
44 }
45
46 if (!$sPlaceId) userError('Please select a place id');
47
48 $iPlaceID = (int)$sPlaceId;
49
50 if (CONST_Use_US_Tiger_Data) {
51     $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
52     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
53 }
54
55 if (CONST_Use_Aux_Location_data) {
56     $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
57     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
58 }
59
60
61 $oAddressLookup = new AddressDetails($oDB, $iPlaceID, -1, $aLangPrefOrder);
62 $aPlaceAddress = array_reverse($oAddressLookup->getAddressDetails());
63
64 if (empty($aPlaceAddress)) userError('Unknown place id.');
65
66 $aBreadcrums = array();
67 foreach ($aPlaceAddress as $i => $aPlace) {
68     if (!$aPlace['place_id']) continue;
69     $aBreadcrums[] = array(
70                       'placeId'   => $aPlace['place_id'],
71                       'osmType'   => $aPlace['osm_type'],
72                       'osmId'     => $aPlace['osm_id'],
73                       'localName' => $aPlace['localname']
74                      );
75
76     if ($sOutputFormat == 'html') {
77         $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
78         if ($i) echo ' &gt; ';
79         echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> ('.osmLink($aPlace).')';
80     }
81 }
82
83
84 if ($sOutputFormat == 'json') {
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 = $oDB->getCol("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,';
95 $sSQL .= " rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea,  st_area(geometry) as area, ";
96 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
97 $sSQL .= ' from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ';
98 $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';
99 $sSQL .= ' order by rank_address asc,rank_search asc,localname,class, type,housenumber';
100 $aParentOfLines = $oDB->getAll($sSQL);
101
102 if (!empty($aParentOfLines)) {
103     echo '<h2>Parent Of:</h2>';
104     $aGroupedAddressLines = array();
105     foreach ($aParentOfLines as $aAddressLine) {
106         $aAddressLine['label'] = Nominatim\ClassTypes\getProperty($aAddressLine, 'label');
107         if (!$aAddressLine['label']) {
108             $aAddressLine['label'] = ucwords($aAddressLine['type']);
109         }
110
111         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
112             $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
113     }
114
115     foreach ($aGroupedAddressLines as $sGroupHeading => $aParentOfLines) {
116         echo "<h3>$sGroupHeading</h3>";
117         foreach ($aParentOfLines as $aAddressLine) {
118             $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
119             $sOSMType = formatOSMType($aAddressLine['osm_type'], false);
120
121             echo '<div class="line">';
122             echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
123             echo ' (';
124             echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
125             if ($sOSMType) echo ', <span class="osm"><span class="label"></span>'.$sOSMType.' '.osmLink($aAddressLine).'</span>';
126             echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
127             echo ', '.$aAddressLine['area'];
128             echo ')';
129             echo '</div>';
130         }
131     }
132     if (count($aParentOfLines) >= 500) {
133         echo '<p>There are more child objects which are not shown.</p>';
134     }
135     echo '</div>';
136 }