8                 protected $sType = false;
 
  10                 protected $aLangPrefOrder = array();
 
  12                 protected $bAddressDetails = false;
 
  14                 protected $bExtraTags = false;
 
  16                 protected $bNameDetails = false;
 
  18                 function PlaceLookup(&$oDB)
 
  23                 function setLanguagePreference($aLangPrefOrder)
 
  25                         $this->aLangPrefOrder = $aLangPrefOrder;
 
  28                 function setIncludeAddressDetails($bAddressDetails = true)
 
  30                         $this->bAddressDetails = $bAddressDetails;
 
  33                 function setIncludeExtraTags($bExtraTags = false)
 
  35                         if ((float) CONST_Postgresql_Version > 9.2)
 
  37                                 $this->bExtraTags = $bExtraTags;
 
  41                 function setIncludeNameDetails($bNameDetails = false)
 
  43                         if ((float) CONST_Postgresql_Version > 9.2)
 
  45                                 $this->bNameDetails = $bNameDetails;
 
  49                 function setPlaceID($iPlaceID)
 
  51                         $this->iPlaceID = $iPlaceID;
 
  54                 function setOSMID($sType, $iID)
 
  56                         $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
 
  57                         $this->iPlaceID = $this->oDB->getOne($sSQL);
 
  60                 function lookupPlace($details)
 
  62                         if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
 
  63                         if (isset($details['type'])) $this->sType = $details['type'];
 
  64                         if (isset($details['osm_type']) && isset($details['osm_id']))
 
  66                                 $this->setOSMID($details['osm_type'], $details['osm_id']);
 
  69                         return $this->lookup();
 
  74                         if (!$this->iPlaceID) return null;
 
  76                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
 
  78                         if ($this->sType == 'tiger')
 
  80                                 $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,";
 
  81                                 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
 
  82                                 $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, ";
 
  83                                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
 
  84                                 $sSQL .= " null as placename,";
 
  85                                 $sSQL .= " null as ref,";
 
  86                                 if ($this->bExtraTags) $sSQL .= " null as extra,";
 
  87                                 if ($this->bNameDetails) $sSQL .= " null as names,";
 
  88                                 $sSQL .= " st_y(centroid) as lat,";
 
  89                                 $sSQL .= " st_x(centroid) as lon";
 
  90                                 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID;
 
  94                                 $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, ";
 
  95                                 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
 
  96                                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
 
  97                                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
 
  98                                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
 
  99                                 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
 
 100                                 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
 
 101                                 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
 
 102                                 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
 
 103                                 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
 
 106                         $aPlace = $this->oDB->getRow($sSQL);
 
 109                         if (PEAR::IsError($aPlace))
 
 111                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
 
 114                         if (!$aPlace['place_id']) return null;
 
 116                         if ($this->bAddressDetails)
 
 118                                 $aAddress = $this->getAddressNames();
 
 119                                 $aPlace['aAddress'] = $aAddress;
 
 122                         if ($this->bExtraTags)
 
 124                                 if ($aPlace['extra'])
 
 126                                         $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
 
 130                                         $aPlace['sExtraTags'] = (object) array();
 
 134                         if ($this->bNameDetails)
 
 136                                 if ($aPlace['names'])
 
 138                                         $aPlace['sNameDetails'] = json_decode($aPlace['names']);
 
 142                                         $aPlace['sNameDetails'] = (object) array();
 
 146                         $aClassType = getClassTypes();
 
 148                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
 
 149                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 151                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
 
 155                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
 
 156                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 157                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
 
 158                                 else $sAddressType = $aPlace['class'];
 
 161                         $aPlace['addresstype'] = $sAddressType;
 
 166                 function getAddressDetails($bAll = false)
 
 168                         if (!$this->iPlaceID) return null;
 
 170                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
 
 172                         $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
 
 173                         if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
 
 174                         $sSQL .= " order by rank_address desc,isaddress desc";
 
 176                         $aAddressLines = $this->oDB->getAll($sSQL);
 
 177                         if (PEAR::IsError($aAddressLines))
 
 179                                 var_dump($aAddressLines);
 
 182                         return $aAddressLines;
 
 185                 function getAddressNames()
 
 187                         $aAddressLines = $this->getAddressDetails(false);
 
 190                         $aFallback = array();
 
 191                         $aClassType = getClassTypes();
 
 192                         foreach($aAddressLines as $aLine)
 
 196                                 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
 
 197                                 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
 
 198                                 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
 
 200                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
 
 205                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
 
 208                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
 
 210                                         $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
 
 211                                         $sTypeLabel = str_replace(' ','_',$sTypeLabel);
 
 212                                         if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
 
 214                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
 
 216                                         $aFallback[$sTypeLabel] = $bFallback;