2 @define('CONST_ConnectionBucket_PageType', 'Details');
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');
9 $sOutputFormat = 'html';
10 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
12 $sOutputFormat = $_GET['format'];
15 ini_set('memory_limit', '200M');
19 $aLangPrefOrder = getPreferredLanguages();
20 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
22 if (isset($_GET['osmtype']) && isset($_GET['osmid']) && (int)$_GET['osmid'] && ($_GET['osmtype'] == 'N' || $_GET['osmtype'] == 'W' || $_GET['osmtype'] == 'R'))
24 $_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");
26 // Be nice about our error messages for broken geometry
27 if (!$_GET['place_id'])
29 $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");
30 if (!PEAR::isError($aPointDetails) && $aPointDetails) {
31 if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches))
33 $aPointDetails['error_x'] = $aMatches[1];
34 $aPointDetails['error_y'] = $aMatches[2];
36 include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
42 if (!isset($_GET['place_id']))
44 echo "Please select a place id";
48 $iPlaceID = (int)$_GET['place_id'];
50 $sAuxHouseNumber = false;
51 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_tiger where place_id = '.$iPlaceID);
54 $iPlaceID = $iParentPlaceID;
58 $iParentPlaceID = $oDB->getOne('select parent_place_id from location_property_aux where place_id = '.$iPlaceID);
59 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
62 $oPlaceLookup = new PlaceLookup($oDB);
63 $oPlaceLookup->setLanguagePreference($aLangPrefOrder);
64 $oPlaceLookup->setIncludeAddressDetails(true);
65 $oPlaceLookup->setPlaceId($iPlaceID);
67 $aPlaceAddress = array_reverse($oPlaceLookup->getAddressDetails());
69 if (!sizeof($aPlaceAddress))
71 echo "Unknown place id.";
75 $aBreadcrums = array();
76 foreach($aPlaceAddress as $i => $aPlace)
78 if (!$aPlace['place_id']) continue;
79 $aBreadcrums[] = array('placeId'=>$aPlace['place_id'], 'osmType'=>$aPlace['osm_type'], 'osmId'=>$aPlace['osm_id'], 'localName'=>$aPlace['localname']);
80 $sPlaceUrl = 'hierarchy.php?place_id='.$aPlace['place_id'];
81 $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
82 $sOSMUrl = 'http://www.openstreetmap.org/browse/'.$sOSMType.'/'.$aPlace['osm_id'];
83 if ($sOutputFormat == 'html') if ($i) echo " > ";
84 if ($sOutputFormat == 'html') echo '<a href="'.$sPlaceUrl.'">'.$aPlace['localname'].'</a> (<a href="'.$sOSMUrl.'">osm</a>)';
88 $aDetails['breadcrumbs'] = $aBreadcrums;
90 if ($sOutputFormat == 'json')
92 header("content-type: application/json; charset=UTF-8");
93 javascript_renderData($aDetails);
97 $aRelatedPlaceIDs = $oDB->getCol($sSQL = "select place_id from placex where linked_place_id = $iPlaceID or place_id = $iPlaceID");
99 $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, ";
100 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
101 $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
102 $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";
103 $sSQL .= " order by rank_address asc,rank_search asc,localname,class, type,housenumber";
104 $aParentOfLines = $oDB->getAll($sSQL);
106 if (sizeof($aParentOfLines))
108 echo '<h2>Parent Of:</h2>';
109 $aClassType = getClassTypesWithImportance();
110 $aGroupedAddressLines = array();
111 foreach($aParentOfLines as $aAddressLine)
113 if (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
114 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'])
116 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type'].':'.$aAddressLine['admin_level']]['label'];
118 elseif (isset($aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
119 && $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'])
121 $aAddressLine['label'] = $aClassType[$aAddressLine['class'].':'.$aAddressLine['type']]['label'];
123 else $aAddressLine['label'] = ucwords($aAddressLine['type']);
125 if (!isset($aGroupedAddressLines[$aAddressLine['label']])) $aGroupedAddressLines[$aAddressLine['label']] = array();
126 $aGroupedAddressLines[$aAddressLine['label']][] = $aAddressLine;
128 foreach($aGroupedAddressLines as $sGroupHeading => $aParentOfLines)
130 echo "<h3>$sGroupHeading</h3>";
131 foreach($aParentOfLines as $aAddressLine)
133 $aAddressLine['localname'] = $aAddressLine['localname']?$aAddressLine['localname']:$aAddressLine['housenumber'];
134 $sOSMType = ($aAddressLine['osm_type'] == 'N'?'node':($aAddressLine['osm_type'] == 'W'?'way':($aAddressLine['osm_type'] == 'R'?'relation':'')));
136 echo '<div class="line">';
137 echo '<span class="name">'.(trim($aAddressLine['localname'])?$aAddressLine['localname']:'<span class="noname">No Name</span>').'</span>';
139 echo '<span class="area">'.($aAddressLine['isarea']=='t'?'Polygon':'Point').'</span>';
140 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>';
141 echo ', <a href="hierarchy.php?place_id='.$aAddressLine['place_id'].'">GOTO</a>';
142 echo ', '.$aAddressLine['area'];
147 if (sizeof($aParentOfLines) >= 500) {
148 echo '<p>There are more child objects which are not shown.</p>';
155 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
157 // Make sure the point we are reporting on is fully indexed
158 //$sSQL = "UPDATE placex set indexed = true where indexed = false and place_id = $iPlaceID";
159 //$oDB->query($sSQL);
161 // Get the details for this point
162 $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,";
163 $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, ";
164 $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') as isarea, ";
165 //$sSQL .= " ST_Area(geometry::geography) as area, ";
166 $sSQL .= " ST_y(centroid) as lat, ST_x(centroid) as lon,";
167 $sSQL .= " case when importance = 0 OR importance IS NULL then 0.75-(rank_search::float/40) else importance end as calculated_importance, ";
168 $sSQL .= " ST_AsText(CASE WHEN ST_NPoints(geometry) > 5000 THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ELSE geometry END) as outlinestring";
169 $sSQL .= " from placex where place_id = $iPlaceID";
170 $aPointDetails = $oDB->getRow($sSQL);
171 if (PEAR::IsError($aPointDetails))
173 failInternalError("Could not get details of place object.", $sSQL, $aPointDetails);
175 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
177 $aClassType = getClassTypesWithImportance();
178 $aPointDetails['icon'] = $aClassType[$aPointDetails['class'].':'.$aPointDetails['type']]['icon'];
180 // Get all alternative names (languages, etc)
181 $sSQL = "select (each(name)).key,(each(name)).value from placex where place_id = $iPlaceID order by (each(name)).key";
182 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
185 $sSQL = "select (each(extratags)).key,(each(extratags)).value from placex where place_id = $iPlaceID order by (each(extratags)).key";
186 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
189 $aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], true);
192 $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, ";
193 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
194 $sSQL .= " from placex, (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
195 $sSQL .= " where linked_place_id = $iPlaceID";
196 $sSQL .= " order by rank_address asc,rank_search asc,get_name_by_language(name,$sLanguagePrefArraySQL),housenumber";
197 $aLinkedLines = $oDB->getAll($sSQL);
199 // All places this is an imediate parent of
200 $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, ";
201 $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) as localname, length(name::text) as namelength ";
202 $sSQL .= " from (select placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name from placex ";
203 $sSQL .= " where parent_place_id = $iPlaceID order by rank_address asc,rank_search asc limit 500) as obj,";
204 $sSQL .= " (select geometry as placegeometry from placex where place_id = $iPlaceID) as x";
205 $sSQL .= " order by rank_address asc,rank_search asc,localname,housenumber";
206 $aParentOfLines = $oDB->getAll($sSQL);
208 $aPlaceSearchNameKeywords = false;
209 $aPlaceSearchAddressKeywords = false;
210 if (isset($_GET['keywords']) && $_GET['keywords'])
212 $sSQL = "select * from search_name where place_id = $iPlaceID";
213 $aPlaceSearchName = $oDB->getRow($sSQL);
214 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['name_vector'],1,-1).")";
215 $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
216 $sSQL = "select * from word where word_id in (".substr($aPlaceSearchName['nameaddress_vector'],1,-1).")";
217 $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
220 logEnd($oDB, $hLog, 1);
222 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');