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 $this->bExtraTags = $bExtraTags;
48 function setIncludeNameDetails($bNameDetails = false)
50 $this->bNameDetails = $bNameDetails;
54 function setIncludePolygonAsPoints($b = true)
56 $this->bIncludePolygonAsPoints = $b;
59 function getIncludePolygonAsPoints()
61 return $this->bIncludePolygonAsPoints;
64 function setIncludePolygonAsText($b = true)
66 $this->bIncludePolygonAsText = $b;
69 function getIncludePolygonAsText()
71 return $this->bIncludePolygonAsText;
74 function setIncludePolygonAsGeoJSON($b = true)
76 $this->bIncludePolygonAsGeoJSON = $b;
79 function setIncludePolygonAsKML($b = true)
81 $this->bIncludePolygonAsKML = $b;
84 function setIncludePolygonAsSVG($b = true)
86 $this->bIncludePolygonAsSVG = $b;
89 function setPolygonSimplificationThreshold($f)
91 $this->fPolygonSimplificationThreshold = $f;
95 function setPlaceID($iPlaceID)
97 $this->iPlaceID = $iPlaceID;
100 function setOSMID($sType, $iID)
102 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
103 $this->iPlaceID = $this->oDB->getOne($sSQL);
106 function lookupPlace($details)
108 if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
109 if (isset($details['type'])) $this->sType = $details['type'];
110 if (isset($details['osm_type']) && isset($details['osm_id']))
112 $this->setOSMID($details['osm_type'], $details['osm_id']);
114 if (isset($details['fraction'])) $this->fInterpolFraction = $details['fraction'];
116 return $this->lookup();
121 if (!$this->iPlaceID) return null;
123 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
125 if (CONST_Use_US_Tiger_Data && $this->sType == 'tiger')
127 $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,";
128 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
129 $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, ";
130 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
131 $sSQL .= " null as placename,";
132 $sSQL .= " null as ref,";
133 if ($this->bExtraTags) $sSQL .= " null as extra,";
134 if ($this->bNameDetails) $sSQL .= " null as names,";
135 $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 ";
136 $sSQL .= " (select *, ";
137 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
138 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
139 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
140 $sSQL .= " END as housenumber";
141 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
143 else if ($this->sType == 'interpolation')
145 $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,";
146 $sSQL .= " calculated_country_code as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
147 $sSQL .= " coalesce(null,0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, calculated_country_code, ";
148 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
149 $sSQL .= " null as placename,";
150 $sSQL .= " null as ref,";
151 if ($this->bExtraTags) $sSQL .= " null as extra,";
152 if ($this->bNameDetails) $sSQL .= " null as names,";
153 $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 ";
154 $sSQL .= " (select *, ";
155 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
156 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
157 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
158 $sSQL .= " END as housenumber";
159 $sSQL .= " from location_property_osmline where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
160 // testcase: interpolationtype=odd, startnumber=1000, endnumber=1006, fInterpolFraction=1 => housenumber=1007 => error in st_lineinterpolatepoint
161 // 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)
162 // and not interpolated
166 $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, ";
167 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
168 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
169 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
170 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
171 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
172 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
173 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
174 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
175 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
178 $aPlace = $this->oDB->getRow($sSQL);
181 if (PEAR::IsError($aPlace))
183 failInternalError("Could not lookup place.", $sSQL, $aPlace);
186 if (!$aPlace['place_id']) return null;
188 if ($this->bAddressDetails)
190 if(CONST_Use_US_Tiger_Data && $this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
191 $aAddress = $this->getAddressNames($aPlace['housenumber']);
193 $aAddress = $this->getAddressNames();
194 $aPlace['aAddress'] = $aAddress;
197 if ($this->bExtraTags)
199 if ($aPlace['extra'])
201 $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
205 $aPlace['sExtraTags'] = (object) array();
209 if ($this->bNameDetails)
211 if ($aPlace['names'])
213 $aPlace['sNameDetails'] = json_decode($aPlace['names']);
217 $aPlace['sNameDetails'] = (object) array();
221 $aClassType = getClassTypes();
223 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
224 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
226 $sAddressType = $aClassType[$aClassType]['simplelabel'];
230 $sClassType = $aPlace['class'].':'.$aPlace['type'];
231 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
232 $sAddressType = $aClassType[$sClassType]['simplelabel'];
233 else $sAddressType = $aPlace['class'];
236 $aPlace['addresstype'] = $sAddressType;
241 function getAddressDetails($bAll = false, $housenumber = -1)
243 if (!$this->iPlaceID) return null;
245 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
247 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.",".$housenumber.")";
248 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
249 $sSQL .= " order by rank_address desc,isaddress desc";
251 $aAddressLines = $this->oDB->getAll($sSQL);
252 if (PEAR::IsError($aAddressLines))
254 var_dump($aAddressLines);
257 return $aAddressLines;
260 function getAddressNames($housenumber = -1)
262 $aAddressLines = $this->getAddressDetails(false, $housenumber);
265 $aFallback = array();
266 $aClassType = getClassTypes();
267 foreach($aAddressLines as $aLine)
271 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
272 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
273 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
275 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
280 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
283 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
285 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
286 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
287 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
289 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
291 $aFallback[$sTypeLabel] = $bFallback;
299 // returns an array which will contain the keys
301 // and may also contain one or more of the keys
308 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
311 $aOutlineResult = array();
312 if (!$iPlaceID) return $aOutlineResult;
314 if (CONST_Search_AreaPolygons)
316 // Get the bounding box and outline polygon
317 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
318 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
319 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
320 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
321 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
322 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
323 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
324 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
325 $sFrom = " from placex where place_id = ".$iPlaceID;
326 if ($this->fPolygonSimplificationThreshold > 0)
328 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
335 $aPointPolygon = $this->oDB->getRow($sSQL);
336 if (PEAR::IsError($aPointPolygon))
338 echo var_dump($aPointPolygon);
339 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
342 if ($aPointPolygon['place_id'])
344 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
346 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
347 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
350 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
351 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
352 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
353 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
354 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
357 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
359 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
360 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
362 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
364 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
365 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
368 $aOutlineResult['aBoundingBox'] = array(
369 (string)$aPointPolygon['minlat'],
370 (string)$aPointPolygon['maxlat'],
371 (string)$aPointPolygon['minlon'],
372 (string)$aPointPolygon['maxlon']
375 } // CONST_Search_AreaPolygons
377 // as a fallback we generate a bounding box without knowing the size of the geometry
378 if ( (!isset($aOutlineResult['aBoundingBox'])) && isset($fLon) )
381 if ($this->bIncludePolygonAsPoints)
383 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
384 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
388 $aBounds['minlat'] = $fLat - $fRadius;
389 $aBounds['maxlat'] = $fLat + $fRadius;
390 $aBounds['minlon'] = $fLon - $fRadius;
391 $aBounds['maxlon'] = $fLon + $fRadius;
393 $aOutlineResult['aBoundingBox'] = array(
394 (string)$aBounds['minlat'],
395 (string)$aBounds['maxlat'],
396 (string)$aBounds['minlon'],
397 (string)$aBounds['maxlon']
400 return $aOutlineResult;