]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
Merge branch 'master' of https://github.com/spin0us/Nominatim
[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                         if (!$this->iPlaceID) return null;
41
42                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
43
44                         $sSQL = "select placex.*,";
45                         $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
46                         $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
47                         $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
48                         $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
49                         $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
50                         $aPlace = $this->oDB->getRow($sSQL);
51
52                         if (!$aPlace['place_id']) return null;
53
54                         if ($this->bAddressDetails)
55                         {
56                                 $aAddress = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $this->iPlaceID, $aPlace['calculated_country_code']);
57                                 $aPlace['aAddress'] = $aAddress;
58                         }
59
60                         $aClassType = getClassTypes();
61                         $sAddressType = '';
62                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
63                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
64                         {
65                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
66                         }
67                         else
68                         {
69                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
70                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
71                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
72                                 else $sAddressType = $aPlace['class'];
73                         }
74
75                         $aPlace['addresstype'] = $sAddressType;
76
77                         return $aPlace;
78                 }
79         }
80 ?>