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->fInterpolFraction = $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->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
144 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
145 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(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";
149 else if ($this->sType == 'interpolation')
151 $sSQL = "select place_id, partition, 'I' as osm_type, osm_id, 'place' as class, 'house' as type, null admin_level, housenumber, null as street, null as isin, postcode,";
152 $sSQL .= " calculated_country_code as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
153 $sSQL .= " coalesce(null,0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, calculated_country_code, ";
154 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
155 $sSQL .= " null as placename,";
156 $sSQL .= " null as ref,";
157 if ($this->bExtraTags) $sSQL .= " null as extra,";
158 if ($this->bNameDetails) $sSQL .= " null as names,";
159 $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 ";
160 $sSQL .= " (select *, ";
161 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
162 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
163 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
164 $sSQL .= " END as housenumber";
165 $sSQL .= " from location_property_osmline where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
166 // testcase: interpolationtype=odd, startnumber=1000, endnumber=1006, fInterpolFraction=1 => housenumber=1007 => error in st_lineinterpolatepoint
167 // but this will never happen, because if the searched point is that close to the endnumber, the endnumber house will be directly taken from placex (in ReverseGeocode.php line 220)
168 // and not interpolated
172 $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, ";
173 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
174 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
175 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
176 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
177 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
178 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
179 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
180 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
181 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
184 $aPlace = $this->oDB->getRow($sSQL);
187 if (PEAR::IsError($aPlace))
189 failInternalError("Could not lookup place.", $sSQL, $aPlace);
192 if (!$aPlace['place_id']) return null;
194 if ($this->bAddressDetails)
196 if ($this->sType == 'tiger' || $this->sType == 'interpolation') // to get addressdetails for interpolation lines and tiger data, the housenumber is needed
197 $aAddress = $this->getAddressNames($aPlace['housenumber']);
199 $aAddress = $this->getAddressNames();
200 $aPlace['aAddress'] = $aAddress;
203 if ($this->bExtraTags)
205 if ($aPlace['extra'])
207 $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
211 $aPlace['sExtraTags'] = (object) array();
215 if ($this->bNameDetails)
217 if ($aPlace['names'])
219 $aPlace['sNameDetails'] = json_decode($aPlace['names']);
223 $aPlace['sNameDetails'] = (object) array();
227 $aClassType = getClassTypes();
229 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
230 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
232 $sAddressType = $aClassType[$aClassType]['simplelabel'];
236 $sClassType = $aPlace['class'].':'.$aPlace['type'];
237 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
238 $sAddressType = $aClassType[$sClassType]['simplelabel'];
239 else $sAddressType = $aPlace['class'];
242 $aPlace['addresstype'] = $sAddressType;
247 function getAddressDetails($bAll = false, $housenumber = -1)
249 if (!$this->iPlaceID) return null;
251 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
253 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.",".$housenumber.")";
254 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
255 $sSQL .= " order by rank_address desc,isaddress desc";
257 $aAddressLines = $this->oDB->getAll($sSQL);
258 if (PEAR::IsError($aAddressLines))
260 var_dump($aAddressLines);
263 return $aAddressLines;
266 function getAddressNames($housenumber = -1)
268 $aAddressLines = $this->getAddressDetails(false, $housenumber);
271 $aFallback = array();
272 $aClassType = getClassTypes();
273 foreach($aAddressLines as $aLine)
277 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
278 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
279 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
281 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
286 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
289 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
291 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
292 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
293 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
295 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
297 $aFallback[$sTypeLabel] = $bFallback;
305 // returns an array which will contain the keys
307 // and may also contain one or more of the keys
314 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
317 $aOutlineResult = array();
318 if (!$iPlaceID) return $aOutlineResult;
320 if (CONST_Search_AreaPolygons)
322 // Get the bounding box and outline polygon
323 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
324 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
325 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
326 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
327 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
328 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
329 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
330 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
331 $sFrom = " from placex where place_id = ".$iPlaceID;
332 if ($this->fPolygonSimplificationThreshold > 0)
334 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
341 $aPointPolygon = $this->oDB->getRow($sSQL);
342 if (PEAR::IsError($aPointPolygon))
344 echo var_dump($aPointPolygon);
345 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
348 if ($aPointPolygon['place_id'])
350 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
352 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
353 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
356 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
357 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
358 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
359 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
360 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
363 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
365 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
366 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
368 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
370 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
371 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
374 $aOutlineResult['aBoundingBox'] = array(
375 (string)$aPointPolygon['minlat'],
376 (string)$aPointPolygon['maxlat'],
377 (string)$aPointPolygon['minlon'],
378 (string)$aPointPolygon['maxlon']
381 } // CONST_Search_AreaPolygons
383 // as a fallback we generate a bounding box without knowing the size of the geometry
384 if ( (!isset($aOutlineResult['aBoundingBox'])) && isset($fLon) )
387 if ($this->bIncludePolygonAsPoints)
389 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
390 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
394 $aBounds['minlat'] = $fLat - $fRadius;
395 $aBounds['maxlat'] = $fLat + $fRadius;
396 $aBounds['minlon'] = $fLon - $fRadius;
397 $aBounds['maxlon'] = $fLon + $fRadius;
399 $aOutlineResult['aBoundingBox'] = array(
400 (string)$aBounds['minlat'],
401 (string)$aBounds['maxlat'],
402 (string)$aBounds['minlon'],
403 (string)$aBounds['maxlon']
406 return $aOutlineResult;