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 = true;
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;
207 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
209 $sValue = trim($sValue);
210 if (!$sValue) return false;
211 $this->aStructuredQuery[$sKey] = $sValue;
212 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
214 $this->iMinAddressRank = $iNewMinAddressRank;
215 $this->iMaxAddressRank = $iNewMaxAddressRank;
217 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
221 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
223 $this->sQuery = false;
226 $this->iMinAddressRank = 0;
227 $this->iMaxAddressRank = 30;
228 $this->aAddressRankList = array();
230 $this->aStructuredQuery = array();
231 $this->sAllowedTypesSQLList = '';
233 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
234 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
235 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
236 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
237 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
238 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
239 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
241 if (sizeof($this->aStructuredQuery) > 0)
243 $this->sQuery = join(', ', $this->aStructuredQuery);
244 if ($this->iMaxAddressRank < 30)
246 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
251 function fallbackStructuredQuery()
253 if (!$this->aStructuredQuery) return false;
255 $aParams = $this->aStructuredQuery;
257 if (sizeof($aParams) == 1) return false;
259 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
261 foreach($aOrderToFallback as $sType)
263 if (isset($aParams[$sType]))
265 unset($aParams[$sType]);
266 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
274 function getDetails($aPlaceIDs)
276 if (sizeof($aPlaceIDs) == 0) return array();
278 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
280 // Get the details for display (is this a redundant extra step?)
281 $sPlaceIDs = join(',',$aPlaceIDs);
283 $sImportanceSQL = '';
284 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
285 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
287 $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,";
288 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
289 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
290 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
291 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
292 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
293 $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, ";
294 $sSQL .= "(extratags->'place') as extra_place ";
295 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
296 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
297 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
298 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
300 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
301 $sSQL .= "and linked_place_id is null ";
302 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
303 if (!$this->bDeDupe) $sSQL .= ",place_id";
304 $sSQL .= ",langaddress ";
305 $sSQL .= ",placename ";
307 $sSQL .= ",extratags->'place' ";
309 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
312 $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,";
313 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
314 $sSQL .= "null as placename,";
315 $sSQL .= "null as ref,";
316 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
317 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
318 $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, ";
319 $sSQL .= "null as extra_place ";
320 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
321 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
322 $sSQL .= "group by place_id";
323 if (!$this->bDeDupe) $sSQL .= ",place_id ";
326 $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,";
327 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
328 $sSQL .= "null as placename,";
329 $sSQL .= "null as ref,";
330 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
331 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
332 $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, ";
333 $sSQL .= "null as extra_place ";
334 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
335 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
336 $sSQL .= "group by place_id";
337 if (!$this->bDeDupe) $sSQL .= ",place_id";
338 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
342 $sSQL .= " order by importance desc";
343 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
344 $aSearchResults = $this->oDB->getAll($sSQL);
346 if (PEAR::IsError($aSearchResults))
348 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
351 return $aSearchResults;
354 /* Perform the actual query lookup.
356 Returns an ordered list of results, each with the following fields:
357 osm_type: type of corresponding OSM object
361 P - postcode (internally computed)
362 osm_id: id of corresponding OSM object
363 class: general object class (corresponds to tag key of primary OSM tag)
364 type: subclass of object (corresponds to tag value of primary OSM tag)
365 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
366 rank_search: rank in search hierarchy
367 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
368 rank_address: rank in address hierarchy (determines orer in address)
369 place_id: internal key (may differ between different instances)
370 country_code: ISO country code
371 langaddress: localized full address
372 placename: localized name of object
373 ref: content of ref tag (if available)
376 importance: importance of place based on Wikipedia link count
377 addressimportance: cumulated importance of address elements
378 extra_place: type of place (for admin boundaries, if there is a place tag)
379 aBoundingBox: bounding Box
380 label: short description of the object class/type (English only)
381 name: full name (currently the same as langaddress)
382 foundorder: secondary ordering for places with same importance
386 if (!$this->sQuery && !$this->aStructuredQuery) return false;
388 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
390 $sCountryCodesSQL = false;
391 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
393 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
396 // Hack to make it handle "new york, ny" (and variants) correctly
397 //$sQuery = str_ireplace(array('New York, ny','new york, new york', 'New York ny','new york new york'), 'new york city, ny', $this->sQuery);
398 $sQuery = $this->sQuery;
400 // Conflicts between US state abreviations and various words for 'the' in different languages
401 if (isset($this->aLangPrefOrder['name:en']))
403 $sQuery = preg_replace('/,\s*il\s*(,|$)/',', illinois\1', $sQuery);
404 $sQuery = preg_replace('/,\s*al\s*(,|$)/',', alabama\1', $sQuery);
405 $sQuery = preg_replace('/,\s*la\s*(,|$)/',', louisiana\1', $sQuery);
410 $bBoundingBoxSearch = false;
413 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
414 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
415 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
416 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
417 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
418 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
420 $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)";
421 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
422 $bBoundingBoxSearch = $this->bBoundedSearch;
426 if ($this->aRoutePoints)
428 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
430 foreach($this->aRoutePoints as $aPoint)
432 if (!$bFirst) $sViewboxCentreSQL .= ",";
433 $sViewboxCentreSQL .= $aPoint[1].' '.$aPoint[0];
436 $sViewboxCentreSQL .= ")'::geometry,4326)";
438 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
439 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
440 if (PEAR::isError($this->sViewboxSmallSQL))
442 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
444 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
446 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
447 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
448 if (PEAR::isError($this->sViewboxLargeSQL))
450 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
452 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
453 $bBoundingBoxSearch = $this->bBoundedSearch;
456 // Do we have anything that looks like a lat/lon pair?
457 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
459 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
460 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
461 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
463 $this->setNearPoint(array($fQueryLat, $fQueryLon));
464 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
467 elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
469 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
470 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
471 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
473 $this->setNearPoint(array($fQueryLat, $fQueryLon));
474 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
477 elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
479 $fQueryLat = $aData[2];
480 $fQueryLon = $aData[3];
481 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
483 $this->setNearPoint(array($fQueryLat, $fQueryLon));
484 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
488 $aSearchResults = array();
489 if ($sQuery || $this->aStructuredQuery)
491 // Start with a blank search
493 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
494 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
495 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
498 // Do we have a radius search?
499 $sNearPointSQL = false;
500 if ($this->aNearPoint)
502 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
503 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
504 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
505 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
508 // Any 'special' terms in the search?
509 $bSpecialTerms = false;
510 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
511 $aSpecialTerms = array();
512 foreach($aSpecialTermsRaw as $aSpecialTerm)
514 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
515 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
518 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
519 $aSpecialTerms = array();
520 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
522 $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
523 unset($aStructuredQuery['amenity']);
525 foreach($aSpecialTermsRaw as $aSpecialTerm)
527 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
528 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
529 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
530 $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';
531 if (CONST_Debug) var_Dump($sSQL);
532 $aSearchWords = $this->oDB->getAll($sSQL);
533 $aNewSearches = array();
534 foreach($aSearches as $aSearch)
536 foreach($aSearchWords as $aSearchTerm)
538 $aNewSearch = $aSearch;
539 if ($aSearchTerm['country_code'])
541 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
542 $aNewSearches[] = $aNewSearch;
543 $bSpecialTerms = true;
545 if ($aSearchTerm['class'])
547 $aNewSearch['sClass'] = $aSearchTerm['class'];
548 $aNewSearch['sType'] = $aSearchTerm['type'];
549 $aNewSearches[] = $aNewSearch;
550 $bSpecialTerms = true;
554 $aSearches = $aNewSearches;
557 // Split query into phrases
558 // Commas are used to reduce the search space by indicating where phrases split
559 if ($this->aStructuredQuery)
561 $aPhrases = $this->aStructuredQuery;
562 $bStructuredPhrases = true;
566 $aPhrases = explode(',',$sQuery);
567 $bStructuredPhrases = false;
570 // Convert each phrase to standard form
571 // Create a list of standard words
572 // Get all 'sets' of words
573 // Generate a complete list of all
575 foreach($aPhrases as $iPhrase => $sPhrase)
577 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
578 if (PEAR::isError($aPhrase))
580 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
581 if (CONST_Debug) var_dump($aPhrase);
584 if (trim($aPhrase['string']))
586 $aPhrases[$iPhrase] = $aPhrase;
587 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
588 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
589 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
593 unset($aPhrases[$iPhrase]);
597 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
598 $aPhraseTypes = array_keys($aPhrases);
599 $aPhrases = array_values($aPhrases);
601 if (sizeof($aTokens))
603 // Check which tokens we have, get the ID numbers
604 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
605 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
607 if (CONST_Debug) var_Dump($sSQL);
609 $aValidTokens = array();
610 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
611 else $aDatabaseWords = array();
612 if (PEAR::IsError($aDatabaseWords))
614 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
616 $aPossibleMainWordIDs = array();
617 $aWordFrequencyScores = array();
618 foreach($aDatabaseWords as $aToken)
620 // Very special case - require 2 letter country param to match the country code found
621 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
622 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
627 if (isset($aValidTokens[$aToken['word_token']]))
629 $aValidTokens[$aToken['word_token']][] = $aToken;
633 $aValidTokens[$aToken['word_token']] = array($aToken);
635 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
636 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
638 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
640 // Try and calculate GB postcodes we might be missing
641 foreach($aTokens as $sToken)
643 // Source of gb postcodes is now definitive - always use
644 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
646 if (substr($aData[1],-2,1) != ' ')
648 $aData[0] = substr($aData[0],0,strlen($aData[1]-1)).' '.substr($aData[0],strlen($aData[1]-1));
649 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
651 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
652 if ($aGBPostcodeLocation)
654 $aValidTokens[$sToken] = $aGBPostcodeLocation;
657 // US ZIP+4 codes - if there is no token,
658 // merge in the 5-digit ZIP code
659 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
661 if (isset($aValidTokens[$aData[1]]))
663 foreach($aValidTokens[$aData[1]] as $aToken)
665 if (!$aToken['class'])
667 if (isset($aValidTokens[$sToken]))
669 $aValidTokens[$sToken][] = $aToken;
673 $aValidTokens[$sToken] = array($aToken);
681 foreach($aTokens as $sToken)
683 // Unknown single word token with a number - assume it is a house number
684 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
686 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
690 // Any words that have failed completely?
693 // Start the search process
694 $aResultPlaceIDs = array();
697 Calculate all searches using aValidTokens i.e.
698 'Wodsworth Road, Sheffield' =>
702 0 1 (wodsworth)(road)
705 Score how good the search is so they can be ordered
707 foreach($aPhrases as $iPhrase => $sPhrase)
709 $aNewPhraseSearches = array();
710 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
711 else $sPhraseType = '';
713 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
715 // Too many permutations - too expensive
716 if ($iWordSet > 120) break;
718 $aWordsetSearches = $aSearches;
720 // Add all words from this wordset
721 foreach($aWordset as $iToken => $sToken)
723 //echo "<br><b>$sToken</b>";
724 $aNewWordsetSearches = array();
726 foreach($aWordsetSearches as $aCurrentSearch)
729 //var_dump($aCurrentSearch);
732 // If the token is valid
733 if (isset($aValidTokens[' '.$sToken]))
735 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
737 $aSearch = $aCurrentSearch;
738 $aSearch['iSearchRank']++;
739 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
741 if ($aSearch['sCountryCode'] === false)
743 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
744 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
745 // If reverse order is enabled, it may appear at the beginning as well.
746 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) &&
747 (!$this->bReverseInPlan || $iToken > 0 || $iPhrase > 0))
749 $aSearch['iSearchRank'] += 5;
751 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
754 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
756 if ($aSearch['fLat'] === '')
758 $aSearch['fLat'] = $aSearchTerm['lat'];
759 $aSearch['fLon'] = $aSearchTerm['lon'];
760 $aSearch['fRadius'] = $aSearchTerm['radius'];
761 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
764 elseif ($sPhraseType == 'postalcode')
766 // 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
767 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
769 // If we already have a name try putting the postcode first
770 if (sizeof($aSearch['aName']))
772 $aNewSearch = $aSearch;
773 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
774 $aNewSearch['aName'] = array();
775 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
776 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
779 if (sizeof($aSearch['aName']))
781 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
783 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
787 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
788 $aSearch['iSearchRank'] += 1000; // skip;
793 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
794 //$aSearch['iNamePhrase'] = $iPhrase;
796 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
800 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
802 if ($aSearch['sHouseNumber'] === '')
804 $aSearch['sHouseNumber'] = $sToken;
805 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
807 // Fall back to not searching for this item (better than nothing)
808 $aSearch = $aCurrentSearch;
809 $aSearch['iSearchRank'] += 1;
810 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
814 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
816 if ($aSearch['sClass'] === '')
818 $aSearch['sOperator'] = $aSearchTerm['operator'];
819 $aSearch['sClass'] = $aSearchTerm['class'];
820 $aSearch['sType'] = $aSearchTerm['type'];
821 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
822 else $aSearch['sOperator'] = 'near'; // near = in for the moment
823 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
825 // Do we have a shortcut id?
826 if ($aSearch['sOperator'] == 'name')
828 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
829 if ($iAmenityID = $this->oDB->getOne($sSQL))
831 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
832 $aSearch['aName'][$iAmenityID] = $iAmenityID;
833 $aSearch['sClass'] = '';
834 $aSearch['sType'] = '';
837 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
840 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
842 if (sizeof($aSearch['aName']))
844 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
846 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
850 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
851 $aSearch['iSearchRank'] += 1000; // skip;
856 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
857 //$aSearch['iNamePhrase'] = $iPhrase;
859 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
863 if (isset($aValidTokens[$sToken]))
865 // Allow searching for a word - but at extra cost
866 foreach($aValidTokens[$sToken] as $aSearchTerm)
868 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
870 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strlen($sToken) >= 4)
872 $aSearch = $aCurrentSearch;
873 $aSearch['iSearchRank'] += 1;
874 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
876 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
877 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
879 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
881 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
883 if (empty($aSearchTermToken['country_code'])
884 && empty($aSearchTermToken['lat'])
885 && empty($aSearchTermToken['class']))
887 $aSearch = $aCurrentSearch;
888 $aSearch['iSearchRank'] += 1;
889 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
890 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
896 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
897 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
901 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
903 $aSearch = $aCurrentSearch;
904 $aSearch['iSearchRank'] += 2;
905 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
906 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
907 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
909 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
910 $aSearch['iNamePhrase'] = $iPhrase;
911 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
918 // Allow skipping a word - but at EXTREAM cost
919 //$aSearch = $aCurrentSearch;
920 //$aSearch['iSearchRank']+=100;
921 //$aNewWordsetSearches[] = $aSearch;
925 usort($aNewWordsetSearches, 'bySearchRank');
926 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
928 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
930 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
931 usort($aNewPhraseSearches, 'bySearchRank');
933 $aSearchHash = array();
934 foreach($aNewPhraseSearches as $iSearch => $aSearch)
936 $sHash = serialize($aSearch);
937 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
938 else $aSearchHash[$sHash] = 1;
941 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
944 // Re-group the searches by their score, junk anything over 20 as just not worth trying
945 $aGroupedSearches = array();
946 foreach($aNewPhraseSearches as $aSearch)
948 if ($aSearch['iSearchRank'] < $this->iMaxRank)
950 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
951 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
954 ksort($aGroupedSearches);
957 $aSearches = array();
958 foreach($aGroupedSearches as $iScore => $aNewSearches)
960 $iSearchCount += sizeof($aNewSearches);
961 $aSearches = array_merge($aSearches, $aNewSearches);
962 if ($iSearchCount > 50) break;
965 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
972 // Re-group the searches by their score, junk anything over 20 as just not worth trying
973 $aGroupedSearches = array();
974 foreach($aSearches as $aSearch)
976 if ($aSearch['iSearchRank'] < $this->iMaxRank)
978 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
979 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
982 ksort($aGroupedSearches);
985 if (CONST_Debug) var_Dump($aGroupedSearches);
987 if ($this->bReverseInPlan)
989 $aCopyGroupedSearches = $aGroupedSearches;
990 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
992 foreach($aSearches as $iSearch => $aSearch)
994 if (sizeof($aSearch['aAddress']))
996 $iReverseItem = array_pop($aSearch['aAddress']);
997 if (isset($aPossibleMainWordIDs[$iReverseItem]))
999 $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
1000 $aSearch['aName'] = array($iReverseItem);
1001 $aGroupedSearches[$iGroup][] = $aSearch;
1003 //$aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
1004 //$aGroupedSearches[$iGroup][] = $aReverseSearch;
1010 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
1012 $aCopyGroupedSearches = $aGroupedSearches;
1013 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1015 foreach($aSearches as $iSearch => $aSearch)
1017 $aReductionsList = array($aSearch['aAddress']);
1018 $iSearchRank = $aSearch['iSearchRank'];
1019 while(sizeof($aReductionsList) > 0)
1022 if ($iSearchRank > iMaxRank) break 3;
1023 $aNewReductionsList = array();
1024 foreach($aReductionsList as $aReductionsWordList)
1026 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1028 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1029 $aReverseSearch = $aSearch;
1030 $aSearch['aAddress'] = $aReductionsWordListResult;
1031 $aSearch['iSearchRank'] = $iSearchRank;
1032 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1033 if (sizeof($aReductionsWordListResult) > 0)
1035 $aNewReductionsList[] = $aReductionsWordListResult;
1039 $aReductionsList = $aNewReductionsList;
1043 ksort($aGroupedSearches);
1046 // Filter out duplicate searches
1047 $aSearchHash = array();
1048 foreach($aGroupedSearches as $iGroup => $aSearches)
1050 foreach($aSearches as $iSearch => $aSearch)
1052 $sHash = serialize($aSearch);
1053 if (isset($aSearchHash[$sHash]))
1055 unset($aGroupedSearches[$iGroup][$iSearch]);
1056 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1060 $aSearchHash[$sHash] = 1;
1065 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1069 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1072 foreach($aSearches as $aSearch)
1076 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1077 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1079 // No location term?
1080 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1082 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1084 // Just looking for a country by code - look it up
1085 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1087 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1088 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1089 $sSQL .= " order by st_area(geometry) desc limit 1";
1090 if (CONST_Debug) var_dump($sSQL);
1091 $aPlaceIDs = $this->oDB->getCol($sSQL);
1096 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1097 if (!$aSearch['sClass']) continue;
1098 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1099 if ($this->oDB->getOne($sSQL))
1101 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1102 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1103 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1104 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1105 if (sizeof($this->aExcludePlaceIDs))
1107 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1109 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1110 $sSQL .= " limit $this->iLimit";
1111 if (CONST_Debug) var_dump($sSQL);
1112 $aPlaceIDs = $this->oDB->getCol($sSQL);
1114 // If excluded place IDs are given, it is fair to assume that
1115 // there have been results in the small box, so no further
1116 // expansion in that case.
1117 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs))
1119 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1120 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1121 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1122 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1123 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1124 $sSQL .= " limit $this->iLimit";
1125 if (CONST_Debug) var_dump($sSQL);
1126 $aPlaceIDs = $this->oDB->getCol($sSQL);
1131 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1132 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1133 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1134 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1135 $sSQL .= " limit $this->iLimit";
1136 if (CONST_Debug) var_dump($sSQL);
1137 $aPlaceIDs = $this->oDB->getCol($sSQL);
1143 $aPlaceIDs = array();
1145 // First we need a position, either aName or fLat or both
1149 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1150 // they might be right - but they are just too darned expensive to run
1151 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1152 //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1153 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1155 // For infrequent name terms disable index usage for address
1156 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1157 sizeof($aSearch['aName']) == 1 &&
1158 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1160 //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1161 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1165 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1166 //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1169 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1170 if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank between 16 and 27";
1171 if ($aSearch['fLon'] && $aSearch['fLat'])
1173 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1174 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1176 if (sizeof($this->aExcludePlaceIDs))
1178 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1180 if ($sCountryCodesSQL)
1182 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1185 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1186 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1188 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1189 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1190 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1191 $aOrder[] = "$sImportanceSQL DESC";
1192 if (sizeof($aSearch['aFullNameAddress']))
1194 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1195 $aOrder[] = 'exactmatch DESC';
1197 $sExactMatchSQL = '0::int as exactmatch';
1200 if (sizeof($aTerms))
1202 $sSQL = "select place_id, ";
1203 $sSQL .= $sExactMatchSQL;
1204 $sSQL .= " from search_name";
1205 $sSQL .= " where ".join(' and ',$aTerms);
1206 $sSQL .= " order by ".join(', ',$aOrder);
1207 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1208 $sSQL .= " limit 50";
1209 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1210 $sSQL .= " limit 1";
1212 $sSQL .= " limit ".$this->iLimit;
1214 if (CONST_Debug) { var_dump($sSQL); }
1215 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1216 if (PEAR::IsError($aViewBoxPlaceIDs))
1218 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1220 //var_dump($aViewBoxPlaceIDs);
1221 // Did we have an viewbox matches?
1222 $aPlaceIDs = array();
1223 $bViewBoxMatch = false;
1224 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1226 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1227 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1228 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1229 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1230 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1231 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1234 //var_Dump($aPlaceIDs);
1237 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1239 $aRoadPlaceIDs = $aPlaceIDs;
1240 $sPlaceIDs = join(',',$aPlaceIDs);
1242 // Now they are indexed look for a house attached to a street we found
1243 $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-,/ ]',$aSearch['sHouseNumber']).'\\\\M';
1244 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
1245 if (sizeof($this->aExcludePlaceIDs))
1247 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1249 $sSQL .= " limit $this->iLimit";
1250 if (CONST_Debug) var_dump($sSQL);
1251 $aPlaceIDs = $this->oDB->getCol($sSQL);
1253 // If not try the aux fallback table
1255 if (!sizeof($aPlaceIDs))
1257 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1258 if (sizeof($this->aExcludePlaceIDs))
1260 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1262 //$sSQL .= " limit $this->iLimit";
1263 if (CONST_Debug) var_dump($sSQL);
1264 $aPlaceIDs = $this->oDB->getCol($sSQL);
1268 if (!sizeof($aPlaceIDs))
1270 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1271 if (sizeof($this->aExcludePlaceIDs))
1273 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1275 //$sSQL .= " limit $this->iLimit";
1276 if (CONST_Debug) var_dump($sSQL);
1277 $aPlaceIDs = $this->oDB->getCol($sSQL);
1280 // Fallback to the road
1281 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1283 $aPlaceIDs = $aRoadPlaceIDs;
1288 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1290 $sPlaceIDs = join(',',$aPlaceIDs);
1291 $aClassPlaceIDs = array();
1293 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1295 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1296 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1297 $sSQL .= " and linked_place_id is null";
1298 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1299 $sSQL .= " order by rank_search asc limit $this->iLimit";
1300 if (CONST_Debug) var_dump($sSQL);
1301 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1304 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1306 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1307 $bCacheTable = $this->oDB->getOne($sSQL);
1309 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1311 if (CONST_Debug) var_dump($sSQL);
1312 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1314 // For state / country level searches the normal radius search doesn't work very well
1315 $sPlaceGeom = false;
1316 if ($this->iMaxRank < 9 && $bCacheTable)
1318 // Try and get a polygon to search in instead
1319 $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";
1320 if (CONST_Debug) var_dump($sSQL);
1321 $sPlaceGeom = $this->oDB->getOne($sSQL);
1330 $this->iMaxRank += 5;
1331 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1332 if (CONST_Debug) var_dump($sSQL);
1333 $aPlaceIDs = $this->oDB->getCol($sSQL);
1334 $sPlaceIDs = join(',',$aPlaceIDs);
1337 if ($sPlaceIDs || $sPlaceGeom)
1343 // More efficient - can make the range bigger
1347 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1348 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1349 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1351 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1352 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1355 $sSQL .= ",placex as f where ";
1356 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1361 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1363 if (sizeof($this->aExcludePlaceIDs))
1365 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1367 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1368 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1369 if ($iOffset) $sSQL .= " offset $iOffset";
1370 $sSQL .= " limit $this->iLimit";
1371 if (CONST_Debug) var_dump($sSQL);
1372 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1376 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1379 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1380 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1382 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1383 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1384 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1385 if (sizeof($this->aExcludePlaceIDs))
1387 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1389 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1390 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1391 if ($iOffset) $sSQL .= " offset $iOffset";
1392 $sSQL .= " limit $this->iLimit";
1393 if (CONST_Debug) var_dump($sSQL);
1394 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1399 $aPlaceIDs = $aClassPlaceIDs;
1405 if (PEAR::IsError($aPlaceIDs))
1407 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1410 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1412 foreach($aPlaceIDs as $iPlaceID)
1414 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1416 if ($iQueryLoop > 20) break;
1419 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1421 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1422 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1423 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1424 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1425 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1426 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1427 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1428 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1430 if (CONST_Debug) var_dump($sSQL);
1431 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1435 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1436 if ($iGroupLoop > 4) break;
1437 if ($iQueryLoop > 30) break;
1440 // Did we find anything?
1441 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1443 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1449 // Just interpret as a reverse geocode
1450 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1452 $aSearchResults = $this->getDetails(array($iPlaceID));
1454 $aSearchResults = array();
1458 if (!sizeof($aSearchResults))
1460 if ($this->bFallback)
1462 if ($this->fallbackStructuredQuery())
1464 return $this->lookup();
1471 $aClassType = getClassTypesWithImportance();
1472 $aRecheckWords = preg_split('/\b/u',$sQuery);
1473 foreach($aRecheckWords as $i => $sWord)
1475 if (!$sWord) unset($aRecheckWords[$i]);
1478 foreach($aSearchResults as $iResNum => $aResult)
1480 if (CONST_Search_AreaPolygons)
1482 // Get the bounding box and outline polygon
1483 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1484 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1485 $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1486 $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1487 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1488 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1489 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1490 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1491 $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1492 $aPointPolygon = $this->oDB->getRow($sSQL);
1493 if (PEAR::IsError($aPointPolygon))
1495 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1498 if ($aPointPolygon['place_id'])
1500 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1501 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1502 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1503 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1505 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1507 $aResult['lat'] = $aPointPolygon['centrelat'];
1508 $aResult['lon'] = $aPointPolygon['centrelon'];
1511 if ($this->bIncludePolygonAsPoints)
1513 // Translate geometary string to point array
1514 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1516 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1519 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1521 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1524 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1527 $iSteps = ($fRadius * 40000)^2;
1528 $fStepSize = (2*pi())/$iSteps;
1529 $aPolyPoints = array();
1530 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1532 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1534 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1535 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1536 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1537 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1541 // Output data suitable for display (points and a bounding box)
1542 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1544 $aResult['aPolyPoints'] = array();
1545 foreach($aPolyPoints as $aPoint)
1547 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1550 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1554 if ($aResult['extra_place'] == 'city')
1556 $aResult['class'] = 'place';
1557 $aResult['type'] = 'city';
1558 $aResult['rank_search'] = 16;
1561 if (!isset($aResult['aBoundingBox']))
1564 $fDiameter = 0.0001;
1566 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1567 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1569 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1571 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1572 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1574 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1576 $fRadius = $fDiameter / 2;
1578 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1579 $fStepSize = (2*pi())/$iSteps;
1580 $aPolyPoints = array();
1581 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1583 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1585 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1586 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1587 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1588 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1590 // Output data suitable for display (points and a bounding box)
1591 if ($this->bIncludePolygonAsPoints)
1593 $aResult['aPolyPoints'] = array();
1594 foreach($aPolyPoints as $aPoint)
1596 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1599 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1602 // Is there an icon set for this type of result?
1603 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1604 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1606 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1609 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1610 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1612 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1614 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1615 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1617 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1620 if ($this->bIncludeAddressDetails)
1622 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1623 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1625 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1629 // Adjust importance for the number of exact string matches in the result
1630 $aResult['importance'] = max(0.001,$aResult['importance']);
1632 $sAddress = $aResult['langaddress'];
1633 foreach($aRecheckWords as $i => $sWord)
1635 if (stripos($sAddress, $sWord)!==false) $iCountWords++;
1638 $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
1640 $aResult['name'] = $aResult['langaddress'];
1641 // secondary ordering (for results with same importance (the smaller the better):
1642 // - approximate importance of address parts
1643 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1644 // - number of exact matches from the query
1645 if (isset($this->exactMatchCache[$aResult['place_id']]))
1646 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1647 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1648 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1649 // - importance of the class/type
1650 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1651 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1653 $aResult['foundorder'] = $aResult['foundorder'] + 0.000001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1657 $aResult['foundorder'] = $aResult['foundorder'] + 0.001;
1659 $aSearchResults[$iResNum] = $aResult;
1661 uasort($aSearchResults, 'byImportance');
1663 $aOSMIDDone = array();
1664 $aClassTypeNameDone = array();
1665 $aToFilter = $aSearchResults;
1666 $aSearchResults = array();
1669 foreach($aToFilter as $iResNum => $aResult)
1671 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1672 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1675 $fLat = $aResult['lat'];
1676 $fLon = $aResult['lon'];
1677 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1680 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1681 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1683 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1684 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1685 $aSearchResults[] = $aResult;
1688 // Absolute limit on number of results
1689 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1692 return $aSearchResults;
1701 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
1703 $aPoints = explode(',',$_GET['route']);
1704 if (sizeof($aPoints) % 2 != 0)
1706 userError("Uneven number of points");
1709 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
1710 $fPrevCoord = false;