]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
refector reverse geocoding into its own class
[nominatim.git] / lib / PlaceLookup.php
1 <?php
2         class PlaceLookup
3         {
4                 protected $oDB;
5
6                 protected $iPlaceID;
7
8                 protected $aLangPrefOrder = array();
9
10                 protected $bAddressDetails = false;
11
12                 function PlaceLookup(&$oDB)
13                 {
14                         $this->oDB =& $oDB;
15                 }
16
17                 function setLanguagePreference($aLangPrefOrder)
18                 {
19                         $this->aLangPrefOrder = $aLangPrefOrder;
20                 }
21
22                 function setIncludeAddressDetails($bAddressDetails = true)
23                 {
24                         $this->bAddressDetails = $bAddressDetails;
25                 }
26
27                 function setPlaceID($iPlaceID)
28                 {
29                         $this->iPlaceID = $iPlaceID;
30                 }
31
32                 function setOSMID($sType, $iID)
33                 {
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);
36                 }
37
38                 function lookup()
39                 {
40                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
41
42                         $sSQL = "select placex.*,";
43                         $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
44                         $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
45                         $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
46                         $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
47                         $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
48                         $aPlace = $this->oDB->getRow($sSQL);
49
50                         if ($this->bAddressDetails)
51                         {
52                                 $aAddress = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $this->iPlaceID, $aPlace['calculated_country_code']);
53                                 $aPlace['aAddress'] = $aAddress;
54                         }
55
56                         $aClassType = getClassTypes();
57                         $sAddressType = '';
58                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
59                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
60                         {
61                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
62                         }
63                         else
64                         {
65                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
66                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
67                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
68                                 else $sAddressType = $aPlace['class'];
69                         }
70
71                         $aPlace['addresstype'] = $sAddressType;
72
73                         return $aPlace;
74                 }
75         }
76 ?>