6 * Operators describing special searches.
8 abstract final class Operator
10 /// No operator selected.
12 /// Search for POI of the given type.
14 /// Search for POIs near the given place.
16 /// Search for POIS in the given place.
18 /// Search for POIS named as given.
20 /// Search for postcodes.
25 * Description of a single interpretation of a search query.
27 class SearchDescription
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.
47 /// Type of special feature to search for.
49 /// Housenumber of the object.
50 private $sHouseNumber = '';
51 /// Postcode for the object.
52 private $sPostcode = '';
53 /// Geographic search area.
54 private $oNearPoint = false;
56 // Temporary values used while creating the search description.
58 /// Index of phrase currently processed
59 private $iNamePhrase = -1;
61 public function getRank()
63 return $this->iSearchRank;
66 public function addToRank($iAddRank)
68 $this->iSearchRank += $iAddRank;
69 return $this->iSearchRank;
72 public function getPostCode()
74 return $this->sPostcode;
78 * Set the geographic search radius.
80 public function setNear(&$oNearPoint)
82 $this->oNearPoint = $oNearPoint;
85 public function setPoiSearch($iOperator, $sClass, $sType)
87 $this->iOperator = $iOperator;
88 $this->sClass = $sClass;
89 $this->sType = $sType;
93 * Check if name or address for the search are specified.
95 public function isNamedSearch()
97 return sizeof($this->aName) > 0 || sizeof($this->aAddress) > 0;
101 * Check if only a country is requested.
103 public function isCountrySearch()
105 return $this->sCountryCode && sizeof($this->aName) == 0
106 && !$this->iOperator && !$this->oNear;
110 * Check if a search near a geographic location is requested.
112 public function isNearSearch()
114 return (bool) $this->oNear;
117 public function isPoiSearch()
119 return (bool) $this->sClass;
122 public function looksLikeFullAddress()
124 return sizeof($this->aName)
125 && (sizeof($this->aAddress || $this->sCountryCode))
126 && preg_match('/[0-9]+/', $this->sHouseNumber);
129 public function isOperator($iType)
131 return $this->iOperator == $iType;
134 public function hasHouseNumber()
136 return (bool) $this->sHouseNumber;
139 private function poiTable()
141 return 'place_classtype_'.$this->sClass.'_'.$this->sType;
144 public function countryCodeSQL($sVar, $sCountryList)
146 if ($this->sCountryCode) {
147 return $sVar.' = \''.$this->sCountryCode."'";
150 return $sVar.' in ('.$this->sCountryCode.')';
156 public function hasOperator()
158 return $this->iOperator != Operator::NONE;
162 * Extract special terms from the query, amend the search
163 * and return the shortended query.
165 * Only the first special term found will be used but all will
166 * be removed from the query.
168 public function extractKeyValuePairs($sQuery)
170 // Search for terms of kind [<key>=<value>].
172 '/\\[([\\w_]*)=([\\w_]*)\\]/',
178 foreach ($aSpecialTermsRaw as $aTerm) {
179 $sQuery = str_replace($aTerm[0], ' ', $sQuery);
180 if (!$this->hasOperator()) {
181 $this->setPoiSearch(Operator::TYPE, $aTerm[1], $aTerm[2]);
188 public function isValidSearch(&$aCountryCodes)
190 if (!sizeof($this->aName)) {
191 if ($this->sHouseNumber) {
196 && $this->sCounrtyCode
197 && !in_array($this->sCountryCode, $aCountryCodes)
205 /////////// Search building functions
207 public function extendWithFullTerm($aSearchTerm, $bWordInQuery, $bHasPartial, $sPhraseType, $bFirstToken, $bFirstPhrase, $bLastToken, &$iGlobalRank)
209 $aNewSearches = array();
211 if (($sPhraseType == '' || $sPhraseType == 'country')
212 && !empty($aSearchTerm['country_code'])
213 && $aSearchTerm['country_code'] != '0'
215 if (!$this->sCountryCode) {
216 $oSearch = clone $this;
217 $oSearch->iSearchRank++;
218 $oSearch->sCountryCode = $aSearchTerm['country_code'];
219 // Country is almost always at the end of the string
220 // - increase score for finding it anywhere else (optimisation)
222 $oSearch->iSearchRank += 5;
224 $aNewSearches[] = $oSearch;
226 // If it is at the beginning, we can be almost sure that
227 // the terms are in the wrong order. Increase score for all searches.
232 } elseif (($sPhraseType == '' || $sPhraseType == 'postalcode')
233 && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'postcode'
235 // We need to try the case where the postal code is the primary element
236 // (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode)
238 if (!$this->sPostcode && $bWordInQuery) {
239 // If we have structured search or this is the first term,
240 // make the postcode the primary search element.
241 if ($this->iOperator == Operator::NONE
242 && ($sPhraseType == 'postalcode' || $bFirstToken)
244 $oSearch = clone $this;
245 $oSearch->iSearchRank++;
246 $oSearch->iOperator = Operator::POSTCODE;
247 $oSearch->aAddress = array_merge($this->aAddress, $this->aName);
249 array($aSearchTerm['word_id'] => $aSearchTerm['word']);
250 $aNewSearches[] = $oSearch;
253 // If we have a structured search or this is not the first term,
254 // add the postcode as an addendum.
255 if ($this->iOperator != Operator::POSTCODE
256 && ($sPhraseType == 'postalcode' || sizeof($this->aName))
258 $oSearch = clone $this;
259 $oSearch->iSearchRank++;
260 $oSearch->sPostcode = $aSearchTerm['word'];
261 $aNewSearches[] = $oSearch;
264 } elseif (($sPhraseType == '' || $sPhraseType == 'street')
265 && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house'
267 if (!$this->sHouseNumber && $this->iOperator != Operator::POSTCODE) {
268 $oSearch = clone $this;
269 $oSearch->iSearchRank++;
270 $oSearch->sHouseNumber = trim($aSearchTerm['word_token']);
271 // sanity check: if the housenumber is not mainly made
272 // up of numbers, add a penalty
273 if (preg_match_all("/[^0-9]/", $oSearch->sHouseNumber, $aMatches) > 2) {
274 $oSearch->iSearchRank++;
276 // also must not appear in the middle of the address
277 if (sizeof($this->aAddress) || sizeof($this->aAddressNonSearch)) {
278 $oSearch->iSearchRank++;
280 $aNewSearches[] = $oSearch;
282 } elseif ($sPhraseType == ''
283 && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null
285 // require a normalized exact match of the term
286 // if we have the normalizer version of the query
288 if ($this->iOperator == Operator::NONE
289 && (isset($aSearchTerm['word']) && $aSearchTerm['word'])
292 $oSearch = clone this;
293 $oSearch->iSearchRank++;
295 $iOp = Operator::NEAR; // near == in for the moment
296 if ($aSearchTerm['operator'] == '') {
297 if (sizeof($this->aName)) {
298 $iOp = Operator::NAME;
300 $oSearch->iSearchRank += 2;
303 $oSearch->setPoiSearch($iOp, $aSearchTerm['class'], $aSearchTerm['type']);
304 $aNewWordsetSearches[] = $oSearch;
306 } elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
307 $iWordID = $aSearchTerm['word_id'];
308 if (sizeof($this->aName)) {
309 if (($sPhraseType == '' || !$bFirstPhrase)
310 && $sPhraseType != 'country'))
313 $oSearch = clone $this;
314 $oSearch->iSearchRank++;
315 $oSearch->aAddress[$iWordID] = $iWordID;
317 $aNewSearches[] = $oSearch;
320 $this->aFullNameAddress[$iWordID] = $iWordID;
323 $oSearch = clone $this;
324 $oSearch->iSearchRank++;
325 $oSearch->aName = array($iWordID => $iWordID);
326 $aNewSearches[] = $oSearch;
330 return $aNewSearches;
333 public function extendWithPartialTerm($aSearchTerm, $bStructuredPhrases, $iPhrase, &$aWordFrequencyScores, $aFullTokens)
335 // Only allow name terms.
336 if (!(isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])) {
340 $aNewSearches = array();
341 $iWordID = $aSearchTerm['word_id'];
343 if ((!$bStructuredPhrases || $iPhrase > 0)
344 && sizeof($this->aName)
345 && strpos($aSearchTerm['word_token'], ' ') === false
347 if ($aWordFrequencyScores[$iWordID] < CONST_Max_Word_Frequency) {
348 $oSearch = clone this;
349 $oSearch->iSearchRank++;
350 $oSearch->aAddress[$iWordID] = $iWordID;
351 $aNewSearches[] = $oSearch;
353 $oSearch = clone this;
354 $oSearch->iSearchRank++;
355 $oSearch->aAddressNonSearch[$iWordID] = $iWordID;
356 if (preg_match('#^[0-9]+$#', $aSearchTerm['word_token'])) {
357 $oSearch->iSearchRank += 2;
359 if (sizeof($aFullTokens) {
360 $oSearch->iSearchRank++;
362 $aNewSearches[] = $oSearch;
364 // revert to the token version?
365 foreach ($aFullTokens as $aSearchTermToken) {
366 if (empty($aSearchTermToken['country_code'])
367 && empty($aSearchTermToken['lat'])
368 && empty($aSearchTermToken['class'])
370 $oSearch = clone $this;
371 $oSearch->iSearchRank++;
372 $oSearch->aAddress[$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
373 $aNewSearches[] = $oSearch;
379 if ((!$this->sPostcode && !$this->aAddress && !$this->aAddressNonSearch)
380 && (!sizeof($this->aName) || $this->iNamePhrase == $iPhrase)
382 $oSearch = clone $this;
383 $oSearch->iSearchRank++;
384 if (!sizeof($this->aName)) {
385 $aSearch->iSearchRank += 1;
387 if (preg_match('#^[0-9]+$#', $sSerchTerm['word_token')) {
388 $oSearch->iSearchRank += 2;
390 if ($aWordFrequencyScores[$iWordID] < CONST_Max_Word_Frequency) {
391 $oSearch->aName[$iWordID] = $iWordID;
393 $aSearch->aNameNonSearch[$iWordID] = $iWordID;
395 $oSearch->iNamePhrase = $iPhrase;
396 $aNewSearches[] = $aSearch;
399 return $aNewSearches;
402 /////////// Query functions
404 public function queryCountry(&$oDB, $sViewboxSQL)
406 $sSQL = 'SELECT place_id FROM placex ';
407 $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
408 $sSQL .= ' AND rank_search = 4';
410 $sSQL .= " AND ST_Intersects($sViewboxSQL, geometry)";
412 $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
414 if (CONST_Debug) var_dump($sSQL);
416 return chksql($oDB->getCol($sSQL));
419 public function queryNearbyPoi(&$oDB, $sCountryList, $sViewboxSQL, $sViewboxCentreSQL, $sExcludeSQL, $iLimit)
421 if (!$this->sClass) {
425 $sPoiTable = $this->poiTable();
427 $sSQL = 'SELECT count(*) FROM pg_tables WHERE tablename = \''.$sPoiTable."'";
428 if (chksql($oDB->getOne($sSQL))) {
429 $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
431 $sSQL .= ' JOIN placex USING (place_id)';
433 if ($this->oNearPoint) {
434 $sSQL .= ' WHERE '.$this->oNearPoint->withinSQL('ct.centroid');
436 $sSQL .= " WHERE ST_Contains($sViewboxSQL, ct.centroid)";
439 $sSQL .= " AND country_code in ($sCountryList)";
442 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
444 if ($sViewboxCentreSQL) {
445 $sSQL .= " ORDER BY ST_Distance($sViewboxCentreSQL, ct.centroid) ASC";
446 } elseif ($this->oNearPoint) {
447 $sSQL .= ' ORDER BY '.$this->oNearPoint->distanceSQL('ct.centroid').' ASC';
449 $sSQL .= " limit $iLimit";
450 if (CONST_Debug) var_dump($sSQL);
451 return chksql($this->oDB->getCol($sSQL));
454 if ($this->oNearPoint) {
455 $sSQL = 'SELECT place_id FROM placex WHERE ';
456 $sSQL .= 'class=\''.$this->sClass."' and type='".$this->sType."'";
457 $sSQL .= ' AND '.$this->oNearPoint->withinSQL('geometry');
458 $sSQL .= ' AND linked_place_id is null';
460 $sSQL .= " AND country_code in ($sCountryList)";
462 $sSQL .= ' ORDER BY '.$this->oNearPoint->distanceSQL('centroid')." ASC";
463 $sSQL .= " LIMIT $iLimit";
464 if (CONST_Debug) var_dump($sSQL);
465 return chksql($this->oDB->getCol($sSQL));
471 public function queryPostcode(&$oDB, $sCountryList, $iLimit)
473 $sSQL = 'SELECT p.place_id FROM location_postcode p ';
475 if (sizeof($this->aAddress)) {
476 $sSQL .= ', search_name s ';
477 $sSQL .= 'WHERE s.place_id = p.parent_place_id ';
478 $sSQL .= 'AND array_cat(s.nameaddress_vector, s.name_vector)';
479 $sSQL .= ' @> '.getArraySQL($this->aAddress).' AND ';
484 $sSQL .= "p.postcode = '".pg_escape_string(reset($this->$aName))."'";
485 $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
487 $sSQL .= ' AND '.$sCountyTerm;
489 $sSQL .= " LIMIT $iLimit";
491 if (CONST_Debug) var_dump($sSQL);
493 return chksql($this->oDB->getCol($sSQL));
496 public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $sCountryList, $iMinAddressRank, $iMaxAddressRank, $sExcludeSQL, $sViewboxSmall, $sViewboxLarge, $iLimit)
501 if ($this->sHouseNumber && sizeof($this->aAddress)) {
502 $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
504 $aOrder[0] .= 'EXISTS(';
505 $aOrder[0] .= ' SELECT place_id';
506 $aOrder[0] .= ' FROM placex';
507 $aOrder[0] .= ' WHERE parent_place_id = search_name.place_id';
508 $aOrder[0] .= " AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
509 $aOrder[0] .= ' LIMIT 1';
511 // also housenumbers from interpolation lines table are needed
512 if (preg_match('/[0-9]+/', $this->sHouseNumber)) {
513 $iHouseNumber = intval($this->sHouseNumber);
514 $aOrder[0] .= 'OR EXISTS(';
515 $aOrder[0] .= ' SELECT place_id ';
516 $aOrder[0] .= ' FROM location_property_osmline ';
517 $aOrder[0] .= ' WHERE parent_place_id = search_name.place_id';
518 $aOrder[0] .= ' AND startnumber is not NULL';
519 $aOrder[0] .= ' AND '.$iHouseNumber.'>=startnumber ';
520 $aOrder[0] .= ' AND '.$iHouseNumber.'<=endnumber ';
521 $aOrder[0] .= ' LIMIT 1';
524 $aOrder[0] .= ') DESC';
527 if (sizeof($this->aName)) {
528 $aTerms[] = 'name_vector @> '.getArraySQL($this->aName);
530 if (sizeof($this->aAddress)) {
531 // For infrequent name terms disable index usage for address
532 if (CONST_Search_NameOnlySearchFrequencyThreshold
533 && sizeof($this->aName) == 1
534 && $aWordFrequencyScores[$this->aName[reset($this->aName)]]
535 < CONST_Search_NameOnlySearchFrequencyThreshold
537 $aTerms[] = 'array_cat(nameaddress_vector,ARRAY[]::integer[]) @> '.getArraySQL($this->aAddress);
539 $aTerms[] = 'nameaddress_vector @> '.getArraySQL($this->aAddress);
543 $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
545 $aTerms[] = $sCountryTerm;
548 if ($this->sHouseNumber) {
549 $aTerms[] = "address_rank between 16 and 27";
550 } elseif (!$this->sClass || $this->iOperator == Operator::NAME) {
551 if ($iMinAddressRank > 0) {
552 $aTerms[] = "address_rank >= ".$iMinAddressRank;
554 if ($iMaxAddressRank < 30) {
555 $aTerms[] = "address_rank <= ".$iMaxAddressRank;
559 if ($this->oNearPoint) {
560 $aTerms[] = $this->oNearPoint->withinSQL('centroid');
561 $aOrder[] = $this->oNearPoint->distanceSQL('centroid');
562 } elseif ($this->sPostcode) {
563 if (!sizeof($this->aAddress)) {
564 $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
566 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$this->sPostcode."')";
571 $aTerms = 'place_id not in ('.$sExcludeSQL.')';
574 if ($sViewboxSmall) {
575 $aTerms[] = 'centroid && '.$sViewboxSmall;
578 if ($this->oNearPoint) {
579 $aOrder[] = $this->oNearPoint->distanceSQL('centroid');
582 if ($this->sHouseNumber) {
583 $sImportanceSQL = '- abs(26 - address_rank) + 3';
585 $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
587 if ($sViewboxSmall) {
588 $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxSmall, centroid) THEN 1 ELSE 0.5 END";
590 if ($sViewboxLarge) {
591 $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxLarge, centroid) THEN 1 ELSE 0.5 END";
593 $aOrder[] = "$sImportanceSQL DESC";
595 if (sizeof($this->aFullNameAddress)) {
596 $sExactMatchSQL = ' ( ';
597 $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
598 $sExactMatchSQL .= ' SELECT unnest('.getArraySQL($this->aFullNameAddress).')';
599 $sExactMatchSQL .= ' INTERSECT ';
600 $sExactMatchSQL .= ' SELECT unnest(nameaddress_vector)';
601 $sExactMatchSQL .= ' ) s';
602 $sExactMatchSQL .= ') as exactmatch';
603 $aOrder[] = 'exactmatch DESC';
605 $sExactMatchSQL = '0::int as exactmatch';
608 if ($this->sHouseNumber || $this->sClass) {
612 if (sizeof($aTerms)) {
613 $sSQL = 'SELECT place_id,'.$sExactMatchSQL;
614 $sSQL .= ' FROM search_name';
615 $sSQL .= ' WHERE '.join(' and ', $aTerms);
616 $sSQL .= ' ORDER BY '.join(', ', $aOrder);
617 $sSQL .= ' LIMIT '.$iLimit;
619 if (CONST_Debug) var_dump($sSQL);
622 $this->oDB->getAll($sSQL),
623 "Could not get places for search terms."
631 public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $sExcludeSQL, $iLimit)
633 $sPlaceIDs = join(',', $aRoadPlaceIDs);
635 $sHouseNumberRegex = '\\\\m'.$this->sHouseNumber.'\\\\M';
636 $sSQL = 'SELECT place_id FROM placex ';
637 $sSQL .= 'WHERE parent_place_id in ('.$sPlaceIDs.')';
638 $sSQL .= " AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
640 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
642 $sSQL .= " LIMIT $iLimit";
644 if (CONST_Debug) var_dump($sSQL);
646 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
648 if (sizeof($aPlaceIDs)) {
649 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => -1);
652 $bIsIntHouseNumber= (bool) preg_match('/[0-9]+/', $this->sHouseNumber);
653 $iHousenumber = intval($this->sHouseNumber);
654 if ($bIsIntHouseNumber) {
655 // if nothing found, search in the interpolation line table
656 $sSQL = 'SELECT distinct place_id FROM location_property_osmline';
657 $sSQL .= ' WHERE startnumber is not NULL';
658 $sSQL .= ' AND parent_place_id in ('.$sPlaceIDs.') AND (';
659 if ($iHousenumber % 2 == 0) {
660 // If housenumber is even, look for housenumber in streets
661 // with interpolationtype even or all.
662 $sSQL .= "interpolationtype='even'";
664 // Else look for housenumber with interpolationtype odd or all.
665 $sSQL .= "interpolationtype='odd'";
667 $sSQL .= " or interpolationtype='all') and ";
668 $sSQL .= $iHousenumber.">=startnumber and ";
669 $sSQL .= $iHousenumber."<=endnumber";
672 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
674 $sSQL .= " limit $iLimit";
676 if (CONST_Debug) var_dump($sSQL);
678 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
680 if (sizeof($aPlaceIDs)) {
681 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => $iHousenumber);
685 // If nothing found try the aux fallback table
686 if (CONST_Use_Aux_Location_data) {
687 $sSQL = 'SELECT place_id FROM location_property_aux';
688 $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.')';
689 $sSQL .= " AND housenumber = '".$this->sHouseNumber."'";
691 $sSQL .= " AND place_id not in ($sExcludeSQL)";
693 $sSQL .= " limit $iLimit";
695 if (CONST_Debug) var_dump($sSQL);
697 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
699 if (sizeof($aPlaceIDs)) {
700 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => -1);
704 // If nothing found then search in Tiger data (location_property_tiger)
705 if (CONST_Use_US_Tiger_Data && $bIsIntHouseNumber) {
706 $sSQL = 'SELECT distinct place_id FROM location_property_tiger';
707 $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.') and (';
708 if ($iHousenumber % 2 == 0) {
709 $sSQL .= "interpolationtype='even'";
711 $sSQL .= "interpolationtype='odd'";
713 $sSQL .= " or interpolationtype='all') and ";
714 $sSQL .= $iHousenumber.">=startnumber and ";
715 $sSQL .= $iHousenumber."<=endnumber";
718 $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
720 $sSQL .= " limit $iLimit";
722 if (CONST_Debug) var_dump($sSQL);
724 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
726 if (sizeof($aPlaceIDs)) {
727 return array('aPlaceIDs' => $aPlaceIDs, 'iHouseNumber' => $iHousenumber);
735 public function queryPoiByOperator(&$oDB, $aParentIDs, $sExcludeSQL, $iLimit)
737 $sPlaceIDs = join(',', $aParentIDs);
738 $aClassPlaceIDs = array();
740 if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NAME) {
741 // If they were searching for a named class (i.e. 'Kings Head pub')
742 // then we might have an extra match
743 $sSQL = 'SELECT place_id FROM placex ';
744 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
745 $sSQL .= " AND class='".$this->sClass."' ";
746 $sSQL .= " AND type='".$this->sType."'";
747 $sSQL .= " AND linked_place_id is null";
748 $sSQL .= " ORDER BY rank_search ASC ";
749 $sSQL .= " LIMIT $iLimit";
751 if (CONST_Debug) var_dump($sSQL);
753 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
756 // NEAR and IN are handled the same
757 if ($this->iOperator == Operator::TYPE || $this->iOperator == Operator::NEAR) {
758 $sClassTable = $this->poiTable();
759 $sSQL = "SELECT count(*) FROM pg_tables WHERE tablename = '$sClassTable'";
760 $bCacheTable = (bool) chksql($this->oDB->getOne($sSQL));
762 $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
763 if (CONST_Debug) var_dump($sSQL);
764 $iMaxRank = (int)chksql($this->oDB->getOne($sSQL));
766 // For state / country level searches the normal radius search doesn't work very well
768 if ($iMaxRank < 9 && $bCacheTable) {
769 // Try and get a polygon to search in instead
770 $sSQL = 'SELECT geometry FROM placex';
771 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
772 $sSQL .= " AND rank_search < $iMaxRank + 5";
773 $sSQL .= " AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')";
774 $sSQL .= " ORDER BY rank_search ASC ";
776 if (CONST_Debug) var_dump($sSQL);
777 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
784 $sSQL = 'SELECT place_id FROM placex';
785 $sSQL .= " WHERE place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
786 if (CONST_Debug) var_dump($sSQL);
787 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
788 $sPlaceIDs = join(',', $aPlaceIDs);
791 if ($sPlaceIDs || $sPlaceGeom) {
794 // More efficient - can make the range bigger
798 if ($this->oNearPoint) {
799 $sOrderBySQL = $this->oNearPoint->distanceSQL('l.centroid');
800 } elseif ($sPlaceIDs) {
801 $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
802 } elseif ($sPlaceGeom) {
803 $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
806 $sSQL = 'SELECT distinct i.place_id';
808 $sSQL .= ', i.order_term';
810 $sSQL .= ' from (SELECT l.place_id';
812 $sSQL .= ','.$sOrderBySQL.' as order_term';
814 $sSQL .= ' from '.$sClassTable.' as l';
817 $sSQL .= ",placex as f WHERE ";
818 $sSQL .= "f.place_id in ($sPlaceIDs) ";
819 $sSQL .= " AND ST_DWithin(l.centroid, f.centroid, $fRange)";
820 } elseif ($sPlaceGeom) {
821 $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
825 $sSQL .= ' AND l.place_id not in ('.$sExcludeSQL.')';
827 $sSQL .= 'limit 300) i ';
829 $sSQL .= 'order by order_term asc';
831 $sSQL .= " limit $iLimit";
833 if (CONST_Debug) var_dump($sSQL);
835 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
837 if ($this->oNearPoint) {
838 $fRange = $this->oNearPoint->radius();
842 if ($this->oNearPoint) {
843 $sOrderBySQL = $this->oNearPoint->distanceSQL('l.geometry');
845 $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
848 $sSQL = 'SELECT distinct l.place_id';
850 $sSQL .= ','.$sOrderBySQL.' as orderterm';
852 $sSQL .= ' FROM placex as l, placex as f';
853 $sSQL .= " WHERE f.place_id in ($sPlaceIDs)";
854 $sSQL .= " AND ST_DWithin(l.geometry, f.centroid, $fRange)";
855 $sSQL .= " AND l.class='".$this->sClass."'";
856 $sSQL .= " AND l.type='".$this->sType."'";
858 $sSQL .= " AND l.place_id not in (".$sExcludeSQL.")";
861 $sSQL .= "ORDER BY orderterm ASC";
863 $sSQL .= " limit $iLimit";
865 if (CONST_Debug) var_dump($sSQL);
867 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
872 return $aClassPlaceIDs;