]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
check correct variable for SQL error
[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.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";
45                         $sSQL .= " importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
46                         $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
47                         $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
48                         $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
49                         $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
50                         $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
51                         $aPlace = $this->oDB->getRow($sSQL);
52
53                         if (!$aPlace['place_id']) return null;
54
55                         if ($this->bAddressDetails)
56                         {
57                                 $aAddress = $this->getAddressNames();
58                                 $aPlace['aAddress'] = $aAddress;
59                         }
60
61                         $aClassType = getClassTypes();
62                         $sAddressType = '';
63                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
64                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
65                         {
66                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
67                         }
68                         else
69                         {
70                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
71                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
72                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
73                                 else $sAddressType = $aPlace['class'];
74                         }
75
76                         $aPlace['addresstype'] = $sAddressType;
77
78                         return $aPlace;
79                 }
80
81                 function getAddressDetails($bAll = false)
82                 {
83                         if (!$this->iPlaceID) return null;
84
85                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
86
87                         $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.")";
88                         if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
89                         $sSQL .= " order by rank_address desc,isaddress desc";
90
91                         $aAddressLines = $this->oDB->getAll($sSQL);
92                         if (PEAR::IsError($aAddressLines))
93                         {
94                                 var_dump($aAddressLines);
95                                 exit;
96                         }
97                         return $aAddressLines;
98                 }
99
100                 function getAddressNames()
101                 {
102                         $aAddressLines = $this->getAddressDetails(false);;
103
104                         $aAddress = array();
105                         $aFallback = array();
106                         $aClassType = getClassTypes();
107                         foreach($aAddressLines as $aLine)
108                         {
109                                 $bFallback = false;
110                                 $aTypeLabel = false;
111                                 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
112                                 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
113                                 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
114                                 {
115                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
116                                         $bFallback = true;
117                                 }
118                                 else
119                                 {
120                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
121                                         $bFallback = true;
122                                 }
123                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
124                                 {
125                                         $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
126                                         $sTypeLabel = str_replace(' ','_',$sTypeLabel);
127                                         if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]))
128                                         {
129                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
130                                         }
131                                         $aFallback[$sTypeLabel] = $bFallback;
132                                 }
133                         }
134                         return $aAddress;
135                 }
136
137         }
138 ?>