8 protected $sType = false;
10 protected $aLangPrefOrder = array();
12 protected $bAddressDetails = false;
14 protected $bExtraTags = false;
16 protected $bNameDetails = false;
18 protected $bIncludePolygonAsPoints = false;
19 protected $bIncludePolygonAsText = false;
20 protected $bIncludePolygonAsGeoJSON = false;
21 protected $bIncludePolygonAsKML = false;
22 protected $bIncludePolygonAsSVG = false;
23 protected $fPolygonSimplificationThreshold = 0.0;
26 function PlaceLookup(&$oDB)
31 function setLanguagePreference($aLangPrefOrder)
33 $this->aLangPrefOrder = $aLangPrefOrder;
36 function setIncludeAddressDetails($bAddressDetails = true)
38 $this->bAddressDetails = $bAddressDetails;
41 function setIncludeExtraTags($bExtraTags = false)
43 if ((float) CONST_Postgresql_Version > 9.2)
45 $this->bExtraTags = $bExtraTags;
49 function setIncludeNameDetails($bNameDetails = false)
51 if ((float) CONST_Postgresql_Version > 9.2)
53 $this->bNameDetails = $bNameDetails;
58 function setIncludePolygonAsPoints($b = true)
60 $this->bIncludePolygonAsPoints = $b;
63 function getIncludePolygonAsPoints()
65 return $this->bIncludePolygonAsPoints;
68 function setIncludePolygonAsText($b = true)
70 $this->bIncludePolygonAsText = $b;
73 function getIncludePolygonAsText()
75 return $this->bIncludePolygonAsText;
78 function setIncludePolygonAsGeoJSON($b = true)
80 $this->bIncludePolygonAsGeoJSON = $b;
83 function setIncludePolygonAsKML($b = true)
85 $this->bIncludePolygonAsKML = $b;
88 function setIncludePolygonAsSVG($b = true)
90 $this->bIncludePolygonAsSVG = $b;
93 function setPolygonSimplificationThreshold($f)
95 $this->fPolygonSimplificationThreshold = $f;
99 function setPlaceID($iPlaceID)
101 $this->iPlaceID = $iPlaceID;
104 function setOSMID($sType, $iID)
106 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
107 $this->iPlaceID = $this->oDB->getOne($sSQL);
110 function lookupPlace($details)
112 if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
113 if (isset($details['type'])) $this->sType = $details['type'];
114 if (isset($details['osm_type']) && isset($details['osm_id']))
116 $this->setOSMID($details['osm_type'], $details['osm_id']);
119 return $this->lookup();
124 if (!$this->iPlaceID) return null;
126 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
128 if ($this->sType == 'tiger')
130 $sSQL = "select place_id,partition, 'T' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, housenumber, null as street, null as isin, postcode,";
131 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
132 $sSQL .= " coalesce(null,0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, 'us' as calculated_country_code, ";
133 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
134 $sSQL .= " null as placename,";
135 $sSQL .= " null as ref,";
136 if ($this->bExtraTags) $sSQL .= " null as extra,";
137 if ($this->bNameDetails) $sSQL .= " null as names,";
138 $sSQL .= " st_y(centroid) as lat,";
139 $sSQL .= " st_x(centroid) as lon";
140 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID;
144 $sSQL = "select placex.place_id, partition, osm_type, osm_id, class, type, admin_level, housenumber, street, isin, postcode, country_code, parent_place_id, linked_place_id, rank_address, rank_search, ";
145 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
146 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
147 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
148 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
149 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
150 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
151 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
152 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
153 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
156 $aPlace = $this->oDB->getRow($sSQL);
159 if (PEAR::IsError($aPlace))
161 failInternalError("Could not lookup place.", $sSQL, $aPlace);
164 if (!$aPlace['place_id']) return null;
166 if ($this->bAddressDetails)
168 $aAddress = $this->getAddressNames();
169 $aPlace['aAddress'] = $aAddress;
172 if ($this->bExtraTags)
174 if ($aPlace['extra'])
176 $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
180 $aPlace['sExtraTags'] = (object) array();
184 if ($this->bNameDetails)
186 if ($aPlace['names'])
188 $aPlace['sNameDetails'] = json_decode($aPlace['names']);
192 $aPlace['sNameDetails'] = (object) array();
196 $aClassType = getClassTypes();
198 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
199 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
201 $sAddressType = $aClassType[$aClassType]['simplelabel'];
205 $sClassType = $aPlace['class'].':'.$aPlace['type'];
206 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
207 $sAddressType = $aClassType[$sClassType]['simplelabel'];
208 else $sAddressType = $aPlace['class'];
211 $aPlace['addresstype'] = $sAddressType;
216 function getAddressDetails($bAll = false)
218 if (!$this->iPlaceID) return null;
220 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
222 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
223 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
224 $sSQL .= " order by rank_address desc,isaddress desc";
226 $aAddressLines = $this->oDB->getAll($sSQL);
227 if (PEAR::IsError($aAddressLines))
229 var_dump($aAddressLines);
232 return $aAddressLines;
235 function getAddressNames()
237 $aAddressLines = $this->getAddressDetails(false);
240 $aFallback = array();
241 $aClassType = getClassTypes();
242 foreach($aAddressLines as $aLine)
246 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
247 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
248 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
250 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
255 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
258 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
260 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
261 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
262 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
264 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
266 $aFallback[$sTypeLabel] = $bFallback;
274 // returns an array which will contain the keys
276 // and may also contain one or more of the keys
283 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
286 $aOutlineResult = array();
287 if (!$iPlaceID) return $aOutlineResult;
289 if (CONST_Search_AreaPolygons)
291 // Get the bounding box and outline polygon
292 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
293 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
294 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
295 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
296 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
297 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
298 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
299 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
300 $sFrom = " from placex where place_id = ".$iPlaceID;
301 if ($this->fPolygonSimplificationThreshold > 0)
303 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
310 $aPointPolygon = $this->oDB->getRow($sSQL);
311 if (PEAR::IsError($aPointPolygon))
313 echo var_dump($aPointPolygon);
314 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
317 if ($aPointPolygon['place_id'])
319 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
321 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
322 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
325 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
326 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
327 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
328 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
329 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
332 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
334 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
335 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
337 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
339 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
340 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
343 $aOutlineResult['aBoundingBox'] = array(
344 (string)$aPointPolygon['minlat'],
345 (string)$aPointPolygon['maxlat'],
346 (string)$aPointPolygon['minlon'],
347 (string)$aPointPolygon['maxlon']
350 } // CONST_Search_AreaPolygons
352 // as a fallback we generate a bounding box without knowing the size of the geometry
353 if ( (!isset($aOutlineResult['aBoundingBox'])) && isset($fLon) )
356 if ($this->bIncludePolygonAsPoints)
358 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
359 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
363 $aBounds['minlat'] = $fLat - $fRadius;
364 $aBounds['maxlat'] = $fLat + $fRadius;
365 $aBounds['minlon'] = $fLon - $fRadius;
366 $aBounds['maxlon'] = $fLon + $fRadius;
368 $aOutlineResult['aBoundingBox'] = array(
369 (string)$aBounds['minlat'],
370 (string)$aBounds['maxlat'],
371 (string)$aBounds['minlon'],
372 (string)$aBounds['maxlon']
375 return $aOutlineResult;