]> git.openstreetmap.org Git - nominatim.git/blob - lib/PlaceLookup.php
add loadParamArray function to PlaceLookup and use for reverse
[nominatim.git] / lib / PlaceLookup.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/Result.php');
6
7 class PlaceLookup
8 {
9     protected $oDB;
10
11     protected $aLangPrefOrderSql = "''";
12
13     protected $bAddressDetails = false;
14     protected $bExtraTags = false;
15     protected $bNameDetails = false;
16
17     protected $bIncludePolygonAsPoints = false;
18     protected $bIncludePolygonAsText = false;
19     protected $bIncludePolygonAsGeoJSON = false;
20     protected $bIncludePolygonAsKML = false;
21     protected $bIncludePolygonAsSVG = false;
22     protected $fPolygonSimplificationThreshold = 0.0;
23
24     protected $sAnchorSql = null;
25     protected $sAddressRankListSql = null;
26     protected $sAllowedTypesSQLList = null;
27     protected $bDeDupe = true;
28
29
30     public function __construct(&$oDB)
31     {
32         $this->oDB =& $oDB;
33     }
34
35     public function loadParamArray($oParams)
36     {
37         $aLangs = $oParams->getPreferredLanguages();
38         $this->aLangPrefOrderSql =
39             'ARRAY['.join(',', array_map('getDBQuoted', $aLangs)).']';
40
41         $this->bExtraTags = $oParams->getBool('extratags', false);
42         $this->bNameDetails = $oParams->getBool('namedetails', false);
43
44         $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
45         $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
46         $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
47         $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
48         $this->fPolygonSimplificationThreshold
49             = $oParams->getFloat('polygon_threshold', 0.0);
50
51         $iWantedTypes =
52             ($this->bIncludePolygonAsText ? 1 : 0) +
53             ($this->bIncludePolygonAsGeoJSON ? 1 : 0) +
54             ($this->bIncludePolygonAsKML ? 1 : 0) +
55             ($this->bIncludePolygonAsSVG ? 1 : 0);
56         if ($iWantedTypes > CONST_PolygonOutput_MaximumTypes) {
57             if (CONST_PolygonOutput_MaximumTypes) {
58                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
59             } else {
60                 userError("Polygon output is disabled");
61             }
62         }
63     }
64
65     public function setAnchorSql($sPoint)
66     {
67         $this->sAnchorSql = $sPoint;
68     }
69
70     public function setDeDupe($bDeDupe)
71     {
72         $this->bDeDupe = $bDeDupe;
73     }
74
75     public function setAddressRankList($aList)
76     {
77         $this->sAddressRankListSql = '('.join(',', $aList).')';
78     }
79
80     public function setAllowedTypesSQLList($sSql)
81     {
82         $this->sAllowedTypesSQLList = $sSql;
83     }
84
85     public function setLanguagePreference($aLangPrefOrder)
86     {
87         $this->aLangPrefOrderSql =
88             'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
89     }
90
91     public function setIncludeAddressDetails($bAddressDetails = true)
92     {
93         $this->bAddressDetails = $bAddressDetails;
94     }
95
96     public function setIncludeExtraTags($bExtraTags = false)
97     {
98         $this->bExtraTags = $bExtraTags;
99     }
100
101     public function setIncludeNameDetails($bNameDetails = false)
102     {
103         $this->bNameDetails = $bNameDetails;
104     }
105
106     public function setIncludePolygonAsPoints($b = true)
107     {
108         $this->bIncludePolygonAsPoints = $b;
109     }
110
111     public function setIncludePolygonAsText($b = true)
112     {
113         $this->bIncludePolygonAsText = $b;
114     }
115
116     public function setIncludePolygonAsGeoJSON($b = true)
117     {
118         $this->bIncludePolygonAsGeoJSON = $b;
119     }
120
121     public function setIncludePolygonAsKML($b = true)
122     {
123         $this->bIncludePolygonAsKML = $b;
124     }
125
126     public function setIncludePolygonAsSVG($b = true)
127     {
128         $this->bIncludePolygonAsSVG = $b;
129     }
130
131     public function setPolygonSimplificationThreshold($f)
132     {
133         $this->fPolygonSimplificationThreshold = $f;
134     }
135
136     private function addressImportanceSql($sGeometry, $sPlaceId)
137     {
138         if ($this->sAnchorSql) {
139             $sSQL = 'ST_Distance('.$this->sAnchorSql.','.$sGeometry.')';
140         } else {
141             $sSQL = '(SELECT max(ai_p.importance * (ai_p.rank_address + 2))';
142             $sSQL .= '   FROM place_addressline ai_s, placex ai_p';
143             $sSQL .= '   WHERE ai_s.place_id = '.$sPlaceId;
144             $sSQL .= '     AND ai_p.place_id = ai_s.address_place_id ';
145             $sSQL .= '     AND ai_s.isaddress ';
146             $sSQL .= '     AND ai_p.importance is not null)';
147         }
148
149         return $sSQL.' AS addressimportance,';
150     }
151
152     private function langAddressSql($sHousenumber)
153     {
154         return 'get_address_by_language(place_id,'.$sHousenumber.','.$this->aLangPrefOrderSql.') AS langaddress,';
155     }
156
157     public function lookupOSMID($sType, $iID)
158     {
159         $sSQL = "select place_id from placex where osm_type = '".$sType."' and osm_id = ".$iID;
160         $iPlaceID = chksql($this->oDB->getOne($sSQL));
161
162         if (!$iPlaceID) {
163             return null;
164         }
165
166         $aResults = $this->lookup(array($iPlaceID => new Result($iPlaceID)));
167
168         return sizeof($aResults) ? reset($aResults) : null;
169     }
170
171     public function lookup($aResults, $iMinRank = 0, $iMaxRank = 30)
172     {
173         if (!sizeof($aResults)) {
174             return array();
175         }
176         $aSubSelects = array();
177
178         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
179         if (CONST_Debug) var_dump('PLACEX', $sPlaceIDs);
180         if ($sPlaceIDs) {
181             $sSQL  = 'SELECT ';
182             $sSQL .= '    osm_type,';
183             $sSQL .= '    osm_id,';
184             $sSQL .= '    class,';
185             $sSQL .= '    type,';
186             $sSQL .= '    admin_level,';
187             $sSQL .= '    rank_search,';
188             $sSQL .= '    rank_address,';
189             $sSQL .= '    min(place_id) AS place_id,';
190             $sSQL .= '    min(parent_place_id) AS parent_place_id,';
191             $sSQL .= '    housenumber,';
192             $sSQL .= '    country_code,';
193             $sSQL .= $this->langAddressSql('-1');
194             $sSQL .= '    get_name_by_language(name,'.$this->aLangPrefOrderSql.') AS placename,';
195             $sSQL .= "    get_name_by_language(name, ARRAY['ref']) AS ref,";
196             if ($this->bExtraTags) {
197                 $sSQL .= 'hstore_to_json(extratags)::text AS extra,';
198             }
199             if ($this->bNameDetails) {
200                 $sSQL .= 'hstore_to_json(name)::text AS names,';
201             }
202             $sSQL .= '    avg(ST_X(centroid)) AS lon, ';
203             $sSQL .= '    avg(ST_Y(centroid)) AS lat, ';
204             $sSQL .= '    COALESCE(importance,0.75-(rank_search::float/40)) AS importance, ';
205             $sSQL .= $this->addressImportanceSql(
206                 'ST_Collect(centroid)',
207                 'min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END)'
208             );
209             $sSQL .= "    (extratags->'place') AS extra_place ";
210             $sSQL .= ' FROM placex';
211             $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
212             $sSQL .= '   AND (';
213             $sSQL .= "        placex.rank_address between $iMinRank and $iMaxRank ";
214             if (14 >= $iMinRank && 14 <= $iMaxRank) {
215                 $sSQL .= "    OR (extratags->'place') = 'city'";
216             }
217             if ($this->sAddressRankListSql) {
218                 $sSQL .= '    OR placex.rank_address in '.$this->sAddressRankListSql;
219             }
220             $sSQL .= "       ) ";
221             if ($this->sAllowedTypesSQLList) {
222                 $sSQL .= 'AND placex.class in '.$this->sAllowedTypesSQLList;
223             }
224             $sSQL .= '    AND linked_place_id is null ';
225             $sSQL .= ' GROUP BY ';
226             $sSQL .= '     osm_type, ';
227             $sSQL .= '     osm_id, ';
228             $sSQL .= '     class, ';
229             $sSQL .= '     type, ';
230             $sSQL .= '     admin_level, ';
231             $sSQL .= '     rank_search, ';
232             $sSQL .= '     rank_address, ';
233             $sSQL .= '     housenumber,';
234             $sSQL .= '     country_code, ';
235             $sSQL .= '     importance, ';
236             if (!$this->bDeDupe) $sSQL .= 'place_id,';
237             $sSQL .= '     langaddress, ';
238             $sSQL .= '     placename, ';
239             $sSQL .= '     ref, ';
240             if ($this->bExtraTags) $sSQL .= 'extratags, ';
241             if ($this->bNameDetails) $sSQL .= 'name, ';
242             $sSQL .= "     extratags->'place' ";
243
244             $aSubSelects[] = $sSQL;
245         }
246
247         // postcode table
248         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_POSTCODE);
249         if ($sPlaceIDs) {
250             $sSQL = 'SELECT';
251             $sSQL .= "  'P' as osm_type,";
252             $sSQL .= "  (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,";
253             $sSQL .= "  'place' as class, 'postcode' as type,";
254             $sSQL .= '  null as admin_level, rank_search, rank_address,';
255             $sSQL .= '  place_id, parent_place_id,';
256             $sSQL .= '  null as housenumber,';
257             $sSQL .= '  country_code,';
258             $sSQL .= $this->langAddressSql('-1');
259             $sSQL .= "  postcode as placename,";
260             $sSQL .= "  postcode as ref,";
261             if ($this->bExtraTags) $sSQL .= "null AS extra,";
262             if ($this->bNameDetails) $sSQL .= "null AS names,";
263             $sSQL .= "  ST_x(geometry) AS lon, ST_y(geometry) AS lat,";
264             $sSQL .= "  (0.75-(rank_search::float/40)) AS importance, ";
265             $sSQL .= $this->addressImportanceSql('geometry', 'lp.parent_place_id');
266             $sSQL .= "  null AS extra_place ";
267             $sSQL .= "FROM location_postcode lp";
268             $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
269             $sSQL .= "   AND lp.rank_address between $iMinRank and $iMaxRank";
270
271             $aSubSelects[] = $sSQL;
272         }
273
274         // All other tables are rank 30 only.
275         if ($iMaxRank == 30) {
276             // TIGER table
277             if (CONST_Use_US_Tiger_Data) {
278                 $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_TIGER);
279                 if ($sPlaceIDs) {
280                     $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_TIGER);
281                     // Tiger search only if a housenumber was searched and if it was found
282                     // (realized through a join)
283                     $sSQL = " SELECT ";
284                     $sSQL .= "     'T' AS osm_type, ";
285                     $sSQL .= "     (SELECT osm_id from placex p WHERE p.place_id=blub.parent_place_id) as osm_id, ";
286                     $sSQL .= "     'place' AS class, ";
287                     $sSQL .= "     'house' AS type, ";
288                     $sSQL .= '     null AS admin_level, ';
289                     $sSQL .= '     30 AS rank_search, ';
290                     $sSQL .= '     30 AS rank_address, ';
291                     $sSQL .= '     place_id, ';
292                     $sSQL .= '     parent_place_id, ';
293                     $sSQL .= '     housenumber_for_place as housenumber,';
294                     $sSQL .= "     'us' AS country_code, ";
295                     $sSQL .= $this->langAddressSql('housenumber_for_place');
296                     $sSQL .= "     null AS placename, ";
297                     $sSQL .= "     null AS ref, ";
298                     if ($this->bExtraTags) $sSQL .= "null AS extra,";
299                     if ($this->bNameDetails) $sSQL .= "null AS names,";
300                     $sSQL .= "     st_x(centroid) AS lon, ";
301                     $sSQL .= "     st_y(centroid) AS lat,";
302                     $sSQL .= "     -1.15 AS importance, ";
303                     $sSQL .= $this->addressImportanceSql('centroid', 'blub.parent_place_id');
304                     $sSQL .= "     null AS extra_place ";
305                     $sSQL .= " FROM (";
306                     $sSQL .= "     SELECT place_id, ";    // interpolate the Tiger housenumbers here
307                     $sSQL .= "         ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) AS centroid, ";
308                     $sSQL .= "         parent_place_id, ";
309                     $sSQL .= "         housenumber_for_place";
310                     $sSQL .= "     FROM (";
311                     $sSQL .= "            location_property_tiger ";
312                     $sSQL .= "            JOIN (values ".$sHousenumbers.") AS housenumbers(place_id, housenumber_for_place) USING(place_id)) ";
313                     $sSQL .= "     WHERE ";
314                     $sSQL .= "         housenumber_for_place >= startnumber";
315                     $sSQL .= "         AND housenumber_for_place <= endnumber";
316                     $sSQL .= " ) AS blub"; //postgres wants an alias here
317
318                     $aSubSelects[] = $sSQL;
319                 }
320             }
321
322             // osmline - interpolated housenumbers
323             $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_OSMLINE);
324             if ($sPlaceIDs) {
325                 $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_OSMLINE);
326                 // interpolation line search only if a housenumber was searched
327                 // (realized through a join)
328                 $sSQL = "SELECT ";
329                 $sSQL .= "  'W' AS osm_type, ";
330                 $sSQL .= "  osm_id, ";
331                 $sSQL .= "  'place' AS class, ";
332                 $sSQL .= "  'house' AS type, ";
333                 $sSQL .= '  15 AS admin_level, ';
334                 $sSQL .= '  30 AS rank_search, ';
335                 $sSQL .= '  30 AS rank_address, ';
336                 $sSQL .= '  place_id, ';
337                 $sSQL .= '  parent_place_id, ';
338                 $sSQL .= '  housenumber_for_place as housenumber,';
339                 $sSQL .= '  country_code, ';
340                 $sSQL .= $this->langAddressSql('housenumber_for_place');
341                 $sSQL .= '  null AS placename, ';
342                 $sSQL .= '  null AS ref, ';
343                 if ($this->bExtraTags) $sSQL .= 'null AS extra, ';
344                 if ($this->bNameDetails) $sSQL .= 'null AS names, ';
345                 $sSQL .= '  st_x(centroid) AS lon, ';
346                 $sSQL .= '  st_y(centroid) AS lat, ';
347                 // slightly smaller than the importance for normal houses
348                 $sSQL .= "  -0.1 AS importance, ";
349                 $sSQL .= $this->addressImportanceSql('centroid', 'blub.parent_place_id');
350                 $sSQL .= "  null AS extra_place ";
351                 $sSQL .= "  FROM (";
352                 $sSQL .= "     SELECT ";
353                 $sSQL .= "         osm_id, ";
354                 $sSQL .= "         place_id, ";
355                 $sSQL .= "         country_code, ";
356                 $sSQL .= "         CASE ";             // interpolate the housenumbers here
357                 $sSQL .= "           WHEN startnumber != endnumber ";
358                 $sSQL .= "           THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
359                 $sSQL .= "           ELSE ST_LineInterpolatePoint(linegeo, 0.5) ";
360                 $sSQL .= "         END as centroid, ";
361                 $sSQL .= "         parent_place_id, ";
362                 $sSQL .= "         housenumber_for_place ";
363                 $sSQL .= "     FROM (";
364                 $sSQL .= "            location_property_osmline ";
365                 $sSQL .= "            JOIN (values ".$sHousenumbers.") AS housenumbers(place_id, housenumber_for_place) USING(place_id)";
366                 $sSQL .= "          ) ";
367                 $sSQL .= "     WHERE housenumber_for_place >= 0 ";
368                 $sSQL .= "  ) as blub"; //postgres wants an alias here
369
370                 $aSubSelects[] = $sSQL;
371             }
372
373             if (CONST_Use_Aux_Location_data) {
374                 $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_AUX);
375                 if ($sPlaceIDs) {
376                     $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_AUX);
377                     $sSQL = "  SELECT ";
378                     $sSQL .= "     'L' AS osm_type, ";
379                     $sSQL .= "     place_id AS osm_id, ";
380                     $sSQL .= "     'place' AS class,";
381                     $sSQL .= "     'house' AS type, ";
382                     $sSQL .= '     null AS admin_level, ';
383                     $sSQL .= '     30 AS rank_search,';
384                     $sSQL .= '     30 AS rank_address, ';
385                     $sSQL .= '     place_id,';
386                     $sSQL .= '     parent_place_id, ';
387                     $sSQL .= '     housenumber,';
388                     $sSQL .= "     'us' AS country_code, ";
389                     $sSQL .= $this->langAddressSql('-1');
390                     $sSQL .= "     null AS placename, ";
391                     $sSQL .= "     null AS ref, ";
392                     if ($this->bExtraTags) $sSQL .= "null AS extra, ";
393                     if ($this->bNameDetails) $sSQL .= "null AS names, ";
394                     $sSQL .= "     ST_X(centroid) AS lon, ";
395                     $sSQL .= "     ST_Y(centroid) AS lat, ";
396                     $sSQL .= "     -1.10 AS importance, ";
397                     $sSQL .= $this->addressImportanceSql(
398                         'centroid',
399                         'location_property_aux.parent_place_id'
400                     );
401                     $sSQL .= "     null AS extra_place ";
402                     $sSQL .= "  FROM location_property_aux ";
403                     $sSQL .= "  WHERE place_id in ($sPlaceIDs) ";
404
405                     $aSubSelects[] = $sSQL;
406                 }
407             }
408         }
409
410         if (CONST_Debug) var_dump($aSubSelects);
411
412         if (!sizeof($aSubSelects)) {
413             return array();
414         }
415
416         $aPlaces = chksql(
417             $this->oDB->getAll(join(' UNION ', $aSubSelects)),
418             "Could not lookup place"
419         );
420
421         $aClassType = getClassTypes();
422         foreach ($aPlaces as &$aPlace) {
423             if ($this->bAddressDetails) {
424                 // to get addressdetails for tiger data, the housenumber is needed
425                 $aPlace['aAddress'] = $this->getAddressNames(
426                     $aPlace['place_id'],
427                     $aPlace['housenumber']
428                 );
429             }
430
431             if ($this->bExtraTags) {
432                 if ($aPlace['extra']) {
433                     $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
434                 } else {
435                     $aPlace['sExtraTags'] = (object) array();
436                 }
437             }
438
439             if ($this->bNameDetails) {
440                 if ($aPlace['names']) {
441                     $aPlace['sNameDetails'] = json_decode($aPlace['names']);
442                 } else {
443                     $aPlace['sNameDetails'] = (object) array();
444                 }
445             }
446
447             $sAddressType = '';
448             $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
449             if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel'])) {
450                 $sAddressType = $aClassType[$aClassType]['simplelabel'];
451             } else {
452                 $sClassType = $aPlace['class'].':'.$aPlace['type'];
453                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
454                     $sAddressType = $aClassType[$sClassType]['simplelabel'];
455                 else $sAddressType = $aPlace['class'];
456             }
457
458             $aPlace['addresstype'] = $sAddressType;
459         }
460
461         if (CONST_Debug) var_dump($aPlaces);
462
463         return $aPlaces;
464     }
465
466     private function getAddressDetails($iPlaceID, $bAll, $sHousenumber)
467     {
468         $sSQL = 'SELECT *,';
469         $sSQL .= '  get_name_by_language(name,'.$this->aLangPrefOrderSql.') as localname';
470         $sSQL .= ' FROM get_addressdata('.$iPlaceID.','.$sHousenumber.')';
471         if (!$bAll) {
472             $sSQL .= " WHERE isaddress OR type = 'country_code'";
473         }
474         $sSQL .= ' ORDER BY rank_address desc,isaddress DESC';
475
476         return chksql($this->oDB->getAll($sSQL));
477     }
478
479     public function getAddressNames($iPlaceID, $sHousenumber = null)
480     {
481         $aAddressLines = $this->getAddressDetails(
482             $iPlaceID,
483             false,
484             $sHousenumber === null ? -1 : $sHousenumber
485         );
486
487         $aAddress = array();
488         $aFallback = array();
489         $aClassType = getClassTypes();
490         foreach ($aAddressLines as $aLine) {
491             $bFallback = false;
492             $aTypeLabel = false;
493             if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) {
494                 $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
495             } elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) {
496                 $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
497             } elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))])) {
498                 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
499                 $bFallback = true;
500             } else {
501                 $aTypeLabel = array('simplelabel' => 'address'.$aLine['rank_address']);
502                 $bFallback = true;
503             }
504             if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber']))) {
505                 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
506                 $sTypeLabel = str_replace(' ', '_', $sTypeLabel);
507                 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place') {
508                     $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
509                 }
510                 $aFallback[$sTypeLabel] = $bFallback;
511             }
512         }
513         return $aAddress;
514     }
515
516
517
518     /* returns an array which will contain the keys
519      *   aBoundingBox
520      * and may also contain one or more of the keys
521      *   asgeojson
522      *   askml
523      *   assvg
524      *   astext
525      *   lat
526      *   lon
527      */
528
529
530     public function getOutlines($iPlaceID, $fLon = null, $fLat = null, $fRadius = null)
531     {
532
533         $aOutlineResult = array();
534         if (!$iPlaceID) return $aOutlineResult;
535
536         if (CONST_Search_AreaPolygons) {
537             // Get the bounding box and outline polygon
538             $sSQL  = "select place_id,0 as numfeatures,st_area(geometry) as area,";
539             $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
540             $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
541             $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
542             if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
543             if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
544             if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
545             if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
546             $sFrom = " from placex where place_id = ".$iPlaceID;
547             if ($this->fPolygonSimplificationThreshold > 0) {
548                 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
549             } else {
550                 $sSQL .= $sFrom;
551             }
552
553             $aPointPolygon = chksql($this->oDB->getRow($sSQL), "Could not get outline");
554
555             if ($aPointPolygon['place_id']) {
556                 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null) {
557                     $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
558                     $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
559                 }
560
561                 if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
562                 if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
563                 if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
564                 if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
565                 if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
566
567
568                 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) {
569                     $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
570                     $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
571                 }
572
573                 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001) {
574                     $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
575                     $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
576                 }
577
578                 $aOutlineResult['aBoundingBox'] = array(
579                                                    (string)$aPointPolygon['minlat'],
580                                                    (string)$aPointPolygon['maxlat'],
581                                                    (string)$aPointPolygon['minlon'],
582                                                    (string)$aPointPolygon['maxlon']
583                                                   );
584             }
585         }
586
587         // as a fallback we generate a bounding box without knowing the size of the geometry
588         if ((!isset($aOutlineResult['aBoundingBox'])) && isset($fLon)) {
589             //
590             if ($this->bIncludePolygonAsPoints) {
591                 $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
592                 $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
593             }
594
595             $aBounds = array();
596             $aBounds['minlat'] = $fLat - $fRadius;
597             $aBounds['maxlat'] = $fLat + $fRadius;
598             $aBounds['minlon'] = $fLon - $fRadius;
599             $aBounds['maxlon'] = $fLon + $fRadius;
600
601             $aOutlineResult['aBoundingBox'] = array(
602                                                (string)$aBounds['minlat'],
603                                                (string)$aBounds['maxlat'],
604                                                (string)$aBounds['minlon'],
605                                                (string)$aBounds['maxlon']
606                                               );
607         }
608         return $aOutlineResult;
609     }
610 }