]> git.openstreetmap.org Git - nominatim.git/blob - website/hierarchy.php
Test code for creating osm breadcrumbs and hierarchy
[nominatim.git] / website / hierarchy.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Details');
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5         require_once(CONST_BasePath.'/lib/log.php');
6         require_once(CONST_BasePath.'/lib/PlaceLookup.php');
7
8         $sOutputFormat = 'html';
9         /*
10            $fLoadAvg = getLoadAverage();
11            if ($fLoadAvg > 3)
12            {
13            echo "Page temporarily blocked due to high server load\n";
14            exit;
15            }
16          */
17         ini_set('memory_limit', '200M');
18
19         $oDB =& getDB();
20
21         $aLangPrefOrder = getPreferredLanguages();
22         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
23
24         if (isset($_GET['osmtype']) && isset($_GET['osmid']) && (int)$_GET['osmid'] && ($_GET['osmtype'] == 'N' || $_GET['osmtype'] == 'W' || $_GET['osmtype'] == 'R'))
25         {
26                 $_GET['place_id'] = $oDB->getOne("select place_id from placex where osm_type = '".$_GET['osmtype']."' and osm_id = ".(int)$_GET['osmid']." order by type = 'postcode' asc");
27
28                 // Be nice about our error messages for broken geometry
29                 if (!$_GET['place_id'])
30                 {
31                         $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 = '".$_GET['osmtype']."' and osm_id = ".(int)$_GET['osmid']." order by updated desc limit 1");
32                         if (!PEAR::isError($aPointDetails) && $aPointDetails) {
33                                 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
34                                 {
35                                         $aPointDetails['error_x'] = $aMatches[1];
36                                         $aPointDetails['error_y'] = $aMatches[2];
37                                 }
38                                 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
39                                 exit;
40                         }
41                 }
42         }
43
44         if (!isset($_GET['place_id']))
45         {
46                 echo "Please select a place id";
47                 exit;
48         }
49
50         $iPlaceID = (int)$_GET['place_id'];
51
52         $sAuxHouseNumber = false;
53         $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
54         if ($iParentPlaceID)
55         {
56                 $iPlaceID = $iParentPlaceID;
57         }
58         else
59         {
60                 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
61                 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
62         }
63
64         $oPlaceLookup = new PlaceLookup($oDB);
65         $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
66         $oPlaceLookup->setIncludeAddressDetails(true);
67         $oPlaceLookup->setPlaceId($iPlaceID);
68
69         $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails());
70         foreach($aPlaceAddress as $i => $aPlace)
71         {
72                 if (!$aPlace['place_id']) continue;
73                 $sPlaceUrl = 'http://localhost/nominatim/details.php?place_id='.$aPlace['place_id'];
74                 $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
75                 $sOSMUrl = 'http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aPlace['osm_id'];
76                 if ($i) echo " > ";
77                 echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> (<a href="'.$sOSMUrl.'">osm</a>)';
78         }
79
80         $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
81
82         $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, ";
83         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
84         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
85         $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";
86         $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
87         $aParentOfLines = $oDB->getAll($sSQL);
88
89         if (sizeof($aParentOfLines))
90         {
91                 echo '<h2>Parent Of:</h2>';
92                 $aClassType = getClassTypesWithImportance();
93                 $aGroupedAddressLines = array();
94                 foreach($aParentOfLines as $aAddressLine)
95                 {
96                         if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
97                                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
98                                 {
99                                         $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
100                                 }
101                         elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
102                                               && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
103                                 {
104                                         $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
105                                 }
106                         else $aAddressLine['label'] = ucwords($aAddressLine['type']);
107
108                         if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
109                         $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
110                 }
111                 foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
112                 {
113                         echo "<h3>$sGroupHeading</h3>";
114                 foreach($aParentOfLines as $aAddressLine)
115                 {
116                         $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
117                         $sOSMType = ($aAddressLine['osm_type'] == 'N'?'node':($aAddressLine['osm_type'] == 'W'?'way':($aAddressLine['osm_type'] == 'R'?'relation':'')));
118
119                         echo '<div class="line">';
120                         echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
121                         echo ' (';
122                         echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
123                         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>';
124                         echo ', <a href="details.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
125                         echo ', '.$aAddressLine['area'];
126                         echo ')';
127                         echo '</div>';
128                 }
129                 }
130                 if (sizeof($aParentOfLines) >= 500) {
131                         echo '<p>There are more child objects which are not shown.</p>';
132                 }
133                 echo '</div>';
134         }
135 exit;
136
137
138         $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
139
140         // Make sure the point we are reporting on is fully indexed
141         //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
142         //$oDB->query($sSQL);
143
144         // Get the details for this point
145         $sSQL = "select place_id, osm_type, osm_id, class, type, name, admin_level, housenumber, street, isin, postcode, calculated_country_code as country_code, importance, wikipedia,";
146         $sSQL .= " to_char(indexed_date, 'YYYY-MM-DD HH24:MI') as indexed_date, parent_place_id, rank_address, rank_search, get_searchrank_label(rank_search) as rank_search_label, get_name_by_language(name,$sLanguagePrefArraySQL) as localname, ";
147         $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, ";
148         //$sSQL .= " ST_Area(geometry::geography) as area, ";
149         $sSQL .= " ST_y(centroid) as lat, ST_x(centroid) as lon,";
150         $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
151         $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
152         $sSQL .= " from placex where place_id = $iPlaceID";
153         $aPointDetails = $oDB->getRow($sSQL);
154         if (PEAR::IsError($aPointDetails))
155         {
156                 failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
157         }
158         $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
159
160         $aClassType = getClassTypesWithImportance();
161         $aPointDetails['icon'] = $aClassType[$aPointDetails['class'].':'.$aPointDetails['type']]['icon'];
162
163         // Get all alternative names (languages, etc)
164         $sSQL = "select (each(name)).key,(each(name)).value from placex where place_id = $iPlaceID order by (each(name)).key";
165         $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
166
167         // Extra tags
168         $sSQL = "select (each(extratags)).key,(each(extratags)).value from placex where place_id = $iPlaceID order by (each(extratags)).key";
169         $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
170
171         // Address
172         $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], true);
173
174         // Linked places
175         $sSQL = "select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, st_distance(geometry, placegeometry) as distance, ";
176         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
177         $sSQL .= " from placex, (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
178         $sSQL .= " where linked_place_id = $iPlaceID";
179         $sSQL .= " order by rank_address asc,rank_search asc,get_name_by_language(name,$sLanguagePrefArraySQL),housenumber";
180         $aLinkedLines = $oDB->getAll($sSQL);
181
182         // All places this is an imediate parent of
183         $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_distance(geometry, placegeometry) as distance, ";
184         $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
185         $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
186         $sSQL .= " where parent_place_id = $iPlaceID order by rank_address asc,rank_search asc limit 500) as obj,";
187         $sSQL .= " (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
188         $sSQL .= " order by rank_address asc,rank_search asc,localname,housenumber";
189         $aParentOfLines = $oDB->getAll($sSQL);
190
191         $aPlaceSearchNameKeywords = false;
192         $aPlaceSearchAddressKeywords = false;
193         if (isset($_GET['keywords']) && $_GET['keywords'])
194         {
195                 $sSQL = "select * from search_name where place_id = $iPlaceID";
196                 $aPlaceSearchName = $oDB->getRow($sSQL);
197                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['name_vector'],1,-1).")";
198                 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
199                 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['nameaddress_vector'],1,-1).")";
200                 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
201         }
202
203         logEnd($oDB, $hLog, 1);
204
205         include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');