8 protected $aLangPrefOrder = array();
10 protected $bAddressDetails = false;
12 function PlaceLookup(&$oDB)
17 function setLanguagePreference($aLangPrefOrder)
19 $this->aLangPrefOrder = $aLangPrefOrder;
22 function setIncludeAddressDetails($bAddressDetails = true)
24 $this->bAddressDetails = $bAddressDetails;
27 function setPlaceID($iPlaceID)
29 $this->iPlaceID = $iPlaceID;
32 function setOSMID($sType, $iID)
34 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
35 $this->iPlaceID = $this->oDB->getOne($sSQL);
40 if (!$this->iPlaceID) return null;
42 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
44 $sSQL = "select placex.place_id, partition, osm_type, osm_id, class, type, admin_level, housenumber, street, isin, postcode, country_code, extratags, parent_place_id, linked_place_id, rank_address, rank_search, ";
45 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
46 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
47 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
48 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
49 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
50 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
51 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
52 $aPlace = $this->oDB->getRow($sSQL);
54 if (PEAR::IsError($aPlace))
56 failInternalError("Could not lookup place.", $sSQL, $aPlace);
58 if (!$aPlace['place_id']) return null;
60 if ($this->bAddressDetails)
62 $aAddress = $this->getAddressNames();
63 $aPlace['aAddress'] = $aAddress;
66 $aClassType = getClassTypes();
68 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
69 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
71 $sAddressType = $aClassType[$aClassType]['simplelabel'];
75 $sClassType = $aPlace['class'].':'.$aPlace['type'];
76 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
77 $sAddressType = $aClassType[$sClassType]['simplelabel'];
78 else $sAddressType = $aPlace['class'];
81 $aPlace['addresstype'] = $sAddressType;
86 function getAddressDetails($bAll = false)
88 if (!$this->iPlaceID) return null;
90 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
92 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
93 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
94 $sSQL .= " order by rank_address desc,isaddress desc";
96 $aAddressLines = $this->oDB->getAll($sSQL);
97 if (PEAR::IsError($aAddressLines))
99 var_dump($aAddressLines);
102 return $aAddressLines;
105 function getAddressNames()
107 $aAddressLines = $this->getAddressDetails(false);;
110 $aFallback = array();
111 $aClassType = getClassTypes();
112 foreach($aAddressLines as $aLine)
116 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
117 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
118 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
120 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
125 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
128 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
130 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
131 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
132 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
134 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
136 $aFallback[$sTypeLabel] = $bFallback;