8 protected $sType = false;
10 protected $fTigerFraction = -1;
12 protected $aLangPrefOrder = array();
14 protected $bAddressDetails = false;
16 protected $bExtraTags = false;
18 protected $bNameDetails = false;
20 protected $bIncludePolygonAsPoints = false;
21 protected $bIncludePolygonAsText = false;
22 protected $bIncludePolygonAsGeoJSON = false;
23 protected $bIncludePolygonAsKML = false;
24 protected $bIncludePolygonAsSVG = false;
25 protected $fPolygonSimplificationThreshold = 0.0;
28 function PlaceLookup(&$oDB)
33 function setLanguagePreference($aLangPrefOrder)
35 $this->aLangPrefOrder = $aLangPrefOrder;
38 function setIncludeAddressDetails($bAddressDetails = true)
40 $this->bAddressDetails = $bAddressDetails;
43 function setIncludeExtraTags($bExtraTags = false)
45 if ((float) CONST_Postgresql_Version > 9.2)
47 $this->bExtraTags = $bExtraTags;
51 function setIncludeNameDetails($bNameDetails = false)
53 if ((float) CONST_Postgresql_Version > 9.2)
55 $this->bNameDetails = $bNameDetails;
60 function setIncludePolygonAsPoints($b = true)
62 $this->bIncludePolygonAsPoints = $b;
65 function getIncludePolygonAsPoints()
67 return $this->bIncludePolygonAsPoints;
70 function setIncludePolygonAsText($b = true)
72 $this->bIncludePolygonAsText = $b;
75 function getIncludePolygonAsText()
77 return $this->bIncludePolygonAsText;
80 function setIncludePolygonAsGeoJSON($b = true)
82 $this->bIncludePolygonAsGeoJSON = $b;
85 function setIncludePolygonAsKML($b = true)
87 $this->bIncludePolygonAsKML = $b;
90 function setIncludePolygonAsSVG($b = true)
92 $this->bIncludePolygonAsSVG = $b;
95 function setPolygonSimplificationThreshold($f)
97 $this->fPolygonSimplificationThreshold = $f;
101 function setPlaceID($iPlaceID)
103 $this->iPlaceID = $iPlaceID;
106 function setOSMID($sType, $iID)
108 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
109 $this->iPlaceID = $this->oDB->getOne($sSQL);
112 function lookupPlace($details)
114 if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
115 if (isset($details['type'])) $this->sType = $details['type'];
116 if (isset($details['osm_type']) && isset($details['osm_id']))
118 $this->setOSMID($details['osm_type'], $details['osm_id']);
120 if (isset($details['fraction'])) $this->fTigerFraction = $details['fraction'];
122 return $this->lookup();
127 if (!$this->iPlaceID) return null;
129 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
131 if ($this->sType == 'tiger')
133 $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,";
134 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
135 $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, ";
136 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
137 $sSQL .= " null as placename,";
138 $sSQL .= " null as ref,";
139 if ($this->bExtraTags) $sSQL .= " null as extra,";
140 if ($this->bNameDetails) $sSQL .= " null as names,";
141 $sSQL .= " ST_X(point) as lon, ST_Y(point) as lat from (select *, ST_LineInterpolatePoint(linegeo, (housenumber-startnumber::float)/(endnumber-startnumber)::float) as point from ";
142 $sSQL .= " (select *, ";
143 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
144 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber+1)/2)::int*2";
145 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)::int";
146 $sSQL .= " END as housenumber";
147 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
151 $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, ";
152 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
153 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
154 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
155 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
156 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
157 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
158 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
159 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
160 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
163 $aPlace = $this->oDB->getRow($sSQL);
166 if (PEAR::IsError($aPlace))
168 failInternalError("Could not lookup place.", $sSQL, $aPlace);
171 if (!$aPlace['place_id']) return null;
173 if ($this->bAddressDetails)
175 if($this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
176 $aAddress = $this->getAddressNames($aPlace['housenumber']);
178 $aAddress = $this->getAddressNames();
179 $aPlace['aAddress'] = $aAddress;
182 if ($this->bExtraTags)
184 if ($aPlace['extra'])
186 $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
190 $aPlace['sExtraTags'] = (object) array();
194 if ($this->bNameDetails)
196 if ($aPlace['names'])
198 $aPlace['sNameDetails'] = json_decode($aPlace['names']);
202 $aPlace['sNameDetails'] = (object) array();
206 $aClassType = getClassTypes();
208 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
209 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
211 $sAddressType = $aClassType[$aClassType]['simplelabel'];
215 $sClassType = $aPlace['class'].':'.$aPlace['type'];
216 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
217 $sAddressType = $aClassType[$sClassType]['simplelabel'];
218 else $sAddressType = $aPlace['class'];
221 $aPlace['addresstype'] = $sAddressType;
226 function getAddressDetails($bAll = false, $housenumber = -1)
228 if (!$this->iPlaceID) return null;
230 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
232 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.",".$housenumber.")";
233 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
234 $sSQL .= " order by rank_address desc,isaddress desc";
236 $aAddressLines = $this->oDB->getAll($sSQL);
237 if (PEAR::IsError($aAddressLines))
239 var_dump($aAddressLines);
242 return $aAddressLines;
245 function getAddressNames($housenumber = -1)
247 $aAddressLines = $this->getAddressDetails(false, $housenumber);
250 $aFallback = array();
251 $aClassType = getClassTypes();
252 foreach($aAddressLines as $aLine)
256 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
257 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
258 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
260 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
265 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
268 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
270 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
271 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
272 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
274 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
276 $aFallback[$sTypeLabel] = $bFallback;
284 // returns an array which will contain the keys
286 // and may also contain one or more of the keys
293 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
296 $aOutlineResult = array();
297 if (!$iPlaceID) return $aOutlineResult;
299 if (CONST_Search_AreaPolygons)
301 // Get the bounding box and outline polygon
302 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
303 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
304 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
305 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
306 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
307 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
308 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
309 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
310 $sFrom = " from placex where place_id = ".$iPlaceID;
311 if ($this->fPolygonSimplificationThreshold > 0)
313 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
320 $aPointPolygon = $this->oDB->getRow($sSQL);
321 if (PEAR::IsError($aPointPolygon))
323 echo var_dump($aPointPolygon);
324 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
327 if ($aPointPolygon['place_id'])
329 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
331 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
332 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
335 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
336 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
337 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
338 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
339 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
342 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
344 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
345 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
347 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
349 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
350 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
353 $aOutlineResult['aBoundingBox'] = array(
354 (string)$aPointPolygon['minlat'],
355 (string)$aPointPolygon['maxlat'],
356 (string)$aPointPolygon['minlon'],
357 (string)$aPointPolygon['maxlon']
360 } // CONST_Search_AreaPolygons
362 // as a fallback we generate a bounding box without knowing the size of the geometry
363 if ( (!isset($aOutlineResult['aBoundingBox'])) && isset($fLon) )
366 if ($this->bIncludePolygonAsPoints)
368 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
369 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
373 $aBounds['minlat'] = $fLat - $fRadius;
374 $aBounds['maxlat'] = $fLat + $fRadius;
375 $aBounds['minlon'] = $fLon - $fRadius;
376 $aBounds['maxlon'] = $fLon + $fRadius;
378 $aOutlineResult['aBoundingBox'] = array(
379 (string)$aBounds['minlat'],
380 (string)$aBounds['maxlat'],
381 (string)$aBounds['minlon'],
382 (string)$aBounds['maxlon']
385 return $aOutlineResult;