]> git.openstreetmap.org Git - nominatim.git/blob - lib/SearchDescription.php
add function to convert array to SQL
[nominatim.git] / lib / SearchDescription.php
1 <?php
2
3 namespace Nominatim;
4
5 /**
6  * Operators describing special searches.
7  */
8 abstract final class Operator
9 {
10     /// No operator selected.
11     const NONE = 0;
12     /// Search for POI of the given type.
13     const TYPE = 1;
14     /// Search for POIs near the given place.
15     const NEAR = 2;
16     /// Search for POIS in the given place.
17     const IN = 3;
18     /// Search for POIS named as given.
19     const NAME = 4;
20     /// Search for postcodes.
21     const POSTCODE = 5;
22 }
23
24 /**
25  * Description of a single interpretation of a search query.
26  */
27 class SearchDescription
28 {
29     /// Ranking how well the description fits the query.
30     private $iSearchRank = 0;
31     /// Country code of country the result must belong to.
32     private $sCountryCode = '';
33     /// List of word ids making up the name of the object.
34     private $aName = array();
35     /// List of word ids making up the address of the object.
36     private $aAddress = array();
37     /// Subset of word ids of full words making up the address.
38     private $aFullNameAddress = array();
39     /// List of word ids that appear in the name but should be ignored.
40     private $aNameNonSearch = array();
41     /// List of word ids that appear in the address but should be ignored.
42     private $aAddressNonSearch = array();
43     /// Kind of search for special searches, see Nominatim::Operator.
44     private $iOperator = Operator::NONE;
45     /// Class of special feature to search for.
46     private $sClass = '';
47     /// Type of special feature to search for.
48     private $sType = '';
49     /// Housenumber of the object.
50     private $sHouseNumber = '';
51     /// Postcode for the object.
52     private $sPostcode = '';
53     /// Geographic search area.
54     private $oNearPoint = false;
55
56     // Temporary values used while creating the search description.
57
58     /// Index of phrase currently processed
59     private $iNamePhrase = -1;
60
61     public function getRank()
62     {
63         return $this->iSearchRank;
64     }
65
66     public function getPostCode()
67     {
68         return $this->sPostcode;
69     }
70
71     /**
72      * Set the geographic search radius.
73      */
74     public function setNear(&$oNearPoint)
75     {
76         $this->oNearPoint = $oNearPoint;
77     }
78
79     public function setPoiSearch($iOperator, $sClass, $sType)
80     {
81         $this->iOperator = $iOperator;
82         $this->sClass = $sClass;
83         $this->sType = $sType;
84     }
85
86     /**
87      * Check if name or address for the search are specified.
88      */
89     public function isNamedSearch()
90     {
91         return sizeof($this->aName) > 0 || sizeof($this->aAddress) > 0;
92     }
93
94     /**
95      * Check if only a country is requested.
96      */
97     public function isCountrySearch()
98     {
99         return $this->sCountryCode && sizeof($this->aName) == 0
100                && !$this->iOperator && !$this->oNear;
101     }
102
103     /**
104      * Check if a search near a geographic location is requested.
105      */
106     public function isNearSearch()
107     {
108         return (bool) $this->oNear;
109     }
110
111     public function isPoiSearch()
112     {
113         return (bool) $this->sClass;
114     }
115
116     public function looksLikeFullAddress()
117     {
118         return sizeof($this->aName)
119                && (sizeof($this->aAddress || $this->sCountryCode))
120                && preg_match('/[0-9]+/', $this->sHouseNumber);
121     }
122
123     public function isOperator($iType)
124     {
125         return $this->iOperator == $iType;
126     }
127
128     public function hasHouseNumber()
129     {
130         return (bool) $this->sHouseNumber;
131     }
132
133     private function poiTable()
134     {
135         return 'place_classtype_'.$this->sClass.'_'.$this->sType;
136     }
137
138     public function countryCodeSQL($sVar, $sCountryList)
139     {
140         if ($this->sCountryCode) {
141             return $sVar.' = \''.$this->sCountryCode."'";
142         }
143         if ($sCountryList) {
144             return $sVar.' in ('.$this->sCountryCode.')';
145         }
146
147         return '';
148     }
149
150     public function hasOperator()
151     {
152         return $this->iOperator != Operator::NONE;
153     }
154
155     /**
156      * Extract special terms from the query, amend the search
157      * and return the shortended query.
158      *
159      * Only the first special term found will be used but all will
160      * be removed from the query.
161      */
162     public function extractKeyValuePairs($sQuery)
163     {
164         // Search for terms of kind [<key>=<value>].
165         preg_match_all(
166             '/\\[([\\w_]*)=([\\w_]*)\\]/',
167             $sQuery,
168             $aSpecialTermsRaw,
169             PREG_SET_ORDER
170         );
171
172         foreach ($aSpecialTermsRaw as $aTerm) {
173             $sQuery = str_replace($aTerm[0], ' ', $sQuery);
174             if (!$this->hasOperator()) {
175                 $this->setPoiSearch(Operator::TYPE, $aTerm[1], $aTerm[2]);
176             }
177         }
178
179         return $sQuery;
180     }
181
182     public function queryCountry(&$oDB, $sViewboxSQL)
183     {
184         $sSQL = 'SELECT place_id FROM placex ';
185         $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
186         $sSQL .= ' AND rank_search = 4';
187         if ($ViewboxSQL) {
188             $sSQL .= " AND ST_Intersects($sViewboxSQL, geometry)";
189         }
190         $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
191
192         if (CONST_Debug) var_dump($sSQL);
193
194         return chksql($oDB->getCol($sSQL));
195     }
196
197     public function queryNearbyPoi(&$oDB, $sCountryList, $sViewboxSQL, $sViewboxCentreSQL, $sExcludeSQL, $iLimit)
198     {
199         if (!$this->sClass) {
200             return array();
201         }
202
203         $sPoiTable = $this->poiTable();
204
205         $sSQL = 'SELECT count(*) FROM pg_tables WHERE tablename = \''.$sPoiTable."'";
206         if (chksql($oDB->getOne($sSQL))) {
207             $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
208             if ($sCountryList) {
209                 $sSQL .= ' JOIN placex USING (place_id)';
210             }
211             if ($this->oNearPoint) {
212                 $sSQL .= ' WHERE '.$this->oNearPoint->withinSQL('ct.centroid');
213             } else {
214                 $sSQL .= " WHERE ST_Contains($sViewboxSQL, ct.centroid)";
215             }
216             if ($sCountryList) {
217                 $sSQL .= " AND country_code in ($sCountryList)";
218             }
219             if ($sExcludeSQL) {
220                 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
221             }
222             if ($sViewboxCentreSQL) {
223                 $sSQL .= " ORDER BY ST_Distance($sViewboxCentreSQL, ct.centroid) ASC";
224             } elseif ($this->oNearPoint) {
225                 $sSQL .= ' ORDER BY '.$this->oNearPoint->distanceSQL('ct.centroid').' ASC';
226             }
227             $sSQL .= " limit $iLimit";
228             if (CONST_Debug) var_dump($sSQL);
229             return chksql($this->oDB->getCol($sSQL));
230         }
231
232         if ($this->oNearPoint) {
233             $sSQL = 'SELECT place_id FROM placex WHERE ';
234             $sSQL .= 'class=\''.$this->sClass."' and type='".$this->sType."'";
235             $sSQL .= ' AND '.$this->oNearPoint->withinSQL('geometry');
236             $sSQL .= ' AND linked_place_id is null';
237             if ($sCountryList) {
238                 $sSQL .= " AND country_code in ($sCountryList)";
239             }
240             $sSQL .= ' ORDER BY '.$this->oNearPoint->distanceSQL('centroid')." ASC";
241             $sSQL .= " LIMIT $iLimit";
242             if (CONST_Debug) var_dump($sSQL);
243             return chksql($this->oDB->getCol($sSQL));
244         }
245
246         return array();
247     }
248
249     public function queryPostcode(&$oDB, $sCountryList, $iLimit)
250     {
251         $sSQL  = 'SELECT p.place_id FROM location_postcode p ';
252
253         if (sizeof($this->aAddress)) {
254             $sSQL .= ', search_name s ';
255             $sSQL .= 'WHERE s.place_id = p.parent_place_id ';
256             $sSQL .= 'AND array_cat(s.nameaddress_vector, s.name_vector)';
257             $sSQL .= '      @> '.getArraySQL($this->aAddress).' AND ';
258         } else {
259             $sSQL .= 'WHERE ';
260         }
261
262         $sSQL .= "p.postcode = '".pg_escape_string(reset($this->$aName))."'";
263         $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
264         if ($sCountryTerm) {
265             $sSQL .= ' AND '.$sCountyTerm;
266         }
267         $sSQL .= " LIMIT $iLimit";
268
269         if (CONST_Debug) var_dump($sSQL);
270
271         return chksql($this->oDB->getCol($sSQL));
272     }
273
274     public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $sCountryList, $iMinAddressRank, $iMaxAddressRank, $sExcludeSQL, $sViewboxSmall, $sViewboxLarge, $iLimit)
275     {
276         $aTerms = array();
277         $aOrder = array();
278
279         if ($this->sHouseNumber && sizeof($this->aAddress)) {
280             $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
281             $aOrder[] = ' (';
282             $aOrder[0] .= 'EXISTS(';
283             $aOrder[0] .= '  SELECT place_id';
284             $aOrder[0] .= '  FROM placex';
285             $aOrder[0] .= '  WHERE parent_place_id = search_name.place_id';
286             $aOrder[0] .= "    AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
287             $aOrder[0] .= '  LIMIT 1';
288             $aOrder[0] .= ') ';
289             // also housenumbers from interpolation lines table are needed
290             if (preg_match('/[0-9]+/', $this->sHouseNumber)) {
291                 $iHouseNumber = intval($this->sHouseNumber);
292                 $aOrder[0] .= 'OR EXISTS(';
293                 $aOrder[0] .= '  SELECT place_id ';
294                 $aOrder[0] .= '  FROM location_property_osmline ';
295                 $aOrder[0] .= '  WHERE parent_place_id = search_name.place_id';
296                 $aOrder[0] .= '    AND startnumber is not NULL';
297                 $aOrder[0] .= '    AND '.$iHouseNumber.'>=startnumber ';
298                 $aOrder[0] .= '    AND '.$iHouseNumber.'<=endnumber ';
299                 $aOrder[0] .= '  LIMIT 1';
300                 $aOrder[0] .= ')';
301             }
302             $aOrder[0] .= ') DESC';
303         }
304
305         if (sizeof($this->aName)) {
306             $aTerms[] = 'name_vector @> '.getArraySQL($this->aName);
307         }
308         if (sizeof($this->aAddress)) {
309             // For infrequent name terms disable index usage for address
310             if (CONST_Search_NameOnlySearchFrequencyThreshold
311                 && sizeof($this->aName) == 1
312                 && $aWordFrequencyScores[$this->aName[reset($this->aName)]]
313                      < CONST_Search_NameOnlySearchFrequencyThreshold
314             ) {
315                 $aTerms[] = 'array_cat(nameaddress_vector,ARRAY[]::integer[]) @> '.getArraySQL($this->aAddress);
316             } else {
317                 $aTerms[] = 'nameaddress_vector @> '.getArraySQL($this->aAddress);
318             }
319         }
320
321         $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
322         if ($sCountryTerm) {
323             $aTerms[] = $sCountryTerm;
324         }
325
326         if ($this->sHouseNumber) {
327             $aTerms[] = "address_rank between 16 and 27";
328         } elseif (!$this->sClass || $this->iOperator == Operator::NAME) {
329             if ($iMinAddressRank > 0) {
330                 $aTerms[] = "address_rank >= ".$iMinAddressRank;
331             }
332             if ($iMaxAddressRank < 30) {
333                 $aTerms[] = "address_rank <= ".$iMaxAddressRank;
334             }
335         }
336
337         if ($this->oNearPoint) {
338             $aTerms[] = $this->oNearPoint->withinSQL('centroid');
339             $aOrder[] = $this->oNearPoint->distanceSQL('centroid');
340         } elseif ($this->sPostcode) {
341             if (!sizeof($this->aAddress)) {
342                 $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
343             } else {
344                 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."')";
345             }
346         }
347
348         if ($sExcludeSQL) {
349             $aTerms = 'place_id not in ('.$sExcludeSQL.')';
350         }
351
352         if ($sViewboxSmall) {
353            $aTerms[] = 'centroid && '.$sViewboxSmall;
354         }
355
356         if ($this->oNearPoint) {
357             $aOrder[] = $this->oNearPoint->distanceSQL('centroid');
358         }
359
360         if ($this->sHouseNumber) {
361             $sImportanceSQL = '- abs(26 - address_rank) + 3';
362         } else {
363             $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
364         }
365         if ($sViewboxSmall) {
366             $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxSmall, centroid) THEN 1 ELSE 0.5 END";
367         }
368         if ($sViewboxLarge) {
369             $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxLarge, centroid) THEN 1 ELSE 0.5 END";
370         }
371         $aOrder[] = "$sImportanceSQL DESC";
372
373         if (sizeof($this->aFullNameAddress)) {
374             $sExactMatchSQL = ' ( ';
375             $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
376             $sExactMatchSQL .= '  SELECT unnest('.getArraySQL($this->aFullNameAddress).')';
377             $sExactMatchSQL .= '    INTERSECT ';
378             $sExactMatchSQL .= '  SELECT unnest(nameaddress_vector)';
379             $sExactMatchSQL .= ' ) s';
380             $sExactMatchSQL .= ') as exactmatch';
381             $aOrder[] = 'exactmatch DESC';
382         } else {
383             $sExactMatchSQL = '0::int as exactmatch';
384         }
385
386         if ($this->sHouseNumber || $this->sClass) {
387             $iLimit = 20;
388         }
389
390         if (sizeof($aTerms)) {
391             $sSQL = 'SELECT place_id,'.$sExactMatchSQL;
392             $sSQL .= ' FROM search_name';
393             $sSQL .= ' WHERE '.join(' and ', $aTerms);
394             $sSQL .= ' ORDER BY '.join(', ', $aOrder);
395             $sSQL .= ' LIMIT '.$iLimit;
396
397             if (CONST_Debug) var_dump($sSQL);
398
399             return chksql(
400                 $this->oDB->getAll($sSQL),
401                 "Could not get places for search terms."
402             );
403         }
404
405         return array();
406     }
407
408
409     public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $sExcludeSQL, $iLimit)
410     {
411         $sPlaceIDs = join(',', $aRoadPlaceIDs);
412
413         $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
414         $sSQL = 'SELECT place_id FROM placex ';
415         $sSQL .= 'WHERE parent_place_id in ('.$sPlaceIDs.')';
416         $sSQL .= "  AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
417         if ($sExcludeSQL) {
418             $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
419         }
420         $sSQL .= " LIMIT $iLimit";
421
422         if (CONST_Debug) var_dump($sSQL);
423
424         $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
425
426         if (sizeof($aPlaceIDs)) {
427             return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => -1);
428         }
429
430         $bIsIntHouseNumber= (bool) preg_match('/[0-9]+/', $this->sHouseNumber);
431         $iHousenumber = intval($this->sHouseNumber);
432         if ($bIsIntHouseNumber) {
433             // if nothing found, search in the interpolation line table
434             $sSQL = 'SELECT distinct place_id FROM location_property_osmline';
435             $sSQL .= ' WHERE startnumber is not NULL';
436             $sSQL .= '  AND parent_place_id in ('.$sPlaceIDs.') AND (';
437             if ($iHousenumber % 2 == 0) {
438                 // If housenumber is even, look for housenumber in streets
439                 // with interpolationtype even or all.
440                 $sSQL .= "interpolationtype='even'";
441             } else {
442                 // Else look for housenumber with interpolationtype odd or all.
443                 $sSQL .= "interpolationtype='odd'";
444             }
445             $sSQL .= " or interpolationtype='all') and ";
446             $sSQL .= $iHousenumber.">=startnumber and ";
447             $sSQL .= $iHousenumber."<=endnumber";
448
449             if ($sExcludeSQL)) {
450                 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
451             }
452             $sSQL .= " limit $iLimit";
453
454             if (CONST_Debug) var_dump($sSQL);
455
456             $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
457
458             if (sizeof($aPlaceIDs)) {
459                 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => $iHousenumber);
460             }
461         }
462
463         // If nothing found try the aux fallback table
464         if (CONST_Use_Aux_Location_data) {
465             $sSQL = 'SELECT place_id FROM location_property_aux';
466             $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.')';
467             $sSQL .= " AND housenumber = '".$this->sHouseNumber."'";
468             if ($sExcludeSQL) {
469                 $sSQL .= " AND place_id not in ($sExcludeSQL)";
470             }
471             $sSQL .= " limit $iLimit";
472
473             if (CONST_Debug) var_dump($sSQL);
474
475             $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
476
477             if (sizeof($aPlaceIDs)) {
478                 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => -1);
479             }
480         }
481
482         // If nothing found then search in Tiger data (location_property_tiger)
483         if (CONST_Use_US_Tiger_Data && $bIsIntHouseNumber) {
484             $sSQL = 'SELECT distinct place_id FROM location_property_tiger';
485             $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.') and (';
486             if ($iHousenumber % 2 == 0) {
487                 $sSQL .= "interpolationtype='even'";
488             } else {
489                 $sSQL .= "interpolationtype='odd'";
490             }
491             $sSQL .= " or interpolationtype='all') and ";
492             $sSQL .= $iHousenumber.">=startnumber and ";
493             $sSQL .= $iHousenumber."<=endnumber";
494
495             if ($sExcludeSQL) {
496                 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
497             }
498             $sSQL .= " limit $iLimit";
499
500             if (CONST_Debug) var_dump($sSQL);
501
502             $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
503
504             if (sizeof($aPlaceIDs)) {
505                 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => $iHousenumber);
506             }
507         }
508
509         return array();
510     }
511
512
513     public function queryPoiByOperator(&$oDB, $aParentIDs, $sExcludeSQL, $iLimit)
514     {
515         $sPlaceIDs = join(',', $aParentIDs);
516         $aClassPlaceIDs = array();
517
518         if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NAME) {
519             // If they were searching for a named class (i.e. 'Kings Head pub')
520             // then we might have an extra match
521             $sSQL = 'SELECT place_id FROM placex ';
522             $sSQL .= " WHERE place_id in ($sPlaceIDs)";
523             $sSQL .= "   AND class='".$this->sClass."' ";
524             $sSQL .= "   AND type='".$this->sType."'";
525             $sSQL .= "   AND linked_place_id is null";
526             $sSQL .= " ORDER BY rank_search ASC ";
527             $sSQL .= " LIMIT $iLimit";
528
529             if (CONST_Debug) var_dump($sSQL);
530
531             $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
532         }
533
534         // NEAR and IN are handled the same
535         if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NEAR) {
536             $sClassTable = $this->poiTable();
537             $sSQL = "SELECT count(*) FROM pg_tables WHERE tablename = '$sClassTable'";
538             $bCacheTable = (bool) chksql($this->oDB->getOne($sSQL));
539
540             $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
541             if (CONST_Debug) var_dump($sSQL);
542             $iMaxRank = (int)chksql($this->oDB->getOne($sSQL));
543
544             // For state / country level searches the normal radius search doesn't work very well
545             $sPlaceGeom = false;
546             if ($iMaxRank < 9 && $bCacheTable) {
547                 // Try and get a polygon to search in instead
548                 $sSQL = 'SELECT geometry FROM placex';
549                 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
550                 $sSQL .= "   AND rank_search < $iMaxRank + 5";
551                 $sSQL .= "   AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')";
552                 $sSQL .= " ORDER BY rank_search ASC ";
553                 $sSQL .= " LIMIT 1";
554                 if (CONST_Debug) var_dump($sSQL);
555                 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
556             }
557
558             if ($sPlaceGeom) {
559                 $sPlaceIDs = false;
560             } else {
561                 $iMaxRank += 5;
562                 $sSQL = 'SELECT place_id FROM placex';
563                 $sSQL .= " WHERE place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
564                 if (CONST_Debug) var_dump($sSQL);
565                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
566                 $sPlaceIDs = join(',', $aPlaceIDs);
567             }
568
569             if ($sPlaceIDs || $sPlaceGeom) {
570                 $fRange = 0.01;
571                 if ($bCacheTable) {
572                     // More efficient - can make the range bigger
573                     $fRange = 0.05;
574
575                     $sOrderBySQL = '';
576                     if ($this->oNearPoint) {
577                         $sOrderBySQL = $this->oNearPoint->distanceSQL('l.centroid');
578                     } elseif ($sPlaceIDs) {
579                         $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
580                     } elseif ($sPlaceGeom) {
581                         $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
582                     }
583
584                     $sSQL = 'SELECT distinct i.place_id';
585                     if ($sOrderBySQL) {
586                         $sSQL .= ', i.order_term';
587                     }
588                     $sSQL .= ' from (SELECT l.place_id';
589                     if ($sOrderBySQL) {
590                         $sSQL .= ','.$sOrderBySQL.' as order_term';
591                     }
592                     $sSQL .= ' from '.$sClassTable.' as l';
593
594                     if ($sPlaceIDs) {
595                         $sSQL .= ",placex as f WHERE ";
596                         $sSQL .= "f.place_id in ($sPlaceIDs) ";
597                         $sSQL .= " AND ST_DWithin(l.centroid, f.centroid, $fRange)";
598                     } elseif ($sPlaceGeom) {
599                         $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
600                     }
601
602                     if ($sExcludeSQL) {
603                         $sSQL .= ' AND l.place_id not in ('.$sExcludeSQL.')';
604                     }
605                     $sSQL .= 'limit 300) i ';
606                     if ($sOrderBySQL) {
607                         $sSQL .= 'order by order_term asc';
608                     }
609                     $sSQL .= " limit $iLimit";
610
611                     if (CONST_Debug) var_dump($sSQL);
612
613                     $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
614                 } else {
615                     if ($this->oNearPoint) {
616                         $fRange = $this->oNearPoint->radius();
617                     }
618
619                     $sOrderBySQL = '';
620                     if ($this->oNearPoint) {
621                         $sOrderBySQL = $this->oNearPoint->distanceSQL('l.geometry');
622                     } else {
623                         $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
624                     }
625
626                     $sSQL = 'SELECT distinct l.place_id';
627                     if ($sOrderBySQL) {
628                         $sSQL .= ','.$sOrderBySQL.' as orderterm';
629                     }
630                     $sSQL .= ' FROM placex as l, placex as f';
631                     $sSQL .= " WHERE f.place_id in ($sPlaceIDs)";
632                     $sSQL .= "  AND ST_DWithin(l.geometry, f.centroid, $fRange)";
633                     $sSQL .= "  AND l.class='".$this->sClass."'";
634                     $sSQL .= "  AND l.type='".$this->sType."'";
635                     if ($sExcludeSQL) {
636                         $sSQL .= " AND l.place_id not in (".$sExcludeSQL.")";
637                     }
638                     if ($sOrderBySQL) {
639                         $sSQL .= "ORDER BY orderterm ASC";
640                     }
641                     $sSQL .= " limit $iLimit";
642
643                     if (CONST_Debug) var_dump($sSQL);
644
645                     $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
646                 }
647             }
648         }
649
650         return $aClassPlaceIDs;
651     }
652 };