]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
more tests for libphp - incorporate lonvias feedback
[nominatim.git] / lib / PlaceLookup.php
1 <?php
2         class PlaceLookup
3         {
4                 protected $oDB;
5
6                 protected $iPlaceID;
7
8                 protected $sType = false;
9
10                 protected $aLangPrefOrder = array();
11
12                 protected $bAddressDetails = false;
13
14                 protected $bExtraTags = false;
15
16                 protected $bNameDetails = false;
17
18                 function PlaceLookup(&$oDB)
19                 {
20                         $this->oDB =& $oDB;
21                 }
22
23                 function setLanguagePreference($aLangPrefOrder)
24                 {
25                         $this->aLangPrefOrder = $aLangPrefOrder;
26                 }
27
28                 function setIncludeAddressDetails($bAddressDetails = true)
29                 {
30                         $this->bAddressDetails = $bAddressDetails;
31                 }
32
33                 function setIncludeExtraTags($bExtraTags = false)
34                 {
35                         if ((float) CONST_Postgresql_Version > 9.2)
36                         {
37                                 $this->bExtraTags = $bExtraTags;
38                         }
39                 }
40
41                 function setIncludeNameDetails($bNameDetails = false)
42                 {
43                         if ((float) CONST_Postgresql_Version > 9.2)
44                         {
45                                 $this->bNameDetails = $bNameDetails;
46                         }
47                 }
48
49                 function setPlaceID($iPlaceID)
50                 {
51                         $this->iPlaceID = $iPlaceID;
52                 }
53
54                 function setOSMID($sType, $iID)
55                 {
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);
58                 }
59
60                 function lookupPlace($details)
61                 {
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']))
65                         {
66                                 $this->setOSMID($details['osm_type'], $details['osm_id']);
67                         }
68
69                         return $this->lookup();
70                 }
71
72                 function lookup()
73                 {
74                         if (!$this->iPlaceID) return null;
75
76                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
77
78                         if ($this->sType == 'tiger')
79                         {
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;
91                         }
92                         else
93                         {
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;
104                         }
105
106                         $aPlace = $this->oDB->getRow($sSQL);
107
108
109                         if (PEAR::IsError($aPlace))
110                         {
111                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
112                         }
113
114                         if (!$aPlace['place_id']) return null;
115
116                         if ($this->bAddressDetails)
117                         {
118                                 $aAddress = $this->getAddressNames();
119                                 $aPlace['aAddress'] = $aAddress;
120                         }
121
122                         if ($this->bExtraTags)
123                         {
124                                 if ($aPlace['extra'])
125                                 {
126                                         $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
127                                 }
128                                 else
129                                 {
130                                         $aPlace['sExtraTags'] = (object) array();
131                                 }
132                         }
133
134                         if ($this->bNameDetails)
135                         {
136                                 if ($aPlace['names'])
137                                 {
138                                         $aPlace['sNameDetails'] = json_decode($aPlace['names']);
139                                 }
140                                 else
141                                 {
142                                         $aPlace['sNameDetails'] = (object) array();
143                                 }
144                         }
145
146                         $aClassType = getClassTypes();
147                         $sAddressType = '';
148                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
149                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
150                         {
151                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
152                         }
153                         else
154                         {
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'];
159                         }
160
161                         $aPlace['addresstype'] = $sAddressType;
162
163                         return $aPlace;
164                 }
165
166                 function getAddressDetails($bAll = false)
167                 {
168                         if (!$this->iPlaceID) return null;
169
170                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
171
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";
175
176                         $aAddressLines = $this->oDB->getAll($sSQL);
177                         if (PEAR::IsError($aAddressLines))
178                         {
179                                 var_dump($aAddressLines);
180                                 exit;
181                         }
182                         return $aAddressLines;
183                 }
184
185                 function getAddressNames()
186                 {
187                         $aAddressLines = $this->getAddressDetails(false);
188
189                         $aAddress = array();
190                         $aFallback = array();
191                         $aClassType = getClassTypes();
192                         foreach($aAddressLines as $aLine)
193                         {
194                                 $bFallback = false;
195                                 $aTypeLabel = false;
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))]))
199                                 {
200                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
201                                         $bFallback = true;
202                                 }
203                                 else
204                                 {
205                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
206                                         $bFallback = true;
207                                 }
208                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
209                                 {
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')
213                                         {
214                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
215                                         }
216                                         $aFallback[$sTypeLabel] = $bFallback;
217                                 }
218                         }
219                         return $aAddress;
220                 }
221
222         }
223 ?>