6 protected $aLangPrefOrder = array();
8 protected $bIncludeAddressDetails = false;
10 protected $bIncludePolygonAsPoints = false;
11 protected $bIncludePolygonAsText = false;
12 protected $bIncludePolygonAsGeoJSON = false;
13 protected $bIncludePolygonAsKML = false;
14 protected $bIncludePolygonAsSVG = false;
16 protected $aExcludePlaceIDs = array();
17 protected $bDeDupe = true;
18 protected $bReverseInPlan = false;
20 protected $iLimit = 20;
21 protected $iFinalLimit = 10;
22 protected $iOffset = 0;
23 protected $bFallback = false;
25 protected $aCountryCodes = false;
26 protected $aNearPoint = false;
28 protected $bBoundedSearch = false;
29 protected $aViewBox = false;
30 protected $sViewboxSmallSQL = false;
31 protected $sViewboxLargeSQL = false;
32 protected $aRoutePoints = false;
34 protected $iMaxRank = 20;
35 protected $iMinAddressRank = 0;
36 protected $iMaxAddressRank = 30;
37 protected $aAddressRankList = array();
38 protected $exactMatchCache = array();
40 protected $sAllowedTypesSQLList = false;
42 protected $sQuery = false;
43 protected $aStructuredQuery = false;
45 function Geocode(&$oDB)
50 function setReverseInPlan($bReverse)
52 $this->bReverseInPlan = $bReverse;
55 function setLanguagePreference($aLangPref)
57 $this->aLangPrefOrder = $aLangPref;
60 function setIncludeAddressDetails($bAddressDetails = true)
62 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
65 function getIncludeAddressDetails()
67 return $this->bIncludeAddressDetails;
70 function setIncludePolygonAsPoints($b = true)
72 $this->bIncludePolygonAsPoints = $b;
75 function getIncludePolygonAsPoints()
77 return $this->bIncludePolygonAsPoints;
80 function setIncludePolygonAsText($b = true)
82 $this->bIncludePolygonAsText = $b;
85 function getIncludePolygonAsText()
87 return $this->bIncludePolygonAsText;
90 function setIncludePolygonAsGeoJSON($b = true)
92 $this->bIncludePolygonAsGeoJSON = $b;
95 function setIncludePolygonAsKML($b = true)
97 $this->bIncludePolygonAsKML = $b;
100 function setIncludePolygonAsSVG($b = true)
102 $this->bIncludePolygonAsSVG = $b;
105 function setDeDupe($bDeDupe = true)
107 $this->bDeDupe = (bool)$bDeDupe;
110 function setLimit($iLimit = 10)
112 if ($iLimit > 50) $iLimit = 50;
113 if ($iLimit < 1) $iLimit = 1;
115 $this->iFinalLimit = $iLimit;
116 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
119 function setOffset($iOffset = 0)
121 $this->iOffset = $iOffset;
124 function setFallback($bFallback = true)
126 $this->bFallback = (bool)$bFallback;
129 function setExcludedPlaceIDs($a)
131 // TODO: force to int
132 $this->aExcludePlaceIDs = $a;
135 function getExcludedPlaceIDs()
137 return $this->aExcludePlaceIDs;
140 function setBounded($bBoundedSearch = true)
142 $this->bBoundedSearch = (bool)$bBoundedSearch;
145 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
147 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
150 function getViewBoxString()
152 if (!$this->aViewBox) return null;
153 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
156 function setRoute($aRoutePoints)
158 $this->aRoutePoints = $aRoutePoints;
161 function setFeatureType($sFeatureType)
163 switch($sFeatureType)
166 $this->setRankRange(4, 4);
169 $this->setRankRange(8, 8);
172 $this->setRankRange(14, 16);
175 $this->setRankRange(8, 20);
180 function setRankRange($iMin, $iMax)
182 $this->iMinAddressRank = (int)$iMin;
183 $this->iMaxAddressRank = (int)$iMax;
186 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
188 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
191 function setCountryCodesList($aCountryCodes)
193 $this->aCountryCodes = $aCountryCodes;
196 function setQuery($sQueryString)
198 $this->sQuery = $sQueryString;
199 $this->aStructuredQuery = false;
202 function getQueryString()
204 return $this->sQuery;
208 function loadParamArray($aParams)
210 if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
211 if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
212 if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
214 if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
215 if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
217 if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
219 // List of excluded Place IDs - used for more acurate pageing
220 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
222 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
224 $iExcludedPlaceID = (int)$iExcludedPlaceID;
225 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
227 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
230 // Only certain ranks of feature
231 if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
232 if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
235 if (isset($aParams['countrycodes']))
237 $aCountryCodes = array();
238 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
240 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
242 $aCountryCodes[] = strtolower($sCountryCode);
245 $this->aCountryCodes = $aCountryCodes;
248 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
250 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
251 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
253 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
255 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
256 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
259 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
261 $aPoints = explode(',',$aParams['route']);
262 if (sizeof($aPoints) % 2 != 0)
264 userError("Uneven number of points");
269 foreach($aPoints as $i => $fPoint)
273 $aRoute[] = array((float)$fPoint, $fPrevCoord);
277 $fPrevCoord = (float)$fPoint;
280 $this->aRoutePoints = $aRoute;
284 function setQueryFromParams($aParams)
287 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
290 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
291 $this->setReverseInPlan(false);
295 $this->setQuery($sQuery);
299 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
301 $sValue = trim($sValue);
302 if (!$sValue) return false;
303 $this->aStructuredQuery[$sKey] = $sValue;
304 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
306 $this->iMinAddressRank = $iNewMinAddressRank;
307 $this->iMaxAddressRank = $iNewMaxAddressRank;
309 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
313 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
315 $this->sQuery = false;
318 $this->iMinAddressRank = 0;
319 $this->iMaxAddressRank = 30;
320 $this->aAddressRankList = array();
322 $this->aStructuredQuery = array();
323 $this->sAllowedTypesSQLList = '';
325 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
326 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
327 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
328 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
329 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
330 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
331 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
333 if (sizeof($this->aStructuredQuery) > 0)
335 $this->sQuery = join(', ', $this->aStructuredQuery);
336 if ($this->iMaxAddressRank < 30)
338 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
343 function fallbackStructuredQuery()
345 if (!$this->aStructuredQuery) return false;
347 $aParams = $this->aStructuredQuery;
349 if (sizeof($aParams) == 1) return false;
351 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
353 foreach($aOrderToFallback as $sType)
355 if (isset($aParams[$sType]))
357 unset($aParams[$sType]);
358 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
366 function getDetails($aPlaceIDs)
368 if (sizeof($aPlaceIDs) == 0) return array();
370 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
372 // Get the details for display (is this a redundant extra step?)
373 $sPlaceIDs = join(',',$aPlaceIDs);
375 $sImportanceSQL = '';
376 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
377 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
379 $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as country_code,";
380 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
381 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
382 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
383 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
384 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
385 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
386 $sSQL .= "(extratags->'place') as extra_place ";
387 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
388 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
389 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
390 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
392 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
393 $sSQL .= "and linked_place_id is null ";
394 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
395 if (!$this->bDeDupe) $sSQL .= ",place_id";
396 $sSQL .= ",langaddress ";
397 $sSQL .= ",placename ";
399 $sSQL .= ",extratags->'place' ";
401 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
404 $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id,'us' as country_code,";
405 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
406 $sSQL .= "null as placename,";
407 $sSQL .= "null as ref,";
408 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
409 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
410 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
411 $sSQL .= "null as extra_place ";
412 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
413 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
414 $sSQL .= "group by place_id";
415 if (!$this->bDeDupe) $sSQL .= ",place_id ";
417 $sSQL .= "select 'L' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id,'us' as country_code,";
418 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
419 $sSQL .= "null as placename,";
420 $sSQL .= "null as ref,";
421 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
422 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
423 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
424 $sSQL .= "null as extra_place ";
425 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
426 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
427 $sSQL .= "group by place_id";
428 if (!$this->bDeDupe) $sSQL .= ",place_id";
429 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
432 $sSQL .= " order by importance desc";
433 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
434 $aSearchResults = $this->oDB->getAll($sSQL);
436 if (PEAR::IsError($aSearchResults))
438 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
441 return $aSearchResults;
444 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
447 Calculate all searches using aValidTokens i.e.
448 'Wodsworth Road, Sheffield' =>
452 0 1 (wodsworth)(road)
455 Score how good the search is so they can be ordered
457 foreach($aPhrases as $iPhrase => $sPhrase)
459 $aNewPhraseSearches = array();
460 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
461 else $sPhraseType = '';
463 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
465 // Too many permutations - too expensive
466 if ($iWordSet > 120) break;
468 $aWordsetSearches = $aSearches;
470 // Add all words from this wordset
471 foreach($aWordset as $iToken => $sToken)
473 //echo "<br><b>$sToken</b>";
474 $aNewWordsetSearches = array();
476 foreach($aWordsetSearches as $aCurrentSearch)
479 //var_dump($aCurrentSearch);
482 // If the token is valid
483 if (isset($aValidTokens[' '.$sToken]))
485 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
487 $aSearch = $aCurrentSearch;
488 $aSearch['iSearchRank']++;
489 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
491 if ($aSearch['sCountryCode'] === false)
493 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
494 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
495 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
497 $aSearch['iSearchRank'] += 5;
499 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
502 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
504 if ($aSearch['fLat'] === '')
506 $aSearch['fLat'] = $aSearchTerm['lat'];
507 $aSearch['fLon'] = $aSearchTerm['lon'];
508 $aSearch['fRadius'] = $aSearchTerm['radius'];
509 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
512 elseif ($sPhraseType == 'postalcode')
514 // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
515 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
517 // If we already have a name try putting the postcode first
518 if (sizeof($aSearch['aName']))
520 $aNewSearch = $aSearch;
521 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
522 $aNewSearch['aName'] = array();
523 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
524 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
527 if (sizeof($aSearch['aName']))
529 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
531 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
535 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
536 $aSearch['iSearchRank'] += 1000; // skip;
541 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
542 //$aSearch['iNamePhrase'] = $iPhrase;
544 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
548 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
550 if ($aSearch['sHouseNumber'] === '')
552 $aSearch['sHouseNumber'] = $sToken;
553 // sanity check: if the housenumber is not mainly made
554 // up of numbers, add a penalty
555 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
556 // also housenumbers should appear in the first or second phrase
557 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
558 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
560 // Fall back to not searching for this item (better than nothing)
561 $aSearch = $aCurrentSearch;
562 $aSearch['iSearchRank'] += 1;
563 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
567 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
569 if ($aSearch['sClass'] === '')
571 $aSearch['sOperator'] = $aSearchTerm['operator'];
572 $aSearch['sClass'] = $aSearchTerm['class'];
573 $aSearch['sType'] = $aSearchTerm['type'];
574 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
575 else $aSearch['sOperator'] = 'near'; // near = in for the moment
576 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
578 // Do we have a shortcut id?
579 if ($aSearch['sOperator'] == 'name')
581 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
582 if ($iAmenityID = $this->oDB->getOne($sSQL))
584 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
585 $aSearch['aName'][$iAmenityID] = $iAmenityID;
586 $aSearch['sClass'] = '';
587 $aSearch['sType'] = '';
590 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
593 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
595 if (sizeof($aSearch['aName']))
597 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
599 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
603 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
604 $aSearch['iSearchRank'] += 1000; // skip;
609 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
610 //$aSearch['iNamePhrase'] = $iPhrase;
612 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
616 // Look for partial matches.
617 // Note that there is no point in adding country terms here
618 // because country are omitted in the address.
619 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
621 // Allow searching for a word - but at extra cost
622 foreach($aValidTokens[$sToken] as $aSearchTerm)
624 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
626 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
628 $aSearch = $aCurrentSearch;
629 $aSearch['iSearchRank'] += 1;
630 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
632 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
633 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
635 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
637 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
638 $aSearch['iSearchRank'] += 1;
639 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
640 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
642 if (empty($aSearchTermToken['country_code'])
643 && empty($aSearchTermToken['lat'])
644 && empty($aSearchTermToken['class']))
646 $aSearch = $aCurrentSearch;
647 $aSearch['iSearchRank'] += 1;
648 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
649 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
655 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
656 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
657 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
661 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
663 $aSearch = $aCurrentSearch;
664 $aSearch['iSearchRank'] += 1;
665 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
666 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
667 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
668 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
670 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
671 $aSearch['iNamePhrase'] = $iPhrase;
672 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
679 // Allow skipping a word - but at EXTREAM cost
680 //$aSearch = $aCurrentSearch;
681 //$aSearch['iSearchRank']+=100;
682 //$aNewWordsetSearches[] = $aSearch;
686 usort($aNewWordsetSearches, 'bySearchRank');
687 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
689 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
691 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
692 usort($aNewPhraseSearches, 'bySearchRank');
694 $aSearchHash = array();
695 foreach($aNewPhraseSearches as $iSearch => $aSearch)
697 $sHash = serialize($aSearch);
698 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
699 else $aSearchHash[$sHash] = 1;
702 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
705 // Re-group the searches by their score, junk anything over 20 as just not worth trying
706 $aGroupedSearches = array();
707 foreach($aNewPhraseSearches as $aSearch)
709 if ($aSearch['iSearchRank'] < $this->iMaxRank)
711 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
712 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
715 ksort($aGroupedSearches);
718 $aSearches = array();
719 foreach($aGroupedSearches as $iScore => $aNewSearches)
721 $iSearchCount += sizeof($aNewSearches);
722 $aSearches = array_merge($aSearches, $aNewSearches);
723 if ($iSearchCount > 50) break;
726 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
729 return $aGroupedSearches;
733 /* Perform the actual query lookup.
735 Returns an ordered list of results, each with the following fields:
736 osm_type: type of corresponding OSM object
740 P - postcode (internally computed)
741 osm_id: id of corresponding OSM object
742 class: general object class (corresponds to tag key of primary OSM tag)
743 type: subclass of object (corresponds to tag value of primary OSM tag)
744 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
745 rank_search: rank in search hierarchy
746 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
747 rank_address: rank in address hierarchy (determines orer in address)
748 place_id: internal key (may differ between different instances)
749 country_code: ISO country code
750 langaddress: localized full address
751 placename: localized name of object
752 ref: content of ref tag (if available)
755 importance: importance of place based on Wikipedia link count
756 addressimportance: cumulated importance of address elements
757 extra_place: type of place (for admin boundaries, if there is a place tag)
758 aBoundingBox: bounding Box
759 label: short description of the object class/type (English only)
760 name: full name (currently the same as langaddress)
761 foundorder: secondary ordering for places with same importance
765 if (!$this->sQuery && !$this->aStructuredQuery) return false;
767 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
769 $sCountryCodesSQL = false;
770 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
772 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
775 $sQuery = $this->sQuery;
777 // Conflicts between US state abreviations and various words for 'the' in different languages
778 if (isset($this->aLangPrefOrder['name:en']))
780 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
781 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
782 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
786 $sViewboxCentreSQL = false;
787 $bBoundingBoxSearch = false;
790 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
791 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
792 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
793 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
794 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
795 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
797 $this->sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$this->aViewBox[0].",".(float)$this->aViewBox[1]."),ST_Point(".(float)$this->aViewBox[2].",".(float)$this->aViewBox[3].")),4326)";
798 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
799 $bBoundingBoxSearch = $this->bBoundedSearch;
803 if ($this->aRoutePoints)
805 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
807 foreach($this->aRoutePoints as $aPoint)
809 if (!$bFirst) $sViewboxCentreSQL .= ",";
810 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
813 $sViewboxCentreSQL .= ")'::geometry,4326)";
815 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
816 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
817 if (PEAR::isError($this->sViewboxSmallSQL))
819 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
821 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
823 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
824 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
825 if (PEAR::isError($this->sViewboxLargeSQL))
827 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
829 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
830 $bBoundingBoxSearch = $this->bBoundedSearch;
833 // Do we have anything that looks like a lat/lon pair?
834 if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
835 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
836 $sQuery = $aLooksLike['query'];
839 $aSearchResults = array();
840 if ($sQuery || $this->aStructuredQuery)
842 // Start with a blank search
844 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
845 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
846 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
849 // Do we have a radius search?
850 $sNearPointSQL = false;
851 if ($this->aNearPoint)
853 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
854 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
855 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
856 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
859 // Any 'special' terms in the search?
860 $bSpecialTerms = false;
861 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
862 $aSpecialTerms = array();
863 foreach($aSpecialTermsRaw as $aSpecialTerm)
865 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
866 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
869 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
870 $aSpecialTerms = array();
871 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
873 $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
874 unset($aStructuredQuery['amenity']);
876 foreach($aSpecialTermsRaw as $aSpecialTerm)
878 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
879 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
880 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
881 $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class not in (\'place\')) or country_code is not null';
882 if (CONST_Debug) var_Dump($sSQL);
883 $aSearchWords = $this->oDB->getAll($sSQL);
884 $aNewSearches = array();
885 foreach($aSearches as $aSearch)
887 foreach($aSearchWords as $aSearchTerm)
889 $aNewSearch = $aSearch;
890 if ($aSearchTerm['country_code'])
892 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
893 $aNewSearches[] = $aNewSearch;
894 $bSpecialTerms = true;
896 if ($aSearchTerm['class'])
898 $aNewSearch['sClass'] = $aSearchTerm['class'];
899 $aNewSearch['sType'] = $aSearchTerm['type'];
900 $aNewSearches[] = $aNewSearch;
901 $bSpecialTerms = true;
905 $aSearches = $aNewSearches;
908 // Split query into phrases
909 // Commas are used to reduce the search space by indicating where phrases split
910 if ($this->aStructuredQuery)
912 $aPhrases = $this->aStructuredQuery;
913 $bStructuredPhrases = true;
917 $aPhrases = explode(',',$sQuery);
918 $bStructuredPhrases = false;
921 // Convert each phrase to standard form
922 // Create a list of standard words
923 // Get all 'sets' of words
924 // Generate a complete list of all
926 foreach($aPhrases as $iPhrase => $sPhrase)
928 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
929 if (PEAR::isError($aPhrase))
931 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
932 if (CONST_Debug) var_dump($aPhrase);
935 if (trim($aPhrase['string']))
937 $aPhrases[$iPhrase] = $aPhrase;
938 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
939 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
940 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
944 unset($aPhrases[$iPhrase]);
948 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
949 $aPhraseTypes = array_keys($aPhrases);
950 $aPhrases = array_values($aPhrases);
952 if (sizeof($aTokens))
954 // Check which tokens we have, get the ID numbers
955 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
956 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
958 if (CONST_Debug) var_Dump($sSQL);
960 $aValidTokens = array();
961 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
962 else $aDatabaseWords = array();
963 if (PEAR::IsError($aDatabaseWords))
965 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
967 $aPossibleMainWordIDs = array();
968 $aWordFrequencyScores = array();
969 foreach($aDatabaseWords as $aToken)
971 // Very special case - require 2 letter country param to match the country code found
972 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
973 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
978 if (isset($aValidTokens[$aToken['word_token']]))
980 $aValidTokens[$aToken['word_token']][] = $aToken;
984 $aValidTokens[$aToken['word_token']] = array($aToken);
986 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
987 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
989 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
991 // Try and calculate GB postcodes we might be missing
992 foreach($aTokens as $sToken)
994 // Source of gb postcodes is now definitive - always use
995 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
997 if (substr($aData[1],-2,1) != ' ')
999 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1000 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1002 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1003 if ($aGBPostcodeLocation)
1005 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1008 // US ZIP+4 codes - if there is no token,
1009 // merge in the 5-digit ZIP code
1010 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1012 if (isset($aValidTokens[$aData[1]]))
1014 foreach($aValidTokens[$aData[1]] as $aToken)
1016 if (!$aToken['class'])
1018 if (isset($aValidTokens[$sToken]))
1020 $aValidTokens[$sToken][] = $aToken;
1024 $aValidTokens[$sToken] = array($aToken);
1032 foreach($aTokens as $sToken)
1034 // Unknown single word token with a number - assume it is a house number
1035 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1037 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1041 // Any words that have failed completely?
1042 // TODO: suggestions
1044 // Start the search process
1045 $aResultPlaceIDs = array();
1047 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1049 if ($this->bReverseInPlan)
1051 // Reverse phrase array and also reverse the order of the wordsets in
1052 // the first and final phrase. Don't bother about phrases in the middle
1053 // because order in the address doesn't matter.
1054 $aPhrases = array_reverse($aPhrases);
1055 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1056 if (sizeof($aPhrases) > 1)
1058 $aFinalPhrase = end($aPhrases);
1059 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1061 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1063 foreach($aGroupedSearches as $aSearches)
1065 foreach($aSearches as $aSearch)
1067 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1069 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1070 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1076 $aGroupedSearches = $aReverseGroupedSearches;
1077 ksort($aGroupedSearches);
1082 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1083 $aGroupedSearches = array();
1084 foreach($aSearches as $aSearch)
1086 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1088 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1089 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1092 ksort($aGroupedSearches);
1095 if (CONST_Debug) var_Dump($aGroupedSearches);
1097 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
1099 $aCopyGroupedSearches = $aGroupedSearches;
1100 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1102 foreach($aSearches as $iSearch => $aSearch)
1104 $aReductionsList = array($aSearch['aAddress']);
1105 $iSearchRank = $aSearch['iSearchRank'];
1106 while(sizeof($aReductionsList) > 0)
1109 if ($iSearchRank > iMaxRank) break 3;
1110 $aNewReductionsList = array();
1111 foreach($aReductionsList as $aReductionsWordList)
1113 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1115 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1116 $aReverseSearch = $aSearch;
1117 $aSearch['aAddress'] = $aReductionsWordListResult;
1118 $aSearch['iSearchRank'] = $iSearchRank;
1119 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1120 if (sizeof($aReductionsWordListResult) > 0)
1122 $aNewReductionsList[] = $aReductionsWordListResult;
1126 $aReductionsList = $aNewReductionsList;
1130 ksort($aGroupedSearches);
1133 // Filter out duplicate searches
1134 $aSearchHash = array();
1135 foreach($aGroupedSearches as $iGroup => $aSearches)
1137 foreach($aSearches as $iSearch => $aSearch)
1139 $sHash = serialize($aSearch);
1140 if (isset($aSearchHash[$sHash]))
1142 unset($aGroupedSearches[$iGroup][$iSearch]);
1143 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1147 $aSearchHash[$sHash] = 1;
1152 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1156 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1159 foreach($aSearches as $aSearch)
1163 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1164 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1166 // No location term?
1167 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1169 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1171 // Just looking for a country by code - look it up
1172 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1174 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1175 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1176 if ($bBoundingBoxSearch)
1177 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1178 $sSQL .= " order by st_area(geometry) desc limit 1";
1179 if (CONST_Debug) var_dump($sSQL);
1180 $aPlaceIDs = $this->oDB->getCol($sSQL);
1184 $aPlaceIDs = array();
1189 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1190 if (!$aSearch['sClass']) continue;
1191 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1192 if ($this->oDB->getOne($sSQL))
1194 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1195 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1196 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1197 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1198 if (sizeof($this->aExcludePlaceIDs))
1200 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1202 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1203 $sSQL .= " limit $this->iLimit";
1204 if (CONST_Debug) var_dump($sSQL);
1205 $aPlaceIDs = $this->oDB->getCol($sSQL);
1207 // If excluded place IDs are given, it is fair to assume that
1208 // there have been results in the small box, so no further
1209 // expansion in that case.
1210 // Also don't expand if bounded results were requested.
1211 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1213 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1214 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1215 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1216 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1217 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1218 $sSQL .= " limit $this->iLimit";
1219 if (CONST_Debug) var_dump($sSQL);
1220 $aPlaceIDs = $this->oDB->getCol($sSQL);
1225 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1226 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1227 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1228 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1229 $sSQL .= " limit $this->iLimit";
1230 if (CONST_Debug) var_dump($sSQL);
1231 $aPlaceIDs = $this->oDB->getCol($sSQL);
1237 $aPlaceIDs = array();
1239 // First we need a position, either aName or fLat or both
1243 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1245 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1246 $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
1249 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1250 // they might be right - but they are just too darned expensive to run
1251 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1252 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1253 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1255 // For infrequent name terms disable index usage for address
1256 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1257 sizeof($aSearch['aName']) == 1 &&
1258 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1260 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1264 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1265 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1268 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1269 if ($aSearch['sHouseNumber'])
1271 $aTerms[] = "address_rank between 16 and 27";
1275 if ($this->iMinAddressRank > 0)
1277 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1279 if ($this->iMaxAddressRank < 30)
1281 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1284 if ($aSearch['fLon'] && $aSearch['fLat'])
1286 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1287 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1289 if (sizeof($this->aExcludePlaceIDs))
1291 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1293 if ($sCountryCodesSQL)
1295 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1298 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1299 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1301 if ($aSearch['sHouseNumber'])
1303 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1307 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1309 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1310 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1312 $aOrder[] = "$sImportanceSQL DESC";
1313 if (sizeof($aSearch['aFullNameAddress']))
1315 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1316 $aOrder[] = 'exactmatch DESC';
1318 $sExactMatchSQL = '0::int as exactmatch';
1321 if (sizeof($aTerms))
1323 $sSQL = "select place_id, ";
1324 $sSQL .= $sExactMatchSQL;
1325 $sSQL .= " from search_name";
1326 $sSQL .= " where ".join(' and ',$aTerms);
1327 $sSQL .= " order by ".join(', ',$aOrder);
1328 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1329 $sSQL .= " limit 20";
1330 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1331 $sSQL .= " limit 1";
1333 $sSQL .= " limit ".$this->iLimit;
1335 if (CONST_Debug) { var_dump($sSQL); }
1336 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1337 if (PEAR::IsError($aViewBoxPlaceIDs))
1339 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1341 //var_dump($aViewBoxPlaceIDs);
1342 // Did we have an viewbox matches?
1343 $aPlaceIDs = array();
1344 $bViewBoxMatch = false;
1345 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1347 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1348 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1349 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1350 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1351 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1352 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1355 //var_Dump($aPlaceIDs);
1358 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1360 $aRoadPlaceIDs = $aPlaceIDs;
1361 $sPlaceIDs = join(',',$aPlaceIDs);
1363 // Now they are indexed look for a house attached to a street we found
1364 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1365 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1366 if (sizeof($this->aExcludePlaceIDs))
1368 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1370 $sSQL .= " limit $this->iLimit";
1371 if (CONST_Debug) var_dump($sSQL);
1372 $aPlaceIDs = $this->oDB->getCol($sSQL);
1374 // If not try the aux fallback table
1375 if (!sizeof($aPlaceIDs))
1377 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1378 if (sizeof($this->aExcludePlaceIDs))
1380 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1382 //$sSQL .= " limit $this->iLimit";
1383 if (CONST_Debug) var_dump($sSQL);
1384 $aPlaceIDs = $this->oDB->getCol($sSQL);
1387 if (!sizeof($aPlaceIDs))
1389 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1390 if (sizeof($this->aExcludePlaceIDs))
1392 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1394 //$sSQL .= " limit $this->iLimit";
1395 if (CONST_Debug) var_dump($sSQL);
1396 $aPlaceIDs = $this->oDB->getCol($sSQL);
1399 // Fallback to the road
1400 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1402 $aPlaceIDs = $aRoadPlaceIDs;
1407 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1409 $sPlaceIDs = join(',',$aPlaceIDs);
1410 $aClassPlaceIDs = array();
1412 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1414 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1415 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1416 $sSQL .= " and linked_place_id is null";
1417 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1418 $sSQL .= " order by rank_search asc limit $this->iLimit";
1419 if (CONST_Debug) var_dump($sSQL);
1420 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1423 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1425 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1426 $bCacheTable = $this->oDB->getOne($sSQL);
1428 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1430 if (CONST_Debug) var_dump($sSQL);
1431 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1433 // For state / country level searches the normal radius search doesn't work very well
1434 $sPlaceGeom = false;
1435 if ($this->iMaxRank < 9 && $bCacheTable)
1437 // Try and get a polygon to search in instead
1438 $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
1439 if (CONST_Debug) var_dump($sSQL);
1440 $sPlaceGeom = $this->oDB->getOne($sSQL);
1449 $this->iMaxRank += 5;
1450 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1451 if (CONST_Debug) var_dump($sSQL);
1452 $aPlaceIDs = $this->oDB->getCol($sSQL);
1453 $sPlaceIDs = join(',',$aPlaceIDs);
1456 if ($sPlaceIDs || $sPlaceGeom)
1462 // More efficient - can make the range bigger
1466 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1467 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1468 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1470 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1471 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1474 $sSQL .= ",placex as f where ";
1475 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1480 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1482 if (sizeof($this->aExcludePlaceIDs))
1484 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1486 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1487 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1488 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1489 $sSQL .= " limit $this->iLimit";
1490 if (CONST_Debug) var_dump($sSQL);
1491 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1495 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1498 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1499 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1501 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1502 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1503 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1504 if (sizeof($this->aExcludePlaceIDs))
1506 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1508 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1509 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1510 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1511 $sSQL .= " limit $this->iLimit";
1512 if (CONST_Debug) var_dump($sSQL);
1513 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1518 $aPlaceIDs = $aClassPlaceIDs;
1524 if (PEAR::IsError($aPlaceIDs))
1526 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1529 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1531 foreach($aPlaceIDs as $iPlaceID)
1533 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1535 if ($iQueryLoop > 20) break;
1538 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1540 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1541 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1542 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1543 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1544 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1545 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1546 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1547 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1549 if (CONST_Debug) var_dump($sSQL);
1550 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1554 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1555 if ($iGroupLoop > 4) break;
1556 if ($iQueryLoop > 30) break;
1559 // Did we find anything?
1560 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1562 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1568 // Just interpret as a reverse geocode
1569 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1571 $aSearchResults = $this->getDetails(array($iPlaceID));
1573 $aSearchResults = array();
1577 if (!sizeof($aSearchResults))
1579 if ($this->bFallback)
1581 if ($this->fallbackStructuredQuery())
1583 return $this->lookup();
1590 $aClassType = getClassTypesWithImportance();
1591 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1592 foreach($aRecheckWords as $i => $sWord)
1594 if (!$sWord) unset($aRecheckWords[$i]);
1597 foreach($aSearchResults as $iResNum => $aResult)
1599 if (CONST_Search_AreaPolygons)
1601 // Get the bounding box and outline polygon
1602 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1603 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1604 $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1605 $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1606 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1607 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1608 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1609 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1610 $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1611 $aPointPolygon = $this->oDB->getRow($sSQL);
1612 if (PEAR::IsError($aPointPolygon))
1614 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1617 if ($aPointPolygon['place_id'])
1619 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1620 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1621 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1622 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1624 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1626 $aResult['lat'] = $aPointPolygon['centrelat'];
1627 $aResult['lon'] = $aPointPolygon['centrelon'];
1630 if ($this->bIncludePolygonAsPoints)
1632 // Translate geometary string to point array
1633 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1635 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1637 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1639 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1641 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1644 $iSteps = ($fRadius * 40000)^2;
1645 $fStepSize = (2*pi())/$iSteps;
1646 $aPolyPoints = array();
1647 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1649 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1651 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1652 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1653 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1654 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1658 // Output data suitable for display (points and a bounding box)
1659 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1661 $aResult['aPolyPoints'] = array();
1662 foreach($aPolyPoints as $aPoint)
1664 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1667 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1671 if ($aResult['extra_place'] == 'city')
1673 $aResult['class'] = 'place';
1674 $aResult['type'] = 'city';
1675 $aResult['rank_search'] = 16;
1678 if (!isset($aResult['aBoundingBox']))
1681 $fDiameter = 0.0001;
1683 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1684 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1686 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1688 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1689 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1691 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1693 $fRadius = $fDiameter / 2;
1695 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1696 $fStepSize = (2*pi())/$iSteps;
1697 $aPolyPoints = array();
1698 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1700 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1702 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1703 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1704 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1705 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1707 // Output data suitable for display (points and a bounding box)
1708 if ($this->bIncludePolygonAsPoints)
1710 $aResult['aPolyPoints'] = array();
1711 foreach($aPolyPoints as $aPoint)
1713 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1716 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1719 // Is there an icon set for this type of result?
1720 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1721 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1723 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1726 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1727 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1729 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1731 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1732 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1734 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1737 if ($this->bIncludeAddressDetails)
1739 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1740 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1742 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1746 // Adjust importance for the number of exact string matches in the result
1747 $aResult['importance'] = max(0.001,$aResult['importance']);
1749 $sAddress = $aResult['langaddress'];
1750 foreach($aRecheckWords as $i => $sWord)
1752 if (stripos($sAddress, $sWord)!==false)
1755 if (preg_match("/(^|,)\s*$sWord\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1759 $aResult['importance'] = $aResult['importance'] + ($iCountWords*0.1); // 0.1 is a completely arbitrary number but something in the range 0.1 to 0.5 would seem right
1761 $aResult['name'] = $aResult['langaddress'];
1762 // secondary ordering (for results with same importance (the smaller the better):
1763 // - approximate importance of address parts
1764 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1765 // - number of exact matches from the query
1766 if (isset($this->exactMatchCache[$aResult['place_id']]))
1767 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1768 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1769 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1770 // - importance of the class/type
1771 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1772 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1774 $aResult['foundorder'] = $aResult['foundorder'] + 0.000001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1778 $aResult['foundorder'] = $aResult['foundorder'] + 0.001;
1780 $aSearchResults[$iResNum] = $aResult;
1782 uasort($aSearchResults, 'byImportance');
1784 $aOSMIDDone = array();
1785 $aClassTypeNameDone = array();
1786 $aToFilter = $aSearchResults;
1787 $aSearchResults = array();
1790 foreach($aToFilter as $iResNum => $aResult)
1792 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1795 $fLat = $aResult['lat'];
1796 $fLon = $aResult['lon'];
1797 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1800 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1801 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1803 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1804 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1805 $aSearchResults[] = $aResult;
1808 // Absolute limit on number of results
1809 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1812 return $aSearchResults;