6 protected $aLangPrefOrder = array();
8 protected $bIncludeAddressDetails = false;
9 protected $bIncludeExtraTags = false;
10 protected $bIncludeNameDetails = false;
12 protected $bIncludePolygonAsPoints = false;
13 protected $bIncludePolygonAsText = false;
14 protected $bIncludePolygonAsGeoJSON = false;
15 protected $bIncludePolygonAsKML = false;
16 protected $bIncludePolygonAsSVG = false;
17 protected $fPolygonSimplificationThreshold = 0.0;
19 protected $aExcludePlaceIDs = array();
20 protected $bDeDupe = true;
21 protected $bReverseInPlan = false;
23 protected $iLimit = 20;
24 protected $iFinalLimit = 10;
25 protected $iOffset = 0;
26 protected $bFallback = false;
28 protected $aCountryCodes = false;
29 protected $aNearPoint = false;
31 protected $bBoundedSearch = false;
32 protected $aViewBox = false;
33 protected $sViewboxSmallSQL = false;
34 protected $sViewboxLargeSQL = false;
35 protected $aRoutePoints = false;
37 protected $iMaxRank = 20;
38 protected $iMinAddressRank = 0;
39 protected $iMaxAddressRank = 30;
40 protected $aAddressRankList = array();
41 protected $exactMatchCache = array();
43 protected $sAllowedTypesSQLList = false;
45 protected $sQuery = false;
46 protected $aStructuredQuery = false;
48 function Geocode(&$oDB)
53 function setReverseInPlan($bReverse)
55 $this->bReverseInPlan = $bReverse;
58 function setLanguagePreference($aLangPref)
60 $this->aLangPrefOrder = $aLangPref;
63 function setIncludeAddressDetails($bAddressDetails = true)
65 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
68 function getIncludeAddressDetails()
70 return $this->bIncludeAddressDetails;
73 function getIncludeExtraTags()
75 return $this->bIncludeExtraTags;
78 function getIncludeNameDetails()
80 return $this->bIncludeNameDetails;
83 function setIncludePolygonAsPoints($b = true)
85 $this->bIncludePolygonAsPoints = $b;
88 function getIncludePolygonAsPoints()
90 return $this->bIncludePolygonAsPoints;
93 function setIncludePolygonAsText($b = true)
95 $this->bIncludePolygonAsText = $b;
98 function getIncludePolygonAsText()
100 return $this->bIncludePolygonAsText;
103 function setIncludePolygonAsGeoJSON($b = true)
105 $this->bIncludePolygonAsGeoJSON = $b;
108 function setIncludePolygonAsKML($b = true)
110 $this->bIncludePolygonAsKML = $b;
113 function setIncludePolygonAsSVG($b = true)
115 $this->bIncludePolygonAsSVG = $b;
118 function setPolygonSimplificationThreshold($f)
120 $this->fPolygonSimplificationThreshold = $f;
123 function setDeDupe($bDeDupe = true)
125 $this->bDeDupe = (bool)$bDeDupe;
128 function setLimit($iLimit = 10)
130 if ($iLimit > 50) $iLimit = 50;
131 if ($iLimit < 1) $iLimit = 1;
133 $this->iFinalLimit = $iLimit;
134 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
137 function setOffset($iOffset = 0)
139 $this->iOffset = $iOffset;
142 function setFallback($bFallback = true)
144 $this->bFallback = (bool)$bFallback;
147 function setExcludedPlaceIDs($a)
149 // TODO: force to int
150 $this->aExcludePlaceIDs = $a;
153 function getExcludedPlaceIDs()
155 return $this->aExcludePlaceIDs;
158 function setBounded($bBoundedSearch = true)
160 $this->bBoundedSearch = (bool)$bBoundedSearch;
163 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
165 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
168 function getViewBoxString()
170 if (!$this->aViewBox) return null;
171 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
174 function setRoute($aRoutePoints)
176 $this->aRoutePoints = $aRoutePoints;
179 function setFeatureType($sFeatureType)
181 switch($sFeatureType)
184 $this->setRankRange(4, 4);
187 $this->setRankRange(8, 8);
190 $this->setRankRange(14, 16);
193 $this->setRankRange(8, 20);
198 function setRankRange($iMin, $iMax)
200 $this->iMinAddressRank = (int)$iMin;
201 $this->iMaxAddressRank = (int)$iMax;
204 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
206 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
209 function setCountryCodesList($aCountryCodes)
211 $this->aCountryCodes = $aCountryCodes;
214 function setQuery($sQueryString)
216 $this->sQuery = $sQueryString;
217 $this->aStructuredQuery = false;
220 function getQueryString()
222 return $this->sQuery;
226 function loadParamArray($aParams)
228 if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
229 if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags'];
230 if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails'];
231 if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
232 if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
234 if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
235 if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
237 if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
239 // List of excluded Place IDs - used for more acurate pageing
240 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
242 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
244 $iExcludedPlaceID = (int)$iExcludedPlaceID;
245 if ($iExcludedPlaceID)
246 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
249 if (isset($aExcludePlaceIDs))
250 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
253 // Only certain ranks of feature
254 if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
255 if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
258 if (isset($aParams['countrycodes']))
260 $aCountryCodes = array();
261 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
263 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
265 $aCountryCodes[] = strtolower($sCountryCode);
268 $this->aCountryCodes = $aCountryCodes;
271 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
273 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
274 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
276 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
278 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
279 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
282 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
284 $aPoints = explode(',',$aParams['route']);
285 if (sizeof($aPoints) % 2 != 0)
287 userError("Uneven number of points");
292 foreach($aPoints as $i => $fPoint)
296 $aRoute[] = array((float)$fPoint, $fPrevCoord);
300 $fPrevCoord = (float)$fPoint;
303 $this->aRoutePoints = $aRoute;
307 function setQueryFromParams($aParams)
310 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
313 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
314 $this->setReverseInPlan(false);
318 $this->setQuery($sQuery);
322 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
324 $sValue = trim($sValue);
325 if (!$sValue) return false;
326 $this->aStructuredQuery[$sKey] = $sValue;
327 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
329 $this->iMinAddressRank = $iNewMinAddressRank;
330 $this->iMaxAddressRank = $iNewMaxAddressRank;
332 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
336 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
338 $this->sQuery = false;
341 $this->iMinAddressRank = 0;
342 $this->iMaxAddressRank = 30;
343 $this->aAddressRankList = array();
345 $this->aStructuredQuery = array();
346 $this->sAllowedTypesSQLList = '';
348 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
349 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
350 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
351 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
352 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
353 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
354 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
356 if (sizeof($this->aStructuredQuery) > 0)
358 $this->sQuery = join(', ', $this->aStructuredQuery);
359 if ($this->iMaxAddressRank < 30)
361 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
366 function fallbackStructuredQuery()
368 if (!$this->aStructuredQuery) return false;
370 $aParams = $this->aStructuredQuery;
372 if (sizeof($aParams) == 1) return false;
374 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
376 foreach($aOrderToFallback as $sType)
378 if (isset($aParams[$sType]))
380 unset($aParams[$sType]);
381 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
389 function getDetails($aPlaceIDs)
391 if (sizeof($aPlaceIDs) == 0) return array();
393 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
395 // Get the details for display (is this a redundant extra step?)
396 $sPlaceIDs = join(',',$aPlaceIDs);
398 $sImportanceSQL = '';
399 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
400 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
402 $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,";
403 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
404 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
405 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
406 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
407 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
408 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
409 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
410 $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, ";
411 $sSQL .= "(extratags->'place') as extra_place ";
412 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
413 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
414 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
415 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
417 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
418 $sSQL .= "and linked_place_id is null ";
419 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
420 if (!$this->bDeDupe) $sSQL .= ",place_id";
421 $sSQL .= ",langaddress ";
422 $sSQL .= ",placename ";
424 if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
425 if ($this->bIncludeNameDetails) $sSQL .= ",name";
426 $sSQL .= ",extratags->'place' ";
428 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
431 $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,";
432 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
433 $sSQL .= "null as placename,";
434 $sSQL .= "null as ref,";
435 if ($this->bIncludeExtraTags) $sSQL .= "null as extra,";
436 if ($this->bIncludeNameDetails) $sSQL .= "null as names,";
437 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
438 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
439 $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, ";
440 $sSQL .= "null as extra_place ";
441 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
442 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
443 $sSQL .= "group by place_id";
444 if (!$this->bDeDupe) $sSQL .= ",place_id ";
446 $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,";
447 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
448 $sSQL .= "null as placename,";
449 $sSQL .= "null as ref,";
450 if ($this->bIncludeExtraTags) $sSQL .= "null as extra,";
451 if ($this->bIncludeNameDetails) $sSQL .= "null as names,";
452 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
453 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
454 $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, ";
455 $sSQL .= "null as extra_place ";
456 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
457 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
458 $sSQL .= "group by place_id";
459 if (!$this->bDeDupe) $sSQL .= ",place_id";
460 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
463 $sSQL .= " order by importance desc";
464 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
465 $aSearchResults = $this->oDB->getAll($sSQL);
467 if (PEAR::IsError($aSearchResults))
469 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
472 return $aSearchResults;
475 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
478 Calculate all searches using aValidTokens i.e.
479 'Wodsworth Road, Sheffield' =>
483 0 1 (wodsworth)(road)
486 Score how good the search is so they can be ordered
488 foreach($aPhrases as $iPhrase => $sPhrase)
490 $aNewPhraseSearches = array();
491 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
492 else $sPhraseType = '';
494 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
496 // Too many permutations - too expensive
497 if ($iWordSet > 120) break;
499 $aWordsetSearches = $aSearches;
501 // Add all words from this wordset
502 foreach($aWordset as $iToken => $sToken)
504 //echo "<br><b>$sToken</b>";
505 $aNewWordsetSearches = array();
507 foreach($aWordsetSearches as $aCurrentSearch)
510 //var_dump($aCurrentSearch);
513 // If the token is valid
514 if (isset($aValidTokens[' '.$sToken]))
516 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
518 $aSearch = $aCurrentSearch;
519 $aSearch['iSearchRank']++;
520 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
522 if ($aSearch['sCountryCode'] === false)
524 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
525 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
526 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
528 $aSearch['iSearchRank'] += 5;
530 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
533 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
535 if ($aSearch['fLat'] === '')
537 $aSearch['fLat'] = $aSearchTerm['lat'];
538 $aSearch['fLon'] = $aSearchTerm['lon'];
539 $aSearch['fRadius'] = $aSearchTerm['radius'];
540 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
543 elseif ($sPhraseType == 'postalcode')
545 // 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
546 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
548 // If we already have a name try putting the postcode first
549 if (sizeof($aSearch['aName']))
551 $aNewSearch = $aSearch;
552 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
553 $aNewSearch['aName'] = array();
554 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
555 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
558 if (sizeof($aSearch['aName']))
560 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
562 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
566 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
567 $aSearch['iSearchRank'] += 1000; // skip;
572 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
573 //$aSearch['iNamePhrase'] = $iPhrase;
575 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
579 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
581 if ($aSearch['sHouseNumber'] === '')
583 $aSearch['sHouseNumber'] = $sToken;
584 // sanity check: if the housenumber is not mainly made
585 // up of numbers, add a penalty
586 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
587 // also housenumbers should appear in the first or second phrase
588 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
589 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
591 // Fall back to not searching for this item (better than nothing)
592 $aSearch = $aCurrentSearch;
593 $aSearch['iSearchRank'] += 1;
594 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
598 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
600 if ($aSearch['sClass'] === '')
602 $aSearch['sOperator'] = $aSearchTerm['operator'];
603 $aSearch['sClass'] = $aSearchTerm['class'];
604 $aSearch['sType'] = $aSearchTerm['type'];
605 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
606 else $aSearch['sOperator'] = 'near'; // near = in for the moment
607 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
609 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
612 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
614 if (sizeof($aSearch['aName']))
616 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
618 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
622 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
623 $aSearch['iSearchRank'] += 1000; // skip;
628 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
629 //$aSearch['iNamePhrase'] = $iPhrase;
631 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
635 // Look for partial matches.
636 // Note that there is no point in adding country terms here
637 // because country are omitted in the address.
638 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
640 // Allow searching for a word - but at extra cost
641 foreach($aValidTokens[$sToken] as $aSearchTerm)
643 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
645 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
647 $aSearch = $aCurrentSearch;
648 $aSearch['iSearchRank'] += 1;
649 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
651 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
652 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
654 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
656 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
657 $aSearch['iSearchRank'] += 1;
658 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
659 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
661 if (empty($aSearchTermToken['country_code'])
662 && empty($aSearchTermToken['lat'])
663 && empty($aSearchTermToken['class']))
665 $aSearch = $aCurrentSearch;
666 $aSearch['iSearchRank'] += 1;
667 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
668 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
674 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
675 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
676 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
680 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
682 $aSearch = $aCurrentSearch;
683 $aSearch['iSearchRank'] += 1;
684 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
685 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
686 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
687 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
689 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
690 $aSearch['iNamePhrase'] = $iPhrase;
691 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
698 // Allow skipping a word - but at EXTREAM cost
699 //$aSearch = $aCurrentSearch;
700 //$aSearch['iSearchRank']+=100;
701 //$aNewWordsetSearches[] = $aSearch;
705 usort($aNewWordsetSearches, 'bySearchRank');
706 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
708 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
710 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
711 usort($aNewPhraseSearches, 'bySearchRank');
713 $aSearchHash = array();
714 foreach($aNewPhraseSearches as $iSearch => $aSearch)
716 $sHash = serialize($aSearch);
717 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
718 else $aSearchHash[$sHash] = 1;
721 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
724 // Re-group the searches by their score, junk anything over 20 as just not worth trying
725 $aGroupedSearches = array();
726 foreach($aNewPhraseSearches as $aSearch)
728 if ($aSearch['iSearchRank'] < $this->iMaxRank)
730 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
731 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
734 ksort($aGroupedSearches);
737 $aSearches = array();
738 foreach($aGroupedSearches as $iScore => $aNewSearches)
740 $iSearchCount += sizeof($aNewSearches);
741 $aSearches = array_merge($aSearches, $aNewSearches);
742 if ($iSearchCount > 50) break;
745 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
748 return $aGroupedSearches;
752 /* Perform the actual query lookup.
754 Returns an ordered list of results, each with the following fields:
755 osm_type: type of corresponding OSM object
759 P - postcode (internally computed)
760 osm_id: id of corresponding OSM object
761 class: general object class (corresponds to tag key of primary OSM tag)
762 type: subclass of object (corresponds to tag value of primary OSM tag)
763 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
764 rank_search: rank in search hierarchy
765 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
766 rank_address: rank in address hierarchy (determines orer in address)
767 place_id: internal key (may differ between different instances)
768 country_code: ISO country code
769 langaddress: localized full address
770 placename: localized name of object
771 ref: content of ref tag (if available)
774 importance: importance of place based on Wikipedia link count
775 addressimportance: cumulated importance of address elements
776 extra_place: type of place (for admin boundaries, if there is a place tag)
777 aBoundingBox: bounding Box
778 label: short description of the object class/type (English only)
779 name: full name (currently the same as langaddress)
780 foundorder: secondary ordering for places with same importance
784 if (!$this->sQuery && !$this->aStructuredQuery) return false;
786 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
787 $sCountryCodesSQL = false;
788 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
790 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
793 $sQuery = $this->sQuery;
795 // Conflicts between US state abreviations and various words for 'the' in different languages
796 if (isset($this->aLangPrefOrder['name:en']))
798 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
799 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
800 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
804 $sViewboxCentreSQL = false;
805 $bBoundingBoxSearch = false;
808 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
809 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
810 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
811 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
812 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
813 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
815 $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)";
816 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
817 $bBoundingBoxSearch = $this->bBoundedSearch;
821 if ($this->aRoutePoints)
823 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
825 foreach($this->aRoutePoints as $aPoint)
827 if (!$bFirst) $sViewboxCentreSQL .= ",";
828 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
831 $sViewboxCentreSQL .= ")'::geometry,4326)";
833 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
834 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
835 if (PEAR::isError($this->sViewboxSmallSQL))
837 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
839 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
841 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
842 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
843 if (PEAR::isError($this->sViewboxLargeSQL))
845 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
847 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
848 $bBoundingBoxSearch = $this->bBoundedSearch;
851 // Do we have anything that looks like a lat/lon pair?
852 if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
853 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
854 $sQuery = $aLooksLike['query'];
857 $aSearchResults = array();
858 if ($sQuery || $this->aStructuredQuery)
860 // Start with a blank search
862 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
863 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
864 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
867 // Do we have a radius search?
868 $sNearPointSQL = false;
869 if ($this->aNearPoint)
871 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
872 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
873 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
874 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
877 // Any 'special' terms in the search?
878 $bSpecialTerms = false;
879 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
880 $aSpecialTerms = array();
881 foreach($aSpecialTermsRaw as $aSpecialTerm)
883 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
884 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
887 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
888 $aSpecialTerms = array();
889 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity'])
891 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
892 unset($this->aStructuredQuery['amenity']);
894 foreach($aSpecialTermsRaw as $aSpecialTerm)
896 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
897 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
898 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
899 $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';
900 if (CONST_Debug) var_Dump($sSQL);
901 $aSearchWords = $this->oDB->getAll($sSQL);
902 $aNewSearches = array();
903 foreach($aSearches as $aSearch)
905 foreach($aSearchWords as $aSearchTerm)
907 $aNewSearch = $aSearch;
908 if ($aSearchTerm['country_code'])
910 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
911 $aNewSearches[] = $aNewSearch;
912 $bSpecialTerms = true;
914 if ($aSearchTerm['class'])
916 $aNewSearch['sClass'] = $aSearchTerm['class'];
917 $aNewSearch['sType'] = $aSearchTerm['type'];
918 $aNewSearches[] = $aNewSearch;
919 $bSpecialTerms = true;
923 $aSearches = $aNewSearches;
926 // Split query into phrases
927 // Commas are used to reduce the search space by indicating where phrases split
928 if ($this->aStructuredQuery)
930 $aPhrases = $this->aStructuredQuery;
931 $bStructuredPhrases = true;
935 $aPhrases = explode(',',$sQuery);
936 $bStructuredPhrases = false;
939 // Convert each phrase to standard form
940 // Create a list of standard words
941 // Get all 'sets' of words
942 // Generate a complete list of all
944 foreach($aPhrases as $iPhrase => $sPhrase)
946 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
947 if (PEAR::isError($aPhrase))
949 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
950 if (CONST_Debug) var_dump($aPhrase);
953 if (trim($aPhrase['string']))
955 $aPhrases[$iPhrase] = $aPhrase;
956 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
957 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
958 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
962 unset($aPhrases[$iPhrase]);
966 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
967 $aPhraseTypes = array_keys($aPhrases);
968 $aPhrases = array_values($aPhrases);
970 if (sizeof($aTokens))
972 // Check which tokens we have, get the ID numbers
973 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
974 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
976 if (CONST_Debug) var_Dump($sSQL);
978 $aValidTokens = array();
979 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
980 else $aDatabaseWords = array();
981 if (PEAR::IsError($aDatabaseWords))
983 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
985 $aPossibleMainWordIDs = array();
986 $aWordFrequencyScores = array();
987 foreach($aDatabaseWords as $aToken)
989 // Very special case - require 2 letter country param to match the country code found
990 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
991 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code'])
996 if (isset($aValidTokens[$aToken['word_token']]))
998 $aValidTokens[$aToken['word_token']][] = $aToken;
1002 $aValidTokens[$aToken['word_token']] = array($aToken);
1004 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1005 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1007 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1009 // Try and calculate GB postcodes we might be missing
1010 foreach($aTokens as $sToken)
1012 // Source of gb postcodes is now definitive - always use
1013 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
1015 if (substr($aData[1],-2,1) != ' ')
1017 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1018 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1020 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1021 if ($aGBPostcodeLocation)
1023 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1026 // US ZIP+4 codes - if there is no token,
1027 // merge in the 5-digit ZIP code
1028 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1030 if (isset($aValidTokens[$aData[1]]))
1032 foreach($aValidTokens[$aData[1]] as $aToken)
1034 if (!$aToken['class'])
1036 if (isset($aValidTokens[$sToken]))
1038 $aValidTokens[$sToken][] = $aToken;
1042 $aValidTokens[$sToken] = array($aToken);
1050 foreach($aTokens as $sToken)
1052 // Unknown single word token with a number - assume it is a house number
1053 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1055 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1059 // Any words that have failed completely?
1060 // TODO: suggestions
1062 // Start the search process
1063 $aResultPlaceIDs = array();
1065 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1067 if ($this->bReverseInPlan)
1069 // Reverse phrase array and also reverse the order of the wordsets in
1070 // the first and final phrase. Don't bother about phrases in the middle
1071 // because order in the address doesn't matter.
1072 $aPhrases = array_reverse($aPhrases);
1073 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1074 if (sizeof($aPhrases) > 1)
1076 $aFinalPhrase = end($aPhrases);
1077 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1079 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1081 foreach($aGroupedSearches as $aSearches)
1083 foreach($aSearches as $aSearch)
1085 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1087 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1088 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1094 $aGroupedSearches = $aReverseGroupedSearches;
1095 ksort($aGroupedSearches);
1100 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1101 $aGroupedSearches = array();
1102 foreach($aSearches as $aSearch)
1104 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1106 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1107 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1110 ksort($aGroupedSearches);
1113 if (CONST_Debug) var_Dump($aGroupedSearches);
1115 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0)
1117 $aCopyGroupedSearches = $aGroupedSearches;
1118 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1120 foreach($aSearches as $iSearch => $aSearch)
1122 $aReductionsList = array($aSearch['aAddress']);
1123 $iSearchRank = $aSearch['iSearchRank'];
1124 while(sizeof($aReductionsList) > 0)
1127 if ($iSearchRank > iMaxRank) break 3;
1128 $aNewReductionsList = array();
1129 foreach($aReductionsList as $aReductionsWordList)
1131 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1133 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1134 $aReverseSearch = $aSearch;
1135 $aSearch['aAddress'] = $aReductionsWordListResult;
1136 $aSearch['iSearchRank'] = $iSearchRank;
1137 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1138 if (sizeof($aReductionsWordListResult) > 0)
1140 $aNewReductionsList[] = $aReductionsWordListResult;
1144 $aReductionsList = $aNewReductionsList;
1148 ksort($aGroupedSearches);
1151 // Filter out duplicate searches
1152 $aSearchHash = array();
1153 foreach($aGroupedSearches as $iGroup => $aSearches)
1155 foreach($aSearches as $iSearch => $aSearch)
1157 $sHash = serialize($aSearch);
1158 if (isset($aSearchHash[$sHash]))
1160 unset($aGroupedSearches[$iGroup][$iSearch]);
1161 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1165 $aSearchHash[$sHash] = 1;
1170 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1174 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1177 foreach($aSearches as $aSearch)
1181 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1182 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1184 // No location term?
1185 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1187 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1189 // Just looking for a country by code - look it up
1190 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1192 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1193 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1194 if ($bBoundingBoxSearch)
1195 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1196 $sSQL .= " order by st_area(geometry) desc limit 1";
1197 if (CONST_Debug) var_dump($sSQL);
1198 $aPlaceIDs = $this->oDB->getCol($sSQL);
1202 $aPlaceIDs = array();
1207 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1208 if (!$aSearch['sClass']) continue;
1209 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1210 if ($this->oDB->getOne($sSQL))
1212 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1213 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1214 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1215 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1216 if (sizeof($this->aExcludePlaceIDs))
1218 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1220 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1221 $sSQL .= " limit $this->iLimit";
1222 if (CONST_Debug) var_dump($sSQL);
1223 $aPlaceIDs = $this->oDB->getCol($sSQL);
1225 // If excluded place IDs are given, it is fair to assume that
1226 // there have been results in the small box, so no further
1227 // expansion in that case.
1228 // Also don't expand if bounded results were requested.
1229 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1231 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1232 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1233 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1234 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1235 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1236 $sSQL .= " limit $this->iLimit";
1237 if (CONST_Debug) var_dump($sSQL);
1238 $aPlaceIDs = $this->oDB->getCol($sSQL);
1243 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1244 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1245 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1246 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1247 $sSQL .= " limit $this->iLimit";
1248 if (CONST_Debug) var_dump($sSQL);
1249 $aPlaceIDs = $this->oDB->getCol($sSQL);
1255 $aPlaceIDs = array();
1257 // First we need a position, either aName or fLat or both
1261 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1263 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1264 $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
1267 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1268 // they might be right - but they are just too darned expensive to run
1269 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1270 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1271 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1273 // For infrequent name terms disable index usage for address
1274 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1275 sizeof($aSearch['aName']) == 1 &&
1276 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1278 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1282 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1283 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1286 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1287 if ($aSearch['sHouseNumber'])
1289 $aTerms[] = "address_rank between 16 and 27";
1293 if ($this->iMinAddressRank > 0)
1295 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1297 if ($this->iMaxAddressRank < 30)
1299 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1302 if ($aSearch['fLon'] && $aSearch['fLat'])
1304 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1305 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1307 if (sizeof($this->aExcludePlaceIDs))
1309 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1311 if ($sCountryCodesSQL)
1313 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1316 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1317 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1319 if ($aSearch['sHouseNumber'])
1321 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1325 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1327 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1328 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1330 $aOrder[] = "$sImportanceSQL DESC";
1331 if (sizeof($aSearch['aFullNameAddress']))
1333 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1334 $aOrder[] = 'exactmatch DESC';
1336 $sExactMatchSQL = '0::int as exactmatch';
1339 if (sizeof($aTerms))
1341 $sSQL = "select place_id, ";
1342 $sSQL .= $sExactMatchSQL;
1343 $sSQL .= " from search_name";
1344 $sSQL .= " where ".join(' and ',$aTerms);
1345 $sSQL .= " order by ".join(', ',$aOrder);
1346 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1347 $sSQL .= " limit 20";
1348 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1349 $sSQL .= " limit 1";
1351 $sSQL .= " limit ".$this->iLimit;
1353 if (CONST_Debug) { var_dump($sSQL); }
1354 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1355 if (PEAR::IsError($aViewBoxPlaceIDs))
1357 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1359 //var_dump($aViewBoxPlaceIDs);
1360 // Did we have an viewbox matches?
1361 $aPlaceIDs = array();
1362 $bViewBoxMatch = false;
1363 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1365 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1366 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1367 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1368 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1369 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1370 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1373 //var_Dump($aPlaceIDs);
1376 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1378 $aRoadPlaceIDs = $aPlaceIDs;
1379 $sPlaceIDs = join(',',$aPlaceIDs);
1381 // Now they are indexed look for a house attached to a street we found
1382 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1383 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1384 if (sizeof($this->aExcludePlaceIDs))
1386 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1388 $sSQL .= " limit $this->iLimit";
1389 if (CONST_Debug) var_dump($sSQL);
1390 $aPlaceIDs = $this->oDB->getCol($sSQL);
1392 // If not try the aux fallback table
1393 if (!sizeof($aPlaceIDs))
1395 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1396 if (sizeof($this->aExcludePlaceIDs))
1398 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1400 //$sSQL .= " limit $this->iLimit";
1401 if (CONST_Debug) var_dump($sSQL);
1402 $aPlaceIDs = $this->oDB->getCol($sSQL);
1405 if (!sizeof($aPlaceIDs))
1407 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1408 if (sizeof($this->aExcludePlaceIDs))
1410 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1412 //$sSQL .= " limit $this->iLimit";
1413 if (CONST_Debug) var_dump($sSQL);
1414 $aPlaceIDs = $this->oDB->getCol($sSQL);
1417 // Fallback to the road
1418 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1420 $aPlaceIDs = $aRoadPlaceIDs;
1425 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1427 $sPlaceIDs = join(',',$aPlaceIDs);
1428 $aClassPlaceIDs = array();
1430 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1432 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1433 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1434 $sSQL .= " and linked_place_id is null";
1435 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1436 $sSQL .= " order by rank_search asc limit $this->iLimit";
1437 if (CONST_Debug) var_dump($sSQL);
1438 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1441 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1443 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1444 $bCacheTable = $this->oDB->getOne($sSQL);
1446 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1448 if (CONST_Debug) var_dump($sSQL);
1449 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1451 // For state / country level searches the normal radius search doesn't work very well
1452 $sPlaceGeom = false;
1453 if ($this->iMaxRank < 9 && $bCacheTable)
1455 // Try and get a polygon to search in instead
1456 $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";
1457 if (CONST_Debug) var_dump($sSQL);
1458 $sPlaceGeom = $this->oDB->getOne($sSQL);
1467 $this->iMaxRank += 5;
1468 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1469 if (CONST_Debug) var_dump($sSQL);
1470 $aPlaceIDs = $this->oDB->getCol($sSQL);
1471 $sPlaceIDs = join(',',$aPlaceIDs);
1474 if ($sPlaceIDs || $sPlaceGeom)
1480 // More efficient - can make the range bigger
1484 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1485 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1486 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1488 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1489 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1492 $sSQL .= ",placex as f where ";
1493 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1498 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1500 if (sizeof($this->aExcludePlaceIDs))
1502 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1504 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1505 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1506 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1507 $sSQL .= " limit $this->iLimit";
1508 if (CONST_Debug) var_dump($sSQL);
1509 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1513 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1516 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1517 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1519 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1520 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1521 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1522 if (sizeof($this->aExcludePlaceIDs))
1524 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1526 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1527 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1528 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1529 $sSQL .= " limit $this->iLimit";
1530 if (CONST_Debug) var_dump($sSQL);
1531 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1536 $aPlaceIDs = $aClassPlaceIDs;
1542 if (PEAR::IsError($aPlaceIDs))
1544 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1547 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1549 foreach($aPlaceIDs as $iPlaceID)
1551 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1553 if ($iQueryLoop > 20) break;
1556 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1558 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1559 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1560 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1561 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1562 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1563 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1564 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1565 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1567 if (CONST_Debug) var_dump($sSQL);
1568 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1572 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1573 if ($iGroupLoop > 4) break;
1574 if ($iQueryLoop > 30) break;
1577 // Did we find anything?
1578 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1580 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1586 // Just interpret as a reverse geocode
1587 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1589 $aSearchResults = $this->getDetails(array($iPlaceID));
1591 $aSearchResults = array();
1595 if (!sizeof($aSearchResults))
1597 if ($this->bFallback)
1599 if ($this->fallbackStructuredQuery())
1601 return $this->lookup();
1608 $aClassType = getClassTypesWithImportance();
1609 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1610 foreach($aRecheckWords as $i => $sWord)
1612 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1615 if (CONST_Debug) { echo '<i>Recheck words:<\i>'; var_dump($aRecheckWords); }
1617 foreach($aSearchResults as $iResNum => $aResult)
1620 $fDiameter = 0.0001;
1622 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1623 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1625 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'];
1627 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1628 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1630 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1632 $fRadius = $fDiameter / 2;
1634 if (CONST_Search_AreaPolygons)
1636 // Get the bounding box and outline polygon
1637 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1638 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1639 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
1640 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
1641 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1642 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1643 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1644 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1645 $sFrom = " from placex where place_id = ".$aResult['place_id'];
1646 if ($this->fPolygonSimplificationThreshold > 0)
1648 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
1655 $aPointPolygon = $this->oDB->getRow($sSQL);
1656 if (PEAR::IsError($aPointPolygon))
1658 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1661 if ($aPointPolygon['place_id'])
1663 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1664 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1665 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1666 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1668 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1670 $aResult['lat'] = $aPointPolygon['centrelat'];
1671 $aResult['lon'] = $aPointPolygon['centrelon'];
1674 if ($this->bIncludePolygonAsPoints)
1676 // Translate geometry string to point array
1677 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1679 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1681 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1683 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1685 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1687 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
1688 $fStepSize = (2*pi())/$iSteps;
1689 $aPolyPoints = array();
1690 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1692 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1697 // Output data suitable for display (points and a bounding box)
1698 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1700 $aResult['aPolyPoints'] = array();
1701 foreach($aPolyPoints as $aPoint)
1703 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1707 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
1709 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1710 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1712 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
1714 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1715 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1717 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1721 if ($aResult['extra_place'] == 'city')
1723 $aResult['class'] = 'place';
1724 $aResult['type'] = 'city';
1725 $aResult['rank_search'] = 16;
1728 if (!isset($aResult['aBoundingBox']))
1730 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1731 $fStepSize = (2*pi())/$iSteps;
1732 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1733 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1734 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1735 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1737 // Output data suitable for display (points and a bounding box)
1738 if ($this->bIncludePolygonAsPoints)
1740 $aPolyPoints = array();
1741 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1743 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1745 $aResult['aPolyPoints'] = array();
1746 foreach($aPolyPoints as $aPoint)
1748 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1751 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1754 // Is there an icon set for this type of result?
1755 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1756 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1758 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1761 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1762 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1764 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1766 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1767 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1769 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1772 if ($this->bIncludeAddressDetails)
1774 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1775 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1777 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1781 if ($this->bIncludeExtraTags)
1783 if ($aResult['extra'])
1785 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1789 $aResult['sExtraTags'] = array();
1793 if ($this->bIncludeNameDetails)
1795 if ($aResult['names'])
1797 $aResult['sNameDetails'] = json_decode($aResult['names']);
1801 $aResult['sNameDetails'] = array();
1805 // Adjust importance for the number of exact string matches in the result
1806 $aResult['importance'] = max(0.001,$aResult['importance']);
1808 $sAddress = $aResult['langaddress'];
1809 foreach($aRecheckWords as $i => $sWord)
1811 if (stripos($sAddress, $sWord)!==false)
1814 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1818 $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
1820 $aResult['name'] = $aResult['langaddress'];
1821 // secondary ordering (for results with same importance (the smaller the better):
1822 // - approximate importance of address parts
1823 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1824 // - number of exact matches from the query
1825 if (isset($this->exactMatchCache[$aResult['place_id']]))
1826 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1827 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1828 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1829 // - importance of the class/type
1830 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1831 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1833 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1837 $aResult['foundorder'] += 0.01;
1839 if (CONST_Debug) { var_dump($aResult); }
1840 $aSearchResults[$iResNum] = $aResult;
1842 uasort($aSearchResults, 'byImportance');
1844 $aOSMIDDone = array();
1845 $aClassTypeNameDone = array();
1846 $aToFilter = $aSearchResults;
1847 $aSearchResults = array();
1850 foreach($aToFilter as $iResNum => $aResult)
1852 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1855 $fLat = $aResult['lat'];
1856 $fLon = $aResult['lon'];
1857 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1860 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1861 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1863 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1864 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1865 $aSearchResults[] = $aResult;
1868 // Absolute limit on number of results
1869 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1872 return $aSearchResults;