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                 function PlaceLookup(&$oDB)
 
  25                 function setLanguagePreference($aLangPrefOrder)
 
  27                         $this->aLangPrefOrder = $aLangPrefOrder;
 
  30                 function setIncludeAddressDetails($bAddressDetails = true)
 
  32                         $this->bAddressDetails = $bAddressDetails;
 
  35                 function setIncludeExtraTags($bExtraTags = false)
 
  37                         if ((float) CONST_Postgresql_Version > 9.2)
 
  39                                 $this->bExtraTags = $bExtraTags;
 
  43                 function setIncludeNameDetails($bNameDetails = false)
 
  45                         if ((float) CONST_Postgresql_Version > 9.2)
 
  47                                 $this->bNameDetails = $bNameDetails;
 
  51                 function setPlaceID($iPlaceID)
 
  53                         $this->iPlaceID = $iPlaceID;
 
  56                 function setOSMID($sType, $iID)
 
  58                         $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
 
  59                         $this->iPlaceID = $this->oDB->getOne($sSQL);
 
  62                 function lookupPlace($details)
 
  64                         if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
 
  65                         if (isset($details['type'])) $this->sType = $details['type'];
 
  66                         if (isset($details['osm_type']) && isset($details['osm_id']))
 
  68                                 $this->setOSMID($details['osm_type'], $details['osm_id']);
 
  70             if (isset($details['fraction'])) $this->fTigerFraction = $details['fraction'];
 
  72                         return $this->lookup();
 
  77                         if (!$this->iPlaceID) return null;
 
  79                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
 
  81                         if ($this->sType == 'tiger')
 
  83                                 $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,";
 
  84                                 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
 
  85                                 $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, ";
 
  86                                 $sSQL .= " get_address_by_language(place_id, housenumber,$sLanguagePrefArraySQL) as langaddress,";
 
  87                                 $sSQL .= " null as placename,";
 
  88                                 $sSQL .= " null as ref,";
 
  89                                 if ($this->bExtraTags) $sSQL .= " null as extra,";
 
  90                                 if ($this->bNameDetails) $sSQL .= " null as names,";
 
  91                                 $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 ";
 
  92                                 $sSQL .= " (select *, ";
 
  93                 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
 
  94                 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber+1)/2)::int*2";
 
  95                 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)::int";
 
  96                 $sSQL .= " END as housenumber";
 
  97                 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
 
 101                                 $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, ";
 
 102                                 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
 
 103                                 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
 
 104                                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
 
 105                                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
 
 106                                 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
 
 107                                 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
 
 108                                 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
 
 109                                 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
 
 110                                 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
 
 113                         $aPlace = $this->oDB->getRow($sSQL);
 
 116                         if (PEAR::IsError($aPlace))
 
 118                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
 
 121                         if (!$aPlace['place_id']) return null;
 
 123                         if ($this->bAddressDetails)
 
 125                 if($this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
 
 126                     $aAddress = $this->getAddressNames($aPlace['housenumber']);
 
 128                     $aAddress = $this->getAddressNames();
 
 129                                 $aPlace['aAddress'] = $aAddress;
 
 132                         if ($this->bExtraTags)
 
 134                                 if ($aPlace['extra'])
 
 136                                         $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
 
 140                                         $aPlace['sExtraTags'] = (object) array();
 
 144                         if ($this->bNameDetails)
 
 146                                 if ($aPlace['names'])
 
 148                                         $aPlace['sNameDetails'] = json_decode($aPlace['names']);
 
 152                                         $aPlace['sNameDetails'] = (object) array();
 
 156                         $aClassType = getClassTypes();
 
 158                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
 
 159                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 161                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
 
 165                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
 
 166                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 167                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
 
 168                                 else $sAddressType = $aPlace['class'];
 
 171                         $aPlace['addresstype'] = $sAddressType;
 
 176                 function getAddressDetails($bAll = false, $housenumber = -1)
 
 178                         if (!$this->iPlaceID) return null;
 
 180                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
 
 182                         $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.",".$housenumber.")";
 
 183                         if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
 
 184                         $sSQL .= " order by rank_address desc,isaddress desc";
 
 186                         $aAddressLines = $this->oDB->getAll($sSQL);
 
 187                         if (PEAR::IsError($aAddressLines))
 
 189                                 var_dump($aAddressLines);
 
 192                         return $aAddressLines;
 
 195                 function getAddressNames($housenumber = -1)
 
 197                         $aAddressLines = $this->getAddressDetails(false, $housenumber);
 
 200                         $aFallback = array();
 
 201                         $aClassType = getClassTypes();
 
 202                         foreach($aAddressLines as $aLine)
 
 206                                 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
 
 207                                 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
 
 208                                 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
 
 210                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
 
 215                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
 
 218                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
 
 220                                         $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
 
 221                                         $sTypeLabel = str_replace(' ','_',$sTypeLabel);
 
 222                                         if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
 
 224                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
 
 226                                         $aFallback[$sTypeLabel] = $bFallback;