2 @define('CONST_ConnectionBucket_PageType', 'Details');
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');
8 $sOutputFormat = 'html';
9 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
11 $sOutputFormat = $_GET['format'];
14 ini_set('memory_limit', '200M');
18 $aLangPrefOrder = getPreferredLanguages();
19 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
21 if (isset($_GET['osmtype']) && isset($_GET['osmid']) && (int)$_GET['osmid'] && ($_GET['osmtype'] == 'N' || $_GET['osmtype'] == 'W' || $_GET['osmtype'] == 'R'))
23 $_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");
25 // Be nice about our error messages for broken geometry
26 if (!$_GET['place_id'])
28 $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");
29 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
30 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
32 $aPointDetails['error_x'] = $aMatches[1];
33 $aPointDetails['error_y'] = $aMatches[2];
35 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
41 if (!isset($_GET['place_id']))
43 echo "Please select a place id";
47 $iPlaceID = (int)$_GET['place_id'];
49 $sAuxHouseNumber = false;
50 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
53 $iPlaceID = $iParentPlaceID;
57 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
58 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
61 $oPlaceLookup = new PlaceLookup($oDB);
62 $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
63 $oPlaceLookup->setIncludeAddressDetails(true);
64 $oPlaceLookup->setPlaceId($iPlaceID);
66 $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails());
68 if (!sizeof($aPlaceAddress))
70 echo "Unknown place id.";
74 $aBreadcrums = array();
75 foreach($aPlaceAddress as $i => $aPlace)
77 if (!$aPlace['place_id']) continue;
78 $aBreadcrums[] = array('placeId'=>$aPlace['place_id'], 'osmType'=>$aPlace['osm_type'], 'osmId'=>$aPlace['osm_id'], 'localName'=>$aPlace['localname']);
79 $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
80 $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
81 $sOSMUrl = 'http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aPlace['osm_id'];
82 if ($sOutputFormat == 'html') if ($i) echo " > ";
83 if ($sOutputFormat == 'html') echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> (<a href="'.$sOSMUrl.'">osm</a>)';
87 $aDetails['breadcrumbs'] = $aBreadcrums;
89 if ($sOutputFormat == 'json')
91 header("content-type: application/json; charset=UTF-8");
92 javascript_renderData($aDetails);
96 $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
98 $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, ";
99 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
100 $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
101 $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";
102 $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
103 $aParentOfLines = $oDB->getAll($sSQL);
105 if (sizeof($aParentOfLines))
107 echo '<h2>Parent Of:</h2>';
108 $aClassType = getClassTypesWithImportance();
109 $aGroupedAddressLines = array();
110 foreach($aParentOfLines as $aAddressLine)
112 if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
113 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
115 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
117 elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
118 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
120 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
122 else $aAddressLine['label'] = ucwords($aAddressLine['type']);
124 if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
125 $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
127 foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
129 echo "<h3>$sGroupHeading</h3>";
130 foreach($aParentOfLines as $aAddressLine)
132 $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
133 $sOSMType = ($aAddressLine['osm_type'] == 'N'?'node':($aAddressLine['osm_type'] == 'W'?'way':($aAddressLine['osm_type'] == 'R'?'relation':'')));
135 echo '<div class="line">';
136 echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
138 echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
139 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>';
140 echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
141 echo ', '.$aAddressLine['area'];
146 if (sizeof($aParentOfLines) >= 500) {
147 echo '<p>There are more child objects which are not shown.</p>';
154 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
156 // Make sure the point we are reporting on is fully indexed
157 //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
158 //$oDB->query($sSQL);
160 // Get the details for this point
161 $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,";
162 $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, ";
163 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, ";
164 //$sSQL .= " ST_Area(geometry::geography) as area, ";
165 $sSQL .= " ST_y(centroid) as lat, ST_x(centroid) as lon,";
166 $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
167 $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
168 $sSQL .= " from placex where place_id = $iPlaceID";
169 $aPointDetails = $oDB->getRow($sSQL);
170 if (PEAR::IsError($aPointDetails))
172 failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
174 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
176 $aClassType = getClassTypesWithImportance();
177 $aPointDetails['icon'] = $aClassType[$aPointDetails['class'].':'.$aPointDetails['type']]['icon'];
179 // Get all alternative names (languages, etc)
180 $sSQL = "select (each(name)).key,(each(name)).value from placex where place_id = $iPlaceID order by (each(name)).key";
181 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
184 $sSQL = "select (each(extratags)).key,(each(extratags)).value from placex where place_id = $iPlaceID order by (each(extratags)).key";
185 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
188 $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], true);
191 $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, ";
192 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
193 $sSQL .= " from placex, (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
194 $sSQL .= " where linked_place_id = $iPlaceID";
195 $sSQL .= " order by rank_address asc,rank_search asc,get_name_by_language(name,$sLanguagePrefArraySQL),housenumber";
196 $aLinkedLines = $oDB->getAll($sSQL);
198 // All places this is an imediate parent of
199 $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, ";
200 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
201 $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
202 $sSQL .= " where parent_place_id = $iPlaceID order by rank_address asc,rank_search asc limit 500) as obj,";
203 $sSQL .= " (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
204 $sSQL .= " order by rank_address asc,rank_search asc,localname,housenumber";
205 $aParentOfLines = $oDB->getAll($sSQL);
207 $aPlaceSearchNameKeywords = false;
208 $aPlaceSearchAddressKeywords = false;
209 if (isset($_GET['keywords']) && $_GET['keywords'])
211 $sSQL = "select * from search_name where place_id = $iPlaceID";
212 $aPlaceSearchName = $oDB->getRow($sSQL);
213 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['name_vector'],1,-1).")";
214 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
215 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['nameaddress_vector'],1,-1).")";
216 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
219 logEnd($oDB, $hLog, 1);
221 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');