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