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 /* Perform the actual query lookup.
446 Returns an ordered list of results, each with the following fields:
447 osm_type: type of corresponding OSM object
451 P - postcode (internally computed)
452 osm_id: id of corresponding OSM object
453 class: general object class (corresponds to tag key of primary OSM tag)
454 type: subclass of object (corresponds to tag value of primary OSM tag)
455 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
456 rank_search: rank in search hierarchy
457 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
458 rank_address: rank in address hierarchy (determines orer in address)
459 place_id: internal key (may differ between different instances)
460 country_code: ISO country code
461 langaddress: localized full address
462 placename: localized name of object
463 ref: content of ref tag (if available)
466 importance: importance of place based on Wikipedia link count
467 addressimportance: cumulated importance of address elements
468 extra_place: type of place (for admin boundaries, if there is a place tag)
469 aBoundingBox: bounding Box
470 label: short description of the object class/type (English only)
471 name: full name (currently the same as langaddress)
472 foundorder: secondary ordering for places with same importance
476 if (!$this->sQuery && !$this->aStructuredQuery) return false;
478 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
480 $sCountryCodesSQL = false;
481 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
483 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
486 $sQuery = $this->sQuery;
488 // Conflicts between US state abreviations and various words for 'the' in different languages
489 if (isset($this->aLangPrefOrder['name:en']))
491 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
492 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
493 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
497 $sViewboxCentreSQL = false;
498 $bBoundingBoxSearch = false;
501 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
502 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
503 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
504 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
505 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
506 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
508 $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)";
509 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
510 $bBoundingBoxSearch = $this->bBoundedSearch;
514 if ($this->aRoutePoints)
516 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
518 foreach($this->aRoutePoints as $aPoint)
520 if (!$bFirst) $sViewboxCentreSQL .= ",";
521 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
524 $sViewboxCentreSQL .= ")'::geometry,4326)";
526 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
527 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
528 if (PEAR::isError($this->sViewboxSmallSQL))
530 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
532 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
534 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
535 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
536 if (PEAR::isError($this->sViewboxLargeSQL))
538 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
540 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
541 $bBoundingBoxSearch = $this->bBoundedSearch;
544 // Do we have anything that looks like a lat/lon pair?
545 if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
546 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
547 $sQuery = $aLooksLike['query'];
550 $aSearchResults = array();
551 if ($sQuery || $this->aStructuredQuery)
553 // Start with a blank search
555 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
556 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
557 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
560 // Do we have a radius search?
561 $sNearPointSQL = false;
562 if ($this->aNearPoint)
564 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
565 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
566 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
567 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
570 // Any 'special' terms in the search?
571 $bSpecialTerms = false;
572 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
573 $aSpecialTerms = array();
574 foreach($aSpecialTermsRaw as $aSpecialTerm)
576 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
577 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
580 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
581 $aSpecialTerms = array();
582 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
584 $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
585 unset($aStructuredQuery['amenity']);
587 foreach($aSpecialTermsRaw as $aSpecialTerm)
589 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
590 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
591 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
592 $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';
593 if (CONST_Debug) var_Dump($sSQL);
594 $aSearchWords = $this->oDB->getAll($sSQL);
595 $aNewSearches = array();
596 foreach($aSearches as $aSearch)
598 foreach($aSearchWords as $aSearchTerm)
600 $aNewSearch = $aSearch;
601 if ($aSearchTerm['country_code'])
603 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
604 $aNewSearches[] = $aNewSearch;
605 $bSpecialTerms = true;
607 if ($aSearchTerm['class'])
609 $aNewSearch['sClass'] = $aSearchTerm['class'];
610 $aNewSearch['sType'] = $aSearchTerm['type'];
611 $aNewSearches[] = $aNewSearch;
612 $bSpecialTerms = true;
616 $aSearches = $aNewSearches;
619 // Split query into phrases
620 // Commas are used to reduce the search space by indicating where phrases split
621 if ($this->aStructuredQuery)
623 $aPhrases = $this->aStructuredQuery;
624 $bStructuredPhrases = true;
628 $aPhrases = explode(',',$sQuery);
629 $bStructuredPhrases = false;
632 // Convert each phrase to standard form
633 // Create a list of standard words
634 // Get all 'sets' of words
635 // Generate a complete list of all
637 foreach($aPhrases as $iPhrase => $sPhrase)
639 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
640 if (PEAR::isError($aPhrase))
642 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
643 if (CONST_Debug) var_dump($aPhrase);
646 if (trim($aPhrase['string']))
648 $aPhrases[$iPhrase] = $aPhrase;
649 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
650 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
651 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
655 unset($aPhrases[$iPhrase]);
659 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
660 $aPhraseTypes = array_keys($aPhrases);
661 $aPhrases = array_values($aPhrases);
663 if (sizeof($aTokens))
665 // Check which tokens we have, get the ID numbers
666 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
667 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
669 if (CONST_Debug) var_Dump($sSQL);
671 $aValidTokens = array();
672 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
673 else $aDatabaseWords = array();
674 if (PEAR::IsError($aDatabaseWords))
676 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
678 $aPossibleMainWordIDs = array();
679 $aWordFrequencyScores = array();
680 foreach($aDatabaseWords as $aToken)
682 // Very special case - require 2 letter country param to match the country code found
683 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
684 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
689 if (isset($aValidTokens[$aToken['word_token']]))
691 $aValidTokens[$aToken['word_token']][] = $aToken;
695 $aValidTokens[$aToken['word_token']] = array($aToken);
697 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
698 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
700 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
702 // Try and calculate GB postcodes we might be missing
703 foreach($aTokens as $sToken)
705 // Source of gb postcodes is now definitive - always use
706 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
708 if (substr($aData[1],-2,1) != ' ')
710 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
711 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
713 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
714 if ($aGBPostcodeLocation)
716 $aValidTokens[$sToken] = $aGBPostcodeLocation;
719 // US ZIP+4 codes - if there is no token,
720 // merge in the 5-digit ZIP code
721 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
723 if (isset($aValidTokens[$aData[1]]))
725 foreach($aValidTokens[$aData[1]] as $aToken)
727 if (!$aToken['class'])
729 if (isset($aValidTokens[$sToken]))
731 $aValidTokens[$sToken][] = $aToken;
735 $aValidTokens[$sToken] = array($aToken);
743 foreach($aTokens as $sToken)
745 // Unknown single word token with a number - assume it is a house number
746 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
748 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
752 // Any words that have failed completely?
755 // Start the search process
756 $aResultPlaceIDs = array();
759 Calculate all searches using aValidTokens i.e.
760 'Wodsworth Road, Sheffield' =>
764 0 1 (wodsworth)(road)
767 Score how good the search is so they can be ordered
769 foreach($aPhrases as $iPhrase => $sPhrase)
771 $aNewPhraseSearches = array();
772 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
773 else $sPhraseType = '';
775 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
777 // Too many permutations - too expensive
778 if ($iWordSet > 120) break;
780 $aWordsetSearches = $aSearches;
782 // Add all words from this wordset
783 foreach($aWordset as $iToken => $sToken)
785 //echo "<br><b>$sToken</b>";
786 $aNewWordsetSearches = array();
788 foreach($aWordsetSearches as $aCurrentSearch)
791 //var_dump($aCurrentSearch);
794 // If the token is valid
795 if (isset($aValidTokens[' '.$sToken]))
797 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
799 $aSearch = $aCurrentSearch;
800 $aSearch['iSearchRank']++;
801 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
803 if ($aSearch['sCountryCode'] === false)
805 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
806 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
807 // If reverse order is enabled, it may appear at the beginning as well.
808 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) &&
809 (!$this->bReverseInPlan || $iToken > 0 || $iPhrase > 0))
811 $aSearch['iSearchRank'] += 5;
813 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
816 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
818 if ($aSearch['fLat'] === '')
820 $aSearch['fLat'] = $aSearchTerm['lat'];
821 $aSearch['fLon'] = $aSearchTerm['lon'];
822 $aSearch['fRadius'] = $aSearchTerm['radius'];
823 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
826 elseif ($sPhraseType == 'postalcode')
828 // 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
829 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
831 // If we already have a name try putting the postcode first
832 if (sizeof($aSearch['aName']))
834 $aNewSearch = $aSearch;
835 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
836 $aNewSearch['aName'] = array();
837 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
838 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
841 if (sizeof($aSearch['aName']))
843 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
845 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
849 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
850 $aSearch['iSearchRank'] += 1000; // skip;
855 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
856 //$aSearch['iNamePhrase'] = $iPhrase;
858 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
862 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
864 if ($aSearch['sHouseNumber'] === '')
866 $aSearch['sHouseNumber'] = $sToken;
867 // sanity check: if the housenumber is not mainly made
868 // up of numbers, add a penalty
869 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
870 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
872 // Fall back to not searching for this item (better than nothing)
873 $aSearch = $aCurrentSearch;
874 $aSearch['iSearchRank'] += 1;
875 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
879 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
881 if ($aSearch['sClass'] === '')
883 $aSearch['sOperator'] = $aSearchTerm['operator'];
884 $aSearch['sClass'] = $aSearchTerm['class'];
885 $aSearch['sType'] = $aSearchTerm['type'];
886 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
887 else $aSearch['sOperator'] = 'near'; // near = in for the moment
888 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
890 // Do we have a shortcut id?
891 if ($aSearch['sOperator'] == 'name')
893 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
894 if ($iAmenityID = $this->oDB->getOne($sSQL))
896 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
897 $aSearch['aName'][$iAmenityID] = $iAmenityID;
898 $aSearch['sClass'] = '';
899 $aSearch['sType'] = '';
902 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
905 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
907 if (sizeof($aSearch['aName']))
909 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
911 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
915 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
916 $aSearch['iSearchRank'] += 1000; // skip;
921 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
922 //$aSearch['iNamePhrase'] = $iPhrase;
924 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
928 if (isset($aValidTokens[$sToken]))
930 // Allow searching for a word - but at extra cost
931 foreach($aValidTokens[$sToken] as $aSearchTerm)
933 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
935 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strlen($sToken) >= 4)
937 $aSearch = $aCurrentSearch;
938 $aSearch['iSearchRank'] += 1;
939 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
941 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
942 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
944 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
946 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
948 if (empty($aSearchTermToken['country_code'])
949 && empty($aSearchTermToken['lat'])
950 && empty($aSearchTermToken['class']))
952 $aSearch = $aCurrentSearch;
953 $aSearch['iSearchRank'] += 1;
954 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
955 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
961 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
962 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
966 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
968 $aSearch = $aCurrentSearch;
969 $aSearch['iSearchRank'] += 2;
970 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
971 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
972 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
974 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
975 $aSearch['iNamePhrase'] = $iPhrase;
976 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
983 // Allow skipping a word - but at EXTREAM cost
984 //$aSearch = $aCurrentSearch;
985 //$aSearch['iSearchRank']+=100;
986 //$aNewWordsetSearches[] = $aSearch;
990 usort($aNewWordsetSearches, 'bySearchRank');
991 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
993 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
995 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
996 usort($aNewPhraseSearches, 'bySearchRank');
998 $aSearchHash = array();
999 foreach($aNewPhraseSearches as $iSearch => $aSearch)
1001 $sHash = serialize($aSearch);
1002 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
1003 else $aSearchHash[$sHash] = 1;
1006 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
1009 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1010 $aGroupedSearches = array();
1011 foreach($aNewPhraseSearches as $aSearch)
1013 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1015 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1016 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1019 ksort($aGroupedSearches);
1022 $aSearches = array();
1023 foreach($aGroupedSearches as $iScore => $aNewSearches)
1025 $iSearchCount += sizeof($aNewSearches);
1026 $aSearches = array_merge($aSearches, $aNewSearches);
1027 if ($iSearchCount > 50) break;
1030 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1037 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1038 $aGroupedSearches = array();
1039 foreach($aSearches as $aSearch)
1041 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1043 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1044 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1047 ksort($aGroupedSearches);
1050 if (CONST_Debug) var_Dump($aGroupedSearches);
1052 if ($this->bReverseInPlan)
1054 $aCopyGroupedSearches = $aGroupedSearches;
1055 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1057 foreach($aSearches as $iSearch => $aSearch)
1059 if (sizeof($aSearch['aAddress']))
1061 $iReverseItem = array_pop($aSearch['aAddress']);
1062 if (isset($aPossibleMainWordIDs[$iReverseItem]))
1064 $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
1065 $aSearch['aName'] = array($iReverseItem);
1066 $aGroupedSearches[$iGroup][] = $aSearch;
1068 //$aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
1069 //$aGroupedSearches[$iGroup][] = $aReverseSearch;
1075 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
1077 $aCopyGroupedSearches = $aGroupedSearches;
1078 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1080 foreach($aSearches as $iSearch => $aSearch)
1082 $aReductionsList = array($aSearch['aAddress']);
1083 $iSearchRank = $aSearch['iSearchRank'];
1084 while(sizeof($aReductionsList) > 0)
1087 if ($iSearchRank > iMaxRank) break 3;
1088 $aNewReductionsList = array();
1089 foreach($aReductionsList as $aReductionsWordList)
1091 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1093 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1094 $aReverseSearch = $aSearch;
1095 $aSearch['aAddress'] = $aReductionsWordListResult;
1096 $aSearch['iSearchRank'] = $iSearchRank;
1097 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1098 if (sizeof($aReductionsWordListResult) > 0)
1100 $aNewReductionsList[] = $aReductionsWordListResult;
1104 $aReductionsList = $aNewReductionsList;
1108 ksort($aGroupedSearches);
1111 // Filter out duplicate searches
1112 $aSearchHash = array();
1113 foreach($aGroupedSearches as $iGroup => $aSearches)
1115 foreach($aSearches as $iSearch => $aSearch)
1117 $sHash = serialize($aSearch);
1118 if (isset($aSearchHash[$sHash]))
1120 unset($aGroupedSearches[$iGroup][$iSearch]);
1121 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1125 $aSearchHash[$sHash] = 1;
1130 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1134 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1137 foreach($aSearches as $aSearch)
1141 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1142 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1144 // No location term?
1145 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1147 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1149 // Just looking for a country by code - look it up
1150 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1152 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1153 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1154 $sSQL .= " order by st_area(geometry) desc limit 1";
1155 if (CONST_Debug) var_dump($sSQL);
1156 $aPlaceIDs = $this->oDB->getCol($sSQL);
1160 $aPlaceIDs = array();
1165 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1166 if (!$aSearch['sClass']) continue;
1167 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1168 if ($this->oDB->getOne($sSQL))
1170 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1171 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1172 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1173 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1174 if (sizeof($this->aExcludePlaceIDs))
1176 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1178 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1179 $sSQL .= " limit $this->iLimit";
1180 if (CONST_Debug) var_dump($sSQL);
1181 $aPlaceIDs = $this->oDB->getCol($sSQL);
1183 // If excluded place IDs are given, it is fair to assume that
1184 // there have been results in the small box, so no further
1185 // expansion in that case.
1186 // Also don't expand if bounded results were requested.
1187 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1189 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1190 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1191 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1192 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1193 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1194 $sSQL .= " limit $this->iLimit";
1195 if (CONST_Debug) var_dump($sSQL);
1196 $aPlaceIDs = $this->oDB->getCol($sSQL);
1201 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1202 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1203 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1204 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1205 $sSQL .= " limit $this->iLimit";
1206 if (CONST_Debug) var_dump($sSQL);
1207 $aPlaceIDs = $this->oDB->getCol($sSQL);
1213 $aPlaceIDs = array();
1215 // First we need a position, either aName or fLat or both
1219 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1220 // they might be right - but they are just too darned expensive to run
1221 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1222 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1223 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1225 // For infrequent name terms disable index usage for address
1226 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1227 sizeof($aSearch['aName']) == 1 &&
1228 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1230 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1234 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1235 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1238 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1239 if ($aSearch['sHouseNumber'])
1241 $aTerms[] = "address_rank between 16 and 27";
1245 if ($this->iMinAddressRank > 0)
1247 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1249 if ($this->iMaxAddressRank < 30)
1251 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1254 if ($aSearch['fLon'] && $aSearch['fLat'])
1256 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1257 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1259 if (sizeof($this->aExcludePlaceIDs))
1261 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1263 if ($sCountryCodesSQL)
1265 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1268 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1269 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1271 if ($aSearch['sHouseNumber'])
1273 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1277 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1279 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1280 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1282 $aOrder[] = "$sImportanceSQL DESC";
1283 if (sizeof($aSearch['aFullNameAddress']))
1285 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1286 $aOrder[] = 'exactmatch DESC';
1288 $sExactMatchSQL = '0::int as exactmatch';
1291 if (sizeof($aTerms))
1293 $sSQL = "select place_id, ";
1294 $sSQL .= $sExactMatchSQL;
1295 $sSQL .= " from search_name";
1296 $sSQL .= " where ".join(' and ',$aTerms);
1297 $sSQL .= " order by ".join(', ',$aOrder);
1298 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1299 $sSQL .= " limit 50";
1300 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1301 $sSQL .= " limit 1";
1303 $sSQL .= " limit ".$this->iLimit;
1305 if (CONST_Debug) { var_dump($sSQL); }
1306 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1307 if (PEAR::IsError($aViewBoxPlaceIDs))
1309 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1311 //var_dump($aViewBoxPlaceIDs);
1312 // Did we have an viewbox matches?
1313 $aPlaceIDs = array();
1314 $bViewBoxMatch = false;
1315 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1317 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1318 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1319 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1320 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1321 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1322 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1325 //var_Dump($aPlaceIDs);
1328 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1330 $aRoadPlaceIDs = $aPlaceIDs;
1331 $sPlaceIDs = join(',',$aPlaceIDs);
1333 // Now they are indexed look for a house attached to a street we found
1334 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1335 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1336 if (sizeof($this->aExcludePlaceIDs))
1338 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1340 $sSQL .= " limit $this->iLimit";
1341 if (CONST_Debug) var_dump($sSQL);
1342 $aPlaceIDs = $this->oDB->getCol($sSQL);
1344 // If not try the aux fallback table
1345 if (!sizeof($aPlaceIDs))
1347 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1348 if (sizeof($this->aExcludePlaceIDs))
1350 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1352 //$sSQL .= " limit $this->iLimit";
1353 if (CONST_Debug) var_dump($sSQL);
1354 $aPlaceIDs = $this->oDB->getCol($sSQL);
1357 if (!sizeof($aPlaceIDs))
1359 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1360 if (sizeof($this->aExcludePlaceIDs))
1362 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1364 //$sSQL .= " limit $this->iLimit";
1365 if (CONST_Debug) var_dump($sSQL);
1366 $aPlaceIDs = $this->oDB->getCol($sSQL);
1369 // Fallback to the road
1370 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1372 $aPlaceIDs = $aRoadPlaceIDs;
1377 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1379 $sPlaceIDs = join(',',$aPlaceIDs);
1380 $aClassPlaceIDs = array();
1382 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1384 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1385 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1386 $sSQL .= " and linked_place_id is null";
1387 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1388 $sSQL .= " order by rank_search asc limit $this->iLimit";
1389 if (CONST_Debug) var_dump($sSQL);
1390 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1393 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1395 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1396 $bCacheTable = $this->oDB->getOne($sSQL);
1398 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1400 if (CONST_Debug) var_dump($sSQL);
1401 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1403 // For state / country level searches the normal radius search doesn't work very well
1404 $sPlaceGeom = false;
1405 if ($this->iMaxRank < 9 && $bCacheTable)
1407 // Try and get a polygon to search in instead
1408 $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";
1409 if (CONST_Debug) var_dump($sSQL);
1410 $sPlaceGeom = $this->oDB->getOne($sSQL);
1419 $this->iMaxRank += 5;
1420 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1421 if (CONST_Debug) var_dump($sSQL);
1422 $aPlaceIDs = $this->oDB->getCol($sSQL);
1423 $sPlaceIDs = join(',',$aPlaceIDs);
1426 if ($sPlaceIDs || $sPlaceGeom)
1432 // More efficient - can make the range bigger
1436 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1437 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1438 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1440 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1441 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1444 $sSQL .= ",placex as f where ";
1445 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1450 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1452 if (sizeof($this->aExcludePlaceIDs))
1454 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1456 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1457 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1458 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1459 $sSQL .= " limit $this->iLimit";
1460 if (CONST_Debug) var_dump($sSQL);
1461 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1465 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1468 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1469 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1471 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1472 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1473 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1474 if (sizeof($this->aExcludePlaceIDs))
1476 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1478 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1479 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1480 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1481 $sSQL .= " limit $this->iLimit";
1482 if (CONST_Debug) var_dump($sSQL);
1483 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1488 $aPlaceIDs = $aClassPlaceIDs;
1494 if (PEAR::IsError($aPlaceIDs))
1496 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1499 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1501 foreach($aPlaceIDs as $iPlaceID)
1503 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1505 if ($iQueryLoop > 20) break;
1508 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1510 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1511 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1512 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1513 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1514 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1515 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1516 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1517 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1519 if (CONST_Debug) var_dump($sSQL);
1520 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1524 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1525 if ($iGroupLoop > 4) break;
1526 if ($iQueryLoop > 30) break;
1529 // Did we find anything?
1530 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1532 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1538 // Just interpret as a reverse geocode
1539 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1541 $aSearchResults = $this->getDetails(array($iPlaceID));
1543 $aSearchResults = array();
1547 if (!sizeof($aSearchResults))
1549 if ($this->bFallback)
1551 if ($this->fallbackStructuredQuery())
1553 return $this->lookup();
1560 $aClassType = getClassTypesWithImportance();
1561 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1562 foreach($aRecheckWords as $i => $sWord)
1564 if (!$sWord) unset($aRecheckWords[$i]);
1567 foreach($aSearchResults as $iResNum => $aResult)
1569 if (CONST_Search_AreaPolygons)
1571 // Get the bounding box and outline polygon
1572 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1573 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1574 $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1575 $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1576 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1577 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1578 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1579 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1580 $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1581 $aPointPolygon = $this->oDB->getRow($sSQL);
1582 if (PEAR::IsError($aPointPolygon))
1584 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1587 if ($aPointPolygon['place_id'])
1589 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1590 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1591 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1592 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1594 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1596 $aResult['lat'] = $aPointPolygon['centrelat'];
1597 $aResult['lon'] = $aPointPolygon['centrelon'];
1600 if ($this->bIncludePolygonAsPoints)
1602 // Translate geometary string to point array
1603 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1605 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1607 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1609 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1611 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1614 $iSteps = ($fRadius * 40000)^2;
1615 $fStepSize = (2*pi())/$iSteps;
1616 $aPolyPoints = array();
1617 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1619 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1621 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1622 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1623 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1624 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1628 // Output data suitable for display (points and a bounding box)
1629 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1631 $aResult['aPolyPoints'] = array();
1632 foreach($aPolyPoints as $aPoint)
1634 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1637 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1641 if ($aResult['extra_place'] == 'city')
1643 $aResult['class'] = 'place';
1644 $aResult['type'] = 'city';
1645 $aResult['rank_search'] = 16;
1648 if (!isset($aResult['aBoundingBox']))
1651 $fDiameter = 0.0001;
1653 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1654 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1656 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1658 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1659 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1661 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1663 $fRadius = $fDiameter / 2;
1665 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1666 $fStepSize = (2*pi())/$iSteps;
1667 $aPolyPoints = array();
1668 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1670 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1672 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1673 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1674 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1675 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1677 // Output data suitable for display (points and a bounding box)
1678 if ($this->bIncludePolygonAsPoints)
1680 $aResult['aPolyPoints'] = array();
1681 foreach($aPolyPoints as $aPoint)
1683 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1686 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1689 // Is there an icon set for this type of result?
1690 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1691 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1693 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1696 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1697 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1699 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1701 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1702 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1704 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1707 if ($this->bIncludeAddressDetails)
1709 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1710 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1712 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1716 // Adjust importance for the number of exact string matches in the result
1717 $aResult['importance'] = max(0.001,$aResult['importance']);
1719 $sAddress = $aResult['langaddress'];
1720 foreach($aRecheckWords as $i => $sWord)
1722 if (stripos($sAddress, $sWord)!==false)
1725 if (preg_match("/(^|,)\s*$sWord\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1729 $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
1731 $aResult['name'] = $aResult['langaddress'];
1732 // secondary ordering (for results with same importance (the smaller the better):
1733 // - approximate importance of address parts
1734 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1735 // - number of exact matches from the query
1736 if (isset($this->exactMatchCache[$aResult['place_id']]))
1737 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1738 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1739 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1740 // - importance of the class/type
1741 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1742 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1744 $aResult['foundorder'] = $aResult['foundorder'] + 0.000001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1748 $aResult['foundorder'] = $aResult['foundorder'] + 0.001;
1750 $aSearchResults[$iResNum] = $aResult;
1752 uasort($aSearchResults, 'byImportance');
1754 $aOSMIDDone = array();
1755 $aClassTypeNameDone = array();
1756 $aToFilter = $aSearchResults;
1757 $aSearchResults = array();
1760 foreach($aToFilter as $iResNum => $aResult)
1762 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1763 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1766 $fLat = $aResult['lat'];
1767 $fLon = $aResult['lon'];
1768 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1771 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1772 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1774 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1775 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1776 $aSearchResults[] = $aResult;
1779 // Absolute limit on number of results
1780 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1783 return $aSearchResults;