7 protected $aLangPrefOrder = array();
9 protected $bAddressDetails = false;
10 protected $bExtraTags = false;
11 protected $bNameDetails = false;
13 protected $bIncludePolygonAsPoints = false;
14 protected $bIncludePolygonAsText = false;
15 protected $bIncludePolygonAsGeoJSON = false;
16 protected $bIncludePolygonAsKML = false;
17 protected $bIncludePolygonAsSVG = false;
18 protected $fPolygonSimplificationThreshold = 0.0;
21 function PlaceLookup(&$oDB)
26 function setLanguagePreference($aLangPrefOrder)
28 $this->aLangPrefOrder = $aLangPrefOrder;
31 function setIncludeAddressDetails($bAddressDetails = true)
33 $this->bAddressDetails = $bAddressDetails;
36 function setIncludeExtraTags($bExtraTags = false)
38 $this->bExtraTags = $bExtraTags;
41 function setIncludeNameDetails($bNameDetails = false)
43 $this->bNameDetails = $bNameDetails;
47 function setIncludePolygonAsPoints($b = true)
49 $this->bIncludePolygonAsPoints = $b;
52 function getIncludePolygonAsPoints()
54 return $this->bIncludePolygonAsPoints;
57 function setIncludePolygonAsText($b = true)
59 $this->bIncludePolygonAsText = $b;
62 function getIncludePolygonAsText()
64 return $this->bIncludePolygonAsText;
67 function setIncludePolygonAsGeoJSON($b = true)
69 $this->bIncludePolygonAsGeoJSON = $b;
72 function setIncludePolygonAsKML($b = true)
74 $this->bIncludePolygonAsKML = $b;
77 function setIncludePolygonAsSVG($b = true)
79 $this->bIncludePolygonAsSVG = $b;
82 function setPolygonSimplificationThreshold($f)
84 $this->fPolygonSimplificationThreshold = $f;
87 function lookupOSMID($sType, $iID)
89 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
90 $iPlaceID = chksql($this->oDB->getOne($sSQL));
92 return $this->lookup((int)$iPlaceID);
95 function lookup($iPlaceID, $sType = '', $fInterpolFraction = 0.0)
97 if (!$iPlaceID) return null;
99 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
100 $bIsTiger = CONST_Use_US_Tiger_Data && $sType == 'tiger';
101 $bIsInterpolation = $sType == 'interpolation';
104 $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,";
105 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
106 $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, ";
107 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
108 $sSQL .= " null as placename,";
109 $sSQL .= " null as ref,";
110 if ($this->bExtraTags) $sSQL .= " null as extra,";
111 if ($this->bNameDetails) $sSQL .= " null as names,";
112 $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 ";
113 $sSQL .= " (select *, ";
114 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
115 $sSQL .= " WHEN interpolationtype='even' THEN ((".$fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
116 $sSQL .= " WHEN interpolationtype='all' THEN (".$fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
117 $sSQL .= " END as housenumber";
118 $sSQL .= " from location_property_tiger where place_id = ".$iPlaceID.") as blub1) as blub2";
119 } else if ($bIsInterpolation) {
120 $sSQL = "select place_id, partition, 'W' as osm_type, osm_id, 'place' as class, 'house' as type, null admin_level, housenumber, null as street, null as isin, postcode,";
121 $sSQL .= " calculated_country_code as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
122 $sSQL .= " (0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, calculated_country_code, ";
123 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
124 $sSQL .= " null as placename,";
125 $sSQL .= " null as ref,";
126 if ($this->bExtraTags) $sSQL .= " null as extra,";
127 if ($this->bNameDetails) $sSQL .= " null as names,";
128 $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 ";
129 $sSQL .= " (select *, ";
130 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
131 $sSQL .= " WHEN interpolationtype='even' THEN ((".$fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
132 $sSQL .= " WHEN interpolationtype='all' THEN (".$fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
133 $sSQL .= " END as housenumber";
134 $sSQL .= " from location_property_osmline where place_id = ".$iPlaceID.") as blub1) as blub2";
135 // testcase: interpolationtype=odd, startnumber=1000, endnumber=1006, fInterpolFraction=1 => housenumber=1007 => error in st_lineinterpolatepoint
136 // 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)
137 // and not interpolated
139 $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, ";
140 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
141 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
142 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
143 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
144 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
145 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
146 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
147 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
148 $sSQL .= " from placex where place_id = ".$iPlaceID;
151 $aPlace = chksql($this->oDB->getRow($sSQL), "Could not lookup place");
153 if (!$aPlace['place_id']) return null;
155 if ($this->bAddressDetails) {
156 // to get addressdetails for tiger data, the housenumber is needed
157 $iHousenumber = ($bIsTiger || $bIsInterpolation) ? $aPlace['housenumber'] : -1;
158 $aPlace['aAddress'] = $this->getAddressNames($aPlace['place_id'],
162 if ($this->bExtraTags) {
163 if ($aPlace['extra']) {
164 $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
166 $aPlace['sExtraTags'] = (object) array();
170 if ($this->bNameDetails) {
171 if ($aPlace['names']) {
172 $aPlace['sNameDetails'] = json_decode($aPlace['names']);
174 $aPlace['sNameDetails'] = (object) array();
178 $aClassType = getClassTypes();
180 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
181 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel'])) {
182 $sAddressType = $aClassType[$aClassType]['simplelabel'];
184 $sClassType = $aPlace['class'].':'.$aPlace['type'];
185 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
186 $sAddressType = $aClassType[$sClassType]['simplelabel'];
187 else $sAddressType = $aPlace['class'];
190 $aPlace['addresstype'] = $sAddressType;
195 function getAddressDetails($iPlaceID, $bAll = false, $housenumber = -1)
197 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
199 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$iPlaceID.",".$housenumber.")";
200 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
201 $sSQL .= " order by rank_address desc,isaddress desc";
203 return chksql($this->oDB->getAll($sSQL));
206 function getAddressNames($iPlaceID, $housenumber = -1)
208 $aAddressLines = $this->getAddressDetails($iPlaceID, false, $housenumber);
211 $aFallback = array();
212 $aClassType = getClassTypes();
213 foreach ($aAddressLines as $aLine) {
216 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) {
217 $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
218 } elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) {
219 $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
220 } elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))])) {
221 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
224 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
227 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber']))) {
228 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
229 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
230 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place') {
231 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
233 $aFallback[$sTypeLabel] = $bFallback;
241 // returns an array which will contain the keys
243 // and may also contain one or more of the keys
250 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
253 $aOutlineResult = array();
254 if (!$iPlaceID) return $aOutlineResult;
256 if (CONST_Search_AreaPolygons) {
257 // Get the bounding box and outline polygon
258 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
259 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
260 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
261 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
262 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
263 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
264 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
265 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
266 $sFrom = " from placex where place_id = ".$iPlaceID;
267 if ($this->fPolygonSimplificationThreshold > 0) {
268 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
273 $aPointPolygon = chksql($this->oDB->getRow($sSQL),
274 "Could not get outline");
276 if ($aPointPolygon['place_id']) {
277 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null) {
278 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
279 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
282 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
283 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
284 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
285 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
286 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
289 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) {
290 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
291 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
294 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001) {
295 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
296 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
299 $aOutlineResult['aBoundingBox'] = array(
300 (string)$aPointPolygon['minlat'],
301 (string)$aPointPolygon['maxlat'],
302 (string)$aPointPolygon['minlon'],
303 (string)$aPointPolygon['maxlon']
306 } // CONST_Search_AreaPolygons
308 // as a fallback we generate a bounding box without knowing the size of the geometry
309 if ((!isset($aOutlineResult['aBoundingBox'])) && isset($fLon)) {
311 if ($this->bIncludePolygonAsPoints) {
312 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
313 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
317 $aBounds['minlat'] = $fLat - $fRadius;
318 $aBounds['maxlat'] = $fLat + $fRadius;
319 $aBounds['minlon'] = $fLon - $fRadius;
320 $aBounds['maxlon'] = $fLon + $fRadius;
322 $aOutlineResult['aBoundingBox'] = array(
323 (string)$aBounds['minlat'],
324 (string)$aBounds['maxlat'],
325 (string)$aBounds['minlon'],
326 (string)$aBounds['maxlon']
329 return $aOutlineResult;