8 protected $bIsTiger = false;
10 protected $aLangPrefOrder = array();
12 protected $bAddressDetails = false;
14 function PlaceLookup(&$oDB)
19 function setLanguagePreference($aLangPrefOrder)
21 $this->aLangPrefOrder = $aLangPrefOrder;
24 function setIncludeAddressDetails($bAddressDetails = true)
26 $this->bAddressDetails = $bAddressDetails;
29 function setPlaceID($iPlaceID)
31 $this->iPlaceID = $iPlaceID;
34 function setOSMID($sType, $iID)
36 $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
37 $this->iPlaceID = $this->oDB->getOne($sSQL);
40 function setIsTiger($b = false)
47 if (!$this->iPlaceID) return null;
49 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
51 $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, ";
52 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
53 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
54 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
55 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
56 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
57 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
58 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
63 $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,";
64 $sSQL .= " 'us' as country_code, null as extratags, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
65 $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, ";
66 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
67 $sSQL .= " null as placename,";
68 $sSQL .= " null as ref,";
69 $sSQL .= " st_y(centroid) as lat,";
70 $sSQL .= " st_x(centroid) as lon";
71 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID;
74 $aPlace = $this->oDB->getRow($sSQL);
77 if (PEAR::IsError($aPlace))
79 failInternalError("Could not lookup place.", $sSQL, $aPlace);
82 if (!$aPlace['place_id']) return null;
84 if ($this->bAddressDetails)
86 $aAddress = $this->getAddressNames();
87 $aPlace['aAddress'] = $aAddress;
91 $aClassType = getClassTypes();
93 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
94 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
96 $sAddressType = $aClassType[$aClassType]['simplelabel'];
100 $sClassType = $aPlace['class'].':'.$aPlace['type'];
101 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
102 $sAddressType = $aClassType[$sClassType]['simplelabel'];
103 else $sAddressType = $aPlace['class'];
106 $aPlace['addresstype'] = $sAddressType;
111 function getAddressDetails($bAll = false)
113 if (!$this->iPlaceID) return null;
115 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
117 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
118 if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
119 $sSQL .= " order by rank_address desc,isaddress desc";
121 $aAddressLines = $this->oDB->getAll($sSQL);
122 if (PEAR::IsError($aAddressLines))
124 var_dump($aAddressLines);
127 return $aAddressLines;
130 function getAddressNames()
132 $aAddressLines = $this->getAddressDetails(false);;
135 $aFallback = array();
136 $aClassType = getClassTypes();
137 foreach($aAddressLines as $aLine)
141 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
142 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
143 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
145 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
150 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
153 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
155 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
156 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
157 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
159 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
161 $aFallback[$sTypeLabel] = $bFallback;