]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
Implement geocoding and reverse geocoding with tiger interpolation lines instead...
[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 $fTigerFraction =-1;
11
12                 protected $aLangPrefOrder = array();
13
14                 protected $bAddressDetails = false;
15
16                 protected $bExtraTags = false;
17
18                 protected $bNameDetails = false;
19
20                 function PlaceLookup(&$oDB)
21                 {
22                         $this->oDB =& $oDB;
23                 }
24
25                 function setLanguagePreference($aLangPrefOrder)
26                 {
27                         $this->aLangPrefOrder = $aLangPrefOrder;
28                 }
29
30                 function setIncludeAddressDetails($bAddressDetails = true)
31                 {
32                         $this->bAddressDetails = $bAddressDetails;
33                 }
34
35                 function setIncludeExtraTags($bExtraTags = false)
36                 {
37                         if ((float) CONST_Postgresql_Version > 9.2)
38                         {
39                                 $this->bExtraTags = $bExtraTags;
40                         }
41                 }
42
43                 function setIncludeNameDetails($bNameDetails = false)
44                 {
45                         if ((float) CONST_Postgresql_Version > 9.2)
46                         {
47                                 $this->bNameDetails = $bNameDetails;
48                         }
49                 }
50
51                 function setPlaceID($iPlaceID)
52                 {
53                         $this->iPlaceID = $iPlaceID;
54                 }
55
56                 function setOSMID($sType, $iID)
57                 {
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);
60                 }
61
62                 function lookupPlace($details)
63                 {
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']))
67                         {
68                                 $this->setOSMID($details['osm_type'], $details['osm_id']);
69                         }
70             if (isset($details['fraction'])) $this->fTigerFraction = $details['fraction'];
71
72                         return $this->lookup();
73                 }
74
75                 function lookup()
76                 {
77                         if (!$this->iPlaceID) return null;
78
79                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
80
81                         if ($this->sType == 'tiger')
82                         {
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";
98                         }
99                         else
100                         {
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;
111                         }
112
113                         $aPlace = $this->oDB->getRow($sSQL);
114
115
116                         if (PEAR::IsError($aPlace))
117                         {
118                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
119                         }
120
121                         if (!$aPlace['place_id']) return null;
122
123                         if ($this->bAddressDetails)
124                         {
125                 if($this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
126                     $aAddress = $this->getAddressNames($aPlace['housenumber']);
127                 else
128                     $aAddress = $this->getAddressNames();
129                                 $aPlace['aAddress'] = $aAddress;
130                         }
131
132                         if ($this->bExtraTags)
133                         {
134                                 if ($aPlace['extra'])
135                                 {
136                                         $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
137                                 }
138                                 else
139                                 {
140                                         $aPlace['sExtraTags'] = (object) array();
141                                 }
142                         }
143
144                         if ($this->bNameDetails)
145                         {
146                                 if ($aPlace['names'])
147                                 {
148                                         $aPlace['sNameDetails'] = json_decode($aPlace['names']);
149                                 }
150                                 else
151                                 {
152                                         $aPlace['sNameDetails'] = (object) array();
153                                 }
154                         }
155
156                         $aClassType = getClassTypes();
157                         $sAddressType = '';
158                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
159                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
160                         {
161                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
162                         }
163                         else
164                         {
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'];
169                         }
170
171                         $aPlace['addresstype'] = $sAddressType;
172
173                         return $aPlace;
174                 }
175
176                 function getAddressDetails($bAll = false, $housenumber = -1)
177                 {
178                         if (!$this->iPlaceID) return null;
179
180                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
181
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";
185
186                         $aAddressLines = $this->oDB->getAll($sSQL);
187                         if (PEAR::IsError($aAddressLines))
188                         {
189                                 var_dump($aAddressLines);
190                                 exit;
191                         }
192                         return $aAddressLines;
193                 }
194
195                 function getAddressNames($housenumber = -1)
196                 {
197                         $aAddressLines = $this->getAddressDetails(false, $housenumber);
198
199                         $aAddress = array();
200                         $aFallback = array();
201                         $aClassType = getClassTypes();
202                         foreach($aAddressLines as $aLine)
203                         {
204                                 $bFallback = false;
205                                 $aTypeLabel = false;
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))]))
209                                 {
210                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
211                                         $bFallback = true;
212                                 }
213                                 else
214                                 {
215                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
216                                         $bFallback = true;
217                                 }
218                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
219                                 {
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')
223                                         {
224                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
225                                         }
226                                         $aFallback[$sTypeLabel] = $bFallback;
227                                 }
228                         }
229                         return $aAddress;
230                 }
231
232         }
233 ?>