]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
remove unused functions in php library
[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                 protected $bIncludePolygonAsPoints = false;
21                 protected $bIncludePolygonAsText = false;
22                 protected $bIncludePolygonAsGeoJSON = false;
23                 protected $bIncludePolygonAsKML = false;
24                 protected $bIncludePolygonAsSVG = false;
25                 protected $fPolygonSimplificationThreshold = 0.0;
26
27
28                 function PlaceLookup(&$oDB)
29                 {
30                         $this->oDB =& $oDB;
31                 }
32
33                 function setLanguagePreference($aLangPrefOrder)
34                 {
35                         $this->aLangPrefOrder = $aLangPrefOrder;
36                 }
37
38                 function setIncludeAddressDetails($bAddressDetails = true)
39                 {
40                         $this->bAddressDetails = $bAddressDetails;
41                 }
42
43                 function setIncludeExtraTags($bExtraTags = false)
44                 {
45                         if ((float) CONST_Postgresql_Version > 9.2)
46                         {
47                                 $this->bExtraTags = $bExtraTags;
48                         }
49                 }
50
51                 function setIncludeNameDetails($bNameDetails = false)
52                 {
53                         if ((float) CONST_Postgresql_Version > 9.2)
54                         {
55                                 $this->bNameDetails = $bNameDetails;
56                         }
57                 }
58
59
60                 function setIncludePolygonAsPoints($b = true)
61                 {
62                         $this->bIncludePolygonAsPoints = $b;
63                 }
64
65                 function getIncludePolygonAsPoints()
66                 {
67                         return $this->bIncludePolygonAsPoints;
68                 }
69
70                 function setIncludePolygonAsText($b = true)
71                 {
72                         $this->bIncludePolygonAsText = $b;
73                 }
74
75                 function getIncludePolygonAsText()
76                 {
77                         return $this->bIncludePolygonAsText;
78                 }
79
80                 function setIncludePolygonAsGeoJSON($b = true)
81                 {
82                         $this->bIncludePolygonAsGeoJSON = $b;
83                 }
84
85                 function setIncludePolygonAsKML($b = true)
86                 {
87                         $this->bIncludePolygonAsKML = $b;
88                 }
89
90                 function setIncludePolygonAsSVG($b = true)
91                 {
92                         $this->bIncludePolygonAsSVG = $b;
93                 }
94
95                 function setPolygonSimplificationThreshold($f)
96                 {
97                         $this->fPolygonSimplificationThreshold = $f;
98                 }
99
100
101                 function setPlaceID($iPlaceID)
102                 {
103                         $this->iPlaceID = $iPlaceID;
104                 }
105
106                 function setOSMID($sType, $iID)
107                 {
108                         $sSQL = "select place_id from placex where osm_type = '".pg_escape_string($sType)."' and osm_id = ".(int)$iID." order by type = 'postcode' asc";
109                         $this->iPlaceID = $this->oDB->getOne($sSQL);
110                 }
111
112                 function lookupPlace($details)
113                 {
114                         if (isset($details['place_id'])) $this->iPlaceID = $details['place_id'];
115                         if (isset($details['type'])) $this->sType = $details['type'];
116                         if (isset($details['osm_type']) && isset($details['osm_id']))
117                         {
118                                 $this->setOSMID($details['osm_type'], $details['osm_id']);
119                         }
120                         if (isset($details['fraction'])) $this->fTigerFraction = $details['fraction'];
121
122                         return $this->lookup();
123                 }
124
125                 function lookup()
126                 {
127                         if (!$this->iPlaceID) return null;
128
129                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
130
131                         if ($this->sType == 'tiger')
132                         {
133                                 $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,";
134                                 $sSQL .= " 'us' as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
135                                 $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, ";
136                                 $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
137                                 $sSQL .= " null as placename,";
138                                 $sSQL .= " null as ref,";
139                                 if ($this->bExtraTags) $sSQL .= " null as extra,";
140                                 if ($this->bNameDetails) $sSQL .= " null as names,";
141                                 $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 ";
142                                 $sSQL .= " (select *, ";
143                                 $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
144                                 $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber+1)/2)::int*2";
145                                 $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)::int";
146                                 $sSQL .= " END as housenumber";
147                                 $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
148                         }
149                         else
150                         {
151                                 $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, ";
152                                 $sSQL .= " coalesce(importance,0.75-(rank_search::float/40)) as importance, indexed_status, indexed_date, wikipedia, calculated_country_code, ";
153                                 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
154                                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
155                                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
156                                 if ($this->bExtraTags) $sSQL .= " hstore_to_json(extratags) as extra,";
157                                 if ($this->bNameDetails) $sSQL .= " hstore_to_json(name) as names,";
158                                 $sSQL .= " (case when centroid is null then st_y(st_centroid(geometry)) else st_y(centroid) end) as lat,";
159                                 $sSQL .= " (case when centroid is null then st_x(st_centroid(geometry)) else st_x(centroid) end) as lon";
160                                 $sSQL .= " from placex where place_id = ".(int)$this->iPlaceID;
161                         }
162
163                         $aPlace = $this->oDB->getRow($sSQL);
164
165
166                         if (PEAR::IsError($aPlace))
167                         {
168                                 failInternalError("Could not lookup place.", $sSQL, $aPlace);
169                         }
170
171                         if (!$aPlace['place_id']) return null;
172
173                         if ($this->bAddressDetails)
174                         {
175                                 if($this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
176                                         $aAddress = $this->getAddressNames($aPlace['housenumber']);
177                                 else
178                                         $aAddress = $this->getAddressNames();
179                                 $aPlace['aAddress'] = $aAddress;
180                         }
181
182                         if ($this->bExtraTags)
183                         {
184                                 if ($aPlace['extra'])
185                                 {
186                                         $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
187                                 }
188                                 else
189                                 {
190                                         $aPlace['sExtraTags'] = (object) array();
191                                 }
192                         }
193
194                         if ($this->bNameDetails)
195                         {
196                                 if ($aPlace['names'])
197                                 {
198                                         $aPlace['sNameDetails'] = json_decode($aPlace['names']);
199                                 }
200                                 else
201                                 {
202                                         $aPlace['sNameDetails'] = (object) array();
203                                 }
204                         }
205
206                         $aClassType = getClassTypes();
207                         $sAddressType = '';
208                         $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
209                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
210                         {
211                                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
212                         }
213                         else
214                         {
215                                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
216                                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
217                                         $sAddressType = $aClassType[$sClassType]['simplelabel'];
218                                 else $sAddressType = $aPlace['class'];
219                         }
220
221                         $aPlace['addresstype'] = $sAddressType;
222
223                         return $aPlace;
224                 }
225
226                 function getAddressDetails($bAll = false, $housenumber = -1)
227                 {
228                         if (!$this->iPlaceID) return null;
229
230                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted", $this->aLangPrefOrder))."]";
231
232                         $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(".$this->iPlaceID.",".$housenumber.")";
233                         if (!$bAll) $sSQL .= " WHERE isaddress OR type = 'country_code'";
234                         $sSQL .= " order by rank_address desc,isaddress desc";
235
236                         $aAddressLines = $this->oDB->getAll($sSQL);
237                         if (PEAR::IsError($aAddressLines))
238                         {
239                                 var_dump($aAddressLines);
240                                 exit;
241                         }
242                         return $aAddressLines;
243                 }
244
245                 function getAddressNames($housenumber = -1)
246                 {
247                         $aAddressLines = $this->getAddressDetails(false, $housenumber);
248
249                         $aAddress = array();
250                         $aFallback = array();
251                         $aClassType = getClassTypes();
252                         foreach($aAddressLines as $aLine)
253                         {
254                                 $bFallback = false;
255                                 $aTypeLabel = false;
256                                 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
257                                 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
258                                 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
259                                 {
260                                         $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
261                                         $bFallback = true;
262                                 }
263                                 else
264                                 {
265                                         $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
266                                         $bFallback = true;
267                                 }
268                                 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
269                                 {
270                                         $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
271                                         $sTypeLabel = str_replace(' ','_',$sTypeLabel);
272                                         if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
273                                         {
274                                                 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
275                                         }
276                                         $aFallback[$sTypeLabel] = $bFallback;
277                                 }
278                         }
279                         return $aAddress;
280                 }
281
282
283
284                 // returns an array which will contain the keys
285                 //   aBoundingBox
286                 // and may also contain one or more of the keys
287                 //   asgeojson
288                 //   askml
289                 //   assvg
290                 //   astext
291                 //   lat
292                 //   lon
293                 function getOutlines($iPlaceID, $fLon=null, $fLat=null, $fRadius=null)
294                 {
295
296                         $aOutlineResult = array();
297                         if (!$iPlaceID) return $aOutlineResult;
298
299                         if (CONST_Search_AreaPolygons)
300                         {
301                                 // Get the bounding box and outline polygon
302                                 $sSQL  = "select place_id,0 as numfeatures,st_area(geometry) as area,";
303                                 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
304                                 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
305                                 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
306                                 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
307                                 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
308                                 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
309                                 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
310                                 $sFrom = " from placex where place_id = ".$iPlaceID;
311                                 if ($this->fPolygonSimplificationThreshold > 0)
312                                 {
313                                         $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
314                                 }
315                                 else
316                                 {
317                                         $sSQL .= $sFrom;
318                                 }
319
320                                 $aPointPolygon = $this->oDB->getRow($sSQL);
321                                 if (PEAR::IsError($aPointPolygon))
322                                 {
323                                         echo var_dump($aPointPolygon);
324                                         failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
325                                 }
326
327                                 if ($aPointPolygon['place_id'])
328                                 {
329                                         if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
330                                         {
331                                                 $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
332                                                 $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
333                                         }
334
335                                         if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
336                                         if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
337                                         if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
338                                         if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
339                                         if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
340
341
342                                         if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
343                                         {
344                                                 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
345                                                 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
346                                         }
347                                         if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
348                                         {
349                                                 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
350                                                 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
351                                         }
352
353                                         $aOutlineResult['aBoundingBox'] = array(
354                                                                           (string)$aPointPolygon['minlat'],
355                                                                           (string)$aPointPolygon['maxlat'],
356                                                                           (string)$aPointPolygon['minlon'],
357                                                                           (string)$aPointPolygon['maxlon']
358                                                                          );
359                                 }
360                         } // CONST_Search_AreaPolygons
361
362                         // as a fallback we generate a bounding box without knowing the size of the geometry
363                         if ( (!isset($aOutlineResult['aBoundingBox'])) && isset($fLon) )
364                         {
365
366                                 if ($this->bIncludePolygonAsPoints)
367                                 {
368                                         $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
369                                         $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
370                                 }
371
372                                 $aBounds = array();
373                                 $aBounds['minlat'] = $fLat - $fRadius;
374                                 $aBounds['maxlat'] = $fLat + $fRadius;
375                                 $aBounds['minlon'] = $fLon - $fRadius;
376                                 $aBounds['maxlon'] = $fLon + $fRadius;
377
378                                 $aOutlineResult['aBoundingBox'] = array(
379                                                                   (string)$aBounds['minlat'],
380                                                                   (string)$aBounds['maxlat'],
381                                                                   (string)$aBounds['minlon'],
382                                                                   (string)$aBounds['maxlon']
383                                                                  );
384                         }
385                         return $aOutlineResult;
386                 }
387         }
388 ?>