6 protected $aLangPrefOrder = array();
8 protected $bIncludeAddressDetails = false;
9 protected $bIncludeExtraTags = false;
10 protected $bIncludeNameDetails = false;
12 protected $bIncludePolygonAsPoints = false;
13 protected $bIncludePolygonAsText = false;
14 protected $bIncludePolygonAsGeoJSON = false;
15 protected $bIncludePolygonAsKML = false;
16 protected $bIncludePolygonAsSVG = false;
17 protected $fPolygonSimplificationThreshold = 0.0;
19 protected $aExcludePlaceIDs = array();
20 protected $bDeDupe = true;
21 protected $bReverseInPlan = false;
23 protected $iLimit = 20;
24 protected $iFinalLimit = 10;
25 protected $iOffset = 0;
26 protected $bFallback = false;
28 protected $aCountryCodes = false;
29 protected $aNearPoint = false;
31 protected $bBoundedSearch = false;
32 protected $aViewBox = false;
33 protected $sViewboxSmallSQL = false;
34 protected $sViewboxLargeSQL = false;
35 protected $aRoutePoints = false;
37 protected $iMaxRank = 20;
38 protected $iMinAddressRank = 0;
39 protected $iMaxAddressRank = 30;
40 protected $aAddressRankList = array();
41 protected $exactMatchCache = array();
43 protected $sAllowedTypesSQLList = false;
45 protected $sQuery = false;
46 protected $aStructuredQuery = false;
48 function Geocode(&$oDB)
53 function setReverseInPlan($bReverse)
55 $this->bReverseInPlan = $bReverse;
58 function setLanguagePreference($aLangPref)
60 $this->aLangPrefOrder = $aLangPref;
63 function setIncludeAddressDetails($bAddressDetails = true)
65 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
68 function getIncludeAddressDetails()
70 return $this->bIncludeAddressDetails;
73 function getIncludeExtraTags()
75 return $this->bIncludeExtraTags;
78 function getIncludeNameDetails()
80 return $this->bIncludeNameDetails;
83 function setIncludePolygonAsPoints($b = true)
85 $this->bIncludePolygonAsPoints = $b;
88 function getIncludePolygonAsPoints()
90 return $this->bIncludePolygonAsPoints;
93 function setIncludePolygonAsText($b = true)
95 $this->bIncludePolygonAsText = $b;
98 function getIncludePolygonAsText()
100 return $this->bIncludePolygonAsText;
103 function setIncludePolygonAsGeoJSON($b = true)
105 $this->bIncludePolygonAsGeoJSON = $b;
108 function setIncludePolygonAsKML($b = true)
110 $this->bIncludePolygonAsKML = $b;
113 function setIncludePolygonAsSVG($b = true)
115 $this->bIncludePolygonAsSVG = $b;
118 function setPolygonSimplificationThreshold($f)
120 $this->fPolygonSimplificationThreshold = $f;
123 function setDeDupe($bDeDupe = true)
125 $this->bDeDupe = (bool)$bDeDupe;
128 function setLimit($iLimit = 10)
130 if ($iLimit > 50) $iLimit = 50;
131 if ($iLimit < 1) $iLimit = 1;
133 $this->iFinalLimit = $iLimit;
134 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
137 function setOffset($iOffset = 0)
139 $this->iOffset = $iOffset;
142 function setFallback($bFallback = true)
144 $this->bFallback = (bool)$bFallback;
147 function setExcludedPlaceIDs($a)
149 // TODO: force to int
150 $this->aExcludePlaceIDs = $a;
153 function getExcludedPlaceIDs()
155 return $this->aExcludePlaceIDs;
158 function setBounded($bBoundedSearch = true)
160 $this->bBoundedSearch = (bool)$bBoundedSearch;
163 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
165 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
168 function getViewBoxString()
170 if (!$this->aViewBox) return null;
171 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
174 function setRoute($aRoutePoints)
176 $this->aRoutePoints = $aRoutePoints;
179 function setFeatureType($sFeatureType)
181 switch($sFeatureType)
184 $this->setRankRange(4, 4);
187 $this->setRankRange(8, 8);
190 $this->setRankRange(14, 16);
193 $this->setRankRange(8, 20);
198 function setRankRange($iMin, $iMax)
200 $this->iMinAddressRank = (int)$iMin;
201 $this->iMaxAddressRank = (int)$iMax;
204 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
206 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
209 function setCountryCodesList($aCountryCodes)
211 $this->aCountryCodes = $aCountryCodes;
214 function setQuery($sQueryString)
216 $this->sQuery = $sQueryString;
217 $this->aStructuredQuery = false;
220 function getQueryString()
222 return $this->sQuery;
226 function loadParamArray($aParams)
228 if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
229 if ((float) CONST_Postgresql_Version > 9.2)
231 if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags'];
232 if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails'];
234 if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
235 if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
237 if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
238 if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
240 if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
242 // List of excluded Place IDs - used for more acurate pageing
243 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
245 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
247 $iExcludedPlaceID = (int)$iExcludedPlaceID;
248 if ($iExcludedPlaceID)
249 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
252 if (isset($aExcludePlaceIDs))
253 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
256 // Only certain ranks of feature
257 if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
258 if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
261 if (isset($aParams['countrycodes']))
263 $aCountryCodes = array();
264 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
266 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
268 $aCountryCodes[] = strtolower($sCountryCode);
271 $this->aCountryCodes = $aCountryCodes;
274 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
276 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
277 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
279 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
281 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
282 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
285 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
287 $aPoints = explode(',',$aParams['route']);
288 if (sizeof($aPoints) % 2 != 0)
290 userError("Uneven number of points");
295 foreach($aPoints as $i => $fPoint)
299 $aRoute[] = array((float)$fPoint, $fPrevCoord);
303 $fPrevCoord = (float)$fPoint;
306 $this->aRoutePoints = $aRoute;
310 function setQueryFromParams($aParams)
313 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
316 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
317 $this->setReverseInPlan(false);
321 $this->setQuery($sQuery);
325 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
327 $sValue = trim($sValue);
328 if (!$sValue) return false;
329 $this->aStructuredQuery[$sKey] = $sValue;
330 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
332 $this->iMinAddressRank = $iNewMinAddressRank;
333 $this->iMaxAddressRank = $iNewMaxAddressRank;
335 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
339 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
341 $this->sQuery = false;
344 $this->iMinAddressRank = 0;
345 $this->iMaxAddressRank = 30;
346 $this->aAddressRankList = array();
348 $this->aStructuredQuery = array();
349 $this->sAllowedTypesSQLList = '';
351 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
352 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
353 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
354 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
355 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
356 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
357 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
359 if (sizeof($this->aStructuredQuery) > 0)
361 $this->sQuery = join(', ', $this->aStructuredQuery);
362 if ($this->iMaxAddressRank < 30)
364 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
369 function fallbackStructuredQuery()
371 if (!$this->aStructuredQuery) return false;
373 $aParams = $this->aStructuredQuery;
375 if (sizeof($aParams) == 1) return false;
377 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
379 foreach($aOrderToFallback as $sType)
381 if (isset($aParams[$sType]))
383 unset($aParams[$sType]);
384 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
392 function getDetails($aPlaceIDs)
394 //$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
395 if (sizeof($aPlaceIDs) == 0) return array();
397 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
399 // Get the details for display (is this a redundant extra step?)
400 $sPlaceIDs = join(',',array_keys($aPlaceIDs));
402 $sImportanceSQL = '';
403 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
404 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
406 $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,";
407 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
408 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
409 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
410 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
411 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
412 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
413 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
414 $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, ";
415 $sSQL .= "(extratags->'place') as extra_place ";
416 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
417 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
418 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
419 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
421 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
422 $sSQL .= "and linked_place_id is null ";
423 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
424 if (!$this->bDeDupe) $sSQL .= ",place_id";
425 $sSQL .= ",langaddress ";
426 $sSQL .= ",placename ";
428 if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
429 if ($this->bIncludeNameDetails) $sSQL .= ",name";
430 $sSQL .= ",extratags->'place' ";
432 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
434 //query also location_property_tiger and location_property_aux
435 //Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
436 //only Tiger housenumbers need to be interpolated, because they are saved as lines with start- and endnumber, the common osm housenumbers are usually saved as points
439 $length=count($aPlaceIDs);
440 foreach($aPlaceIDs as $placeID => $housenumber){
442 $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
444 $sHousenumbers .= ", ";
448 $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";
449 $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
450 $sSQL .= ", null as placename";
451 $sSQL .= ", null as ref";
452 if ($this->bIncludeExtraTags) $sSQL .= ", null as extra";
453 if ($this->bIncludeNameDetails) $sSQL .= ", null as names";
454 $sSQL .= ", avg(st_x(point)) as lon, avg(st_y(point)) as lat";
455 $sSQL .= $sImportanceSQL.", -1.15 as importance ";
456 $sSQL .= ", (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(blub.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance ";
457 $sSQL .= ", null as extra_place ";
458 $sSQL .= " from (select place_id";
459 //interpolate the Tiger housenumbers here
460 $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as point, parent_place_id, housenumber_for_place ";
461 $sSQL .= "from (location_property_tiger ";
462 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
463 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
464 $sSQL .= " group by place_id, housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
465 if (!$this->bDeDupe) $sSQL .= ", place_id ";
468 $sSQL .= "select 'L' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 0 as rank_search, 0 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, 'us' as country_code, ";
469 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress, ";
470 $sSQL .= "null as placename, ";
471 $sSQL .= "null as ref, ";
472 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
473 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
474 $sSQL .= "avg(ST_X(centroid)) as lon, avg(ST_Y(centroid)) as lat, ";
475 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
476 $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, ";
477 $sSQL .= "null as extra_place ";
478 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
479 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
480 $sSQL .= "group by place_id";
481 if (!$this->bDeDupe) $sSQL .= ", place_id";
482 $sSQL .= ", get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) ";
485 $sSQL .= " order by importance desc";
486 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
487 $aSearchResults = $this->oDB->getAll($sSQL);
489 if (PEAR::IsError($aSearchResults))
491 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
494 return $aSearchResults;
497 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
500 Calculate all searches using aValidTokens i.e.
501 'Wodsworth Road, Sheffield' =>
505 0 1 (wodsworth)(road)
508 Score how good the search is so they can be ordered
510 foreach($aPhrases as $iPhrase => $sPhrase)
512 $aNewPhraseSearches = array();
513 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
514 else $sPhraseType = '';
516 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
518 // Too many permutations - too expensive
519 if ($iWordSet > 120) break;
521 $aWordsetSearches = $aSearches;
523 // Add all words from this wordset
524 foreach($aWordset as $iToken => $sToken)
526 //echo "<br><b>$sToken</b>";
527 $aNewWordsetSearches = array();
529 foreach($aWordsetSearches as $aCurrentSearch)
532 //var_dump($aCurrentSearch);
535 // If the token is valid
536 if (isset($aValidTokens[' '.$sToken]))
538 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
540 $aSearch = $aCurrentSearch;
541 $aSearch['iSearchRank']++;
542 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
544 if ($aSearch['sCountryCode'] === false)
546 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
547 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
548 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
550 $aSearch['iSearchRank'] += 5;
552 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
555 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
557 if ($aSearch['fLat'] === '')
559 $aSearch['fLat'] = $aSearchTerm['lat'];
560 $aSearch['fLon'] = $aSearchTerm['lon'];
561 $aSearch['fRadius'] = $aSearchTerm['radius'];
562 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
565 elseif ($sPhraseType == 'postalcode')
567 // 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
568 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
570 // If we already have a name try putting the postcode first
571 if (sizeof($aSearch['aName']))
573 $aNewSearch = $aSearch;
574 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
575 $aNewSearch['aName'] = array();
576 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
577 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
580 if (sizeof($aSearch['aName']))
582 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
584 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
588 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
589 $aSearch['iSearchRank'] += 1000; // skip;
594 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
595 //$aSearch['iNamePhrase'] = $iPhrase;
597 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
601 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
603 if ($aSearch['sHouseNumber'] === '')
605 $aSearch['sHouseNumber'] = $sToken;
606 // sanity check: if the housenumber is not mainly made
607 // up of numbers, add a penalty
608 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
609 // also housenumbers should appear in the first or second phrase
610 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
611 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
613 // Fall back to not searching for this item (better than nothing)
614 $aSearch = $aCurrentSearch;
615 $aSearch['iSearchRank'] += 1;
616 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
620 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
622 if ($aSearch['sClass'] === '')
624 $aSearch['sOperator'] = $aSearchTerm['operator'];
625 $aSearch['sClass'] = $aSearchTerm['class'];
626 $aSearch['sType'] = $aSearchTerm['type'];
627 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
628 else $aSearch['sOperator'] = 'near'; // near = in for the moment
629 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
631 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
634 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
636 if (sizeof($aSearch['aName']))
638 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
640 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
644 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
645 $aSearch['iSearchRank'] += 1000; // skip;
650 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
651 //$aSearch['iNamePhrase'] = $iPhrase;
653 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
657 // Look for partial matches.
658 // Note that there is no point in adding country terms here
659 // because country are omitted in the address.
660 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
662 // Allow searching for a word - but at extra cost
663 foreach($aValidTokens[$sToken] as $aSearchTerm)
665 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
667 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
669 $aSearch = $aCurrentSearch;
670 $aSearch['iSearchRank'] += 1;
671 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
673 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
674 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
676 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
678 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
679 $aSearch['iSearchRank'] += 1;
680 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
681 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
683 if (empty($aSearchTermToken['country_code'])
684 && empty($aSearchTermToken['lat'])
685 && empty($aSearchTermToken['class']))
687 $aSearch = $aCurrentSearch;
688 $aSearch['iSearchRank'] += 1;
689 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
690 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
696 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
697 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
698 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
702 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
704 $aSearch = $aCurrentSearch;
705 $aSearch['iSearchRank'] += 1;
706 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
707 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
708 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
709 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
711 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
712 $aSearch['iNamePhrase'] = $iPhrase;
713 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
720 // Allow skipping a word - but at EXTREAM cost
721 //$aSearch = $aCurrentSearch;
722 //$aSearch['iSearchRank']+=100;
723 //$aNewWordsetSearches[] = $aSearch;
727 usort($aNewWordsetSearches, 'bySearchRank');
728 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
730 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
732 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
733 usort($aNewPhraseSearches, 'bySearchRank');
735 $aSearchHash = array();
736 foreach($aNewPhraseSearches as $iSearch => $aSearch)
738 $sHash = serialize($aSearch);
739 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
740 else $aSearchHash[$sHash] = 1;
743 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
746 // Re-group the searches by their score, junk anything over 20 as just not worth trying
747 $aGroupedSearches = array();
748 foreach($aNewPhraseSearches as $aSearch)
750 if ($aSearch['iSearchRank'] < $this->iMaxRank)
752 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
753 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
756 ksort($aGroupedSearches);
759 $aSearches = array();
760 foreach($aGroupedSearches as $iScore => $aNewSearches)
762 $iSearchCount += sizeof($aNewSearches);
763 $aSearches = array_merge($aSearches, $aNewSearches);
764 if ($iSearchCount > 50) break;
767 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
770 return $aGroupedSearches;
774 /* Perform the actual query lookup.
776 Returns an ordered list of results, each with the following fields:
777 osm_type: type of corresponding OSM object
781 P - postcode (internally computed)
782 osm_id: id of corresponding OSM object
783 class: general object class (corresponds to tag key of primary OSM tag)
784 type: subclass of object (corresponds to tag value of primary OSM tag)
785 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
786 rank_search: rank in search hierarchy
787 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
788 rank_address: rank in address hierarchy (determines orer in address)
789 place_id: internal key (may differ between different instances)
790 country_code: ISO country code
791 langaddress: localized full address
792 placename: localized name of object
793 ref: content of ref tag (if available)
796 importance: importance of place based on Wikipedia link count
797 addressimportance: cumulated importance of address elements
798 extra_place: type of place (for admin boundaries, if there is a place tag)
799 aBoundingBox: bounding Box
800 label: short description of the object class/type (English only)
801 name: full name (currently the same as langaddress)
802 foundorder: secondary ordering for places with same importance
806 if (!$this->sQuery && !$this->aStructuredQuery) return false;
808 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
809 $sCountryCodesSQL = false;
810 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
812 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
815 $sQuery = $this->sQuery;
817 // Conflicts between US state abreviations and various words for 'the' in different languages
818 if (isset($this->aLangPrefOrder['name:en']))
820 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
821 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
822 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
826 $sViewboxCentreSQL = false;
827 $bBoundingBoxSearch = false;
830 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
831 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
832 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
833 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
834 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
835 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
837 $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)";
838 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
839 $bBoundingBoxSearch = $this->bBoundedSearch;
843 if ($this->aRoutePoints)
845 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
847 foreach($this->aRoutePoints as $aPoint)
849 if (!$bFirst) $sViewboxCentreSQL .= ",";
850 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
853 $sViewboxCentreSQL .= ")'::geometry,4326)";
855 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
856 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
857 if (PEAR::isError($this->sViewboxSmallSQL))
859 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
861 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
863 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
864 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
865 if (PEAR::isError($this->sViewboxLargeSQL))
867 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
869 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
870 $bBoundingBoxSearch = $this->bBoundedSearch;
873 // Do we have anything that looks like a lat/lon pair?
874 if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
875 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
876 $sQuery = $aLooksLike['query'];
879 $aSearchResults = array();
880 if ($sQuery || $this->aStructuredQuery)
882 // Start with a blank search
884 array('iSearchRank' => 0,
886 'sCountryCode' => false,
888 'aAddress' => array(),
889 'aFullNameAddress' => array(),
890 'aNameNonSearch' => array(),
891 'aAddressNonSearch' => array(),
893 'aFeatureName' => array(),
896 'sHouseNumber' => '',
903 // Do we have a radius search?
904 $sNearPointSQL = false;
905 if ($this->aNearPoint)
907 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
908 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
909 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
910 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
913 // Any 'special' terms in the search?
914 $bSpecialTerms = false;
915 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
916 $aSpecialTerms = array();
917 foreach($aSpecialTermsRaw as $aSpecialTerm)
919 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
920 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
923 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
924 $aSpecialTerms = array();
925 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity'])
927 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
928 unset($this->aStructuredQuery['amenity']);
930 foreach($aSpecialTermsRaw as $aSpecialTerm)
932 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
933 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
934 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
935 $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';
936 if (CONST_Debug) var_Dump($sSQL);
937 $aSearchWords = $this->oDB->getAll($sSQL);
938 $aNewSearches = array();
939 foreach($aSearches as $aSearch)
941 foreach($aSearchWords as $aSearchTerm)
943 $aNewSearch = $aSearch;
944 if ($aSearchTerm['country_code'])
946 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
947 $aNewSearches[] = $aNewSearch;
948 $bSpecialTerms = true;
950 if ($aSearchTerm['class'])
952 $aNewSearch['sClass'] = $aSearchTerm['class'];
953 $aNewSearch['sType'] = $aSearchTerm['type'];
954 $aNewSearches[] = $aNewSearch;
955 $bSpecialTerms = true;
959 $aSearches = $aNewSearches;
962 // Split query into phrases
963 // Commas are used to reduce the search space by indicating where phrases split
964 if ($this->aStructuredQuery)
966 $aPhrases = $this->aStructuredQuery;
967 $bStructuredPhrases = true;
971 $aPhrases = explode(',',$sQuery);
972 $bStructuredPhrases = false;
975 // Convert each phrase to standard form
976 // Create a list of standard words
977 // Get all 'sets' of words
978 // Generate a complete list of all
980 foreach($aPhrases as $iPhrase => $sPhrase)
982 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
983 if (PEAR::isError($aPhrase))
985 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
986 if (CONST_Debug) var_dump($aPhrase);
989 if (trim($aPhrase['string']))
991 $aPhrases[$iPhrase] = $aPhrase;
992 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
993 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
994 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
998 unset($aPhrases[$iPhrase]);
1002 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
1003 $aPhraseTypes = array_keys($aPhrases);
1004 $aPhrases = array_values($aPhrases);
1006 if (sizeof($aTokens))
1008 // Check which tokens we have, get the ID numbers
1009 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
1010 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
1012 if (CONST_Debug) var_Dump($sSQL);
1014 $aValidTokens = array();
1015 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
1016 else $aDatabaseWords = array();
1017 if (PEAR::IsError($aDatabaseWords))
1019 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
1021 $aPossibleMainWordIDs = array();
1022 $aWordFrequencyScores = array();
1023 foreach($aDatabaseWords as $aToken)
1025 // Very special case - require 2 letter country param to match the country code found
1026 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
1027 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code'])
1032 if (isset($aValidTokens[$aToken['word_token']]))
1034 $aValidTokens[$aToken['word_token']][] = $aToken;
1038 $aValidTokens[$aToken['word_token']] = array($aToken);
1040 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1041 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1043 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1045 // Try and calculate GB postcodes we might be missing
1046 foreach($aTokens as $sToken)
1048 // Source of gb postcodes is now definitive - always use
1049 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
1051 if (substr($aData[1],-2,1) != ' ')
1053 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1054 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1056 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1057 if ($aGBPostcodeLocation)
1059 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1062 // US ZIP+4 codes - if there is no token,
1063 // merge in the 5-digit ZIP code
1064 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1066 if (isset($aValidTokens[$aData[1]]))
1068 foreach($aValidTokens[$aData[1]] as $aToken)
1070 if (!$aToken['class'])
1072 if (isset($aValidTokens[$sToken]))
1074 $aValidTokens[$sToken][] = $aToken;
1078 $aValidTokens[$sToken] = array($aToken);
1086 foreach($aTokens as $sToken)
1088 // Unknown single word token with a number - assume it is a house number
1089 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1091 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1095 // Any words that have failed completely?
1096 // TODO: suggestions
1098 // Start the search process
1099 // array with: placeid => -1 | tiger-housenumber
1100 $aResultPlaceIDs = array();
1102 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1104 if ($this->bReverseInPlan)
1106 // Reverse phrase array and also reverse the order of the wordsets in
1107 // the first and final phrase. Don't bother about phrases in the middle
1108 // because order in the address doesn't matter.
1109 $aPhrases = array_reverse($aPhrases);
1110 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1111 if (sizeof($aPhrases) > 1)
1113 $aFinalPhrase = end($aPhrases);
1114 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1116 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1118 foreach($aGroupedSearches as $aSearches)
1120 foreach($aSearches as $aSearch)
1122 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1124 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1125 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1131 $aGroupedSearches = $aReverseGroupedSearches;
1132 ksort($aGroupedSearches);
1137 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1138 $aGroupedSearches = array();
1139 foreach($aSearches as $aSearch)
1141 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1143 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1144 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1147 ksort($aGroupedSearches);
1150 if (CONST_Debug) var_Dump($aGroupedSearches);
1152 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0)
1154 $aCopyGroupedSearches = $aGroupedSearches;
1155 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1157 foreach($aSearches as $iSearch => $aSearch)
1159 $aReductionsList = array($aSearch['aAddress']);
1160 $iSearchRank = $aSearch['iSearchRank'];
1161 while(sizeof($aReductionsList) > 0)
1164 if ($iSearchRank > iMaxRank) break 3;
1165 $aNewReductionsList = array();
1166 foreach($aReductionsList as $aReductionsWordList)
1168 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1170 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1171 $aReverseSearch = $aSearch;
1172 $aSearch['aAddress'] = $aReductionsWordListResult;
1173 $aSearch['iSearchRank'] = $iSearchRank;
1174 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1175 if (sizeof($aReductionsWordListResult) > 0)
1177 $aNewReductionsList[] = $aReductionsWordListResult;
1181 $aReductionsList = $aNewReductionsList;
1185 ksort($aGroupedSearches);
1188 // Filter out duplicate searches
1189 $aSearchHash = array();
1190 foreach($aGroupedSearches as $iGroup => $aSearches)
1192 foreach($aSearches as $iSearch => $aSearch)
1194 $sHash = serialize($aSearch);
1195 if (isset($aSearchHash[$sHash]))
1197 unset($aGroupedSearches[$iGroup][$iSearch]);
1198 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1202 $aSearchHash[$sHash] = 1;
1207 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1211 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1214 foreach($aSearches as $aSearch)
1217 $searchedHousenumber=-1;
1219 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1220 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1222 // No location term?
1223 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1225 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1227 // Just looking for a country by code - look it up
1228 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1230 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1231 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1232 if ($bBoundingBoxSearch)
1233 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1234 $sSQL .= " order by st_area(geometry) desc limit 1";
1235 if (CONST_Debug) var_dump($sSQL);
1236 $aPlaceIDs = $this->oDB->getCol($sSQL);
1240 $aPlaceIDs = array();
1245 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1246 if (!$aSearch['sClass']) continue;
1247 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1248 if ($this->oDB->getOne($sSQL))
1250 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1251 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1252 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1253 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1254 if (sizeof($this->aExcludePlaceIDs))
1256 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1258 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1259 $sSQL .= " limit $this->iLimit";
1260 if (CONST_Debug) var_dump($sSQL);
1261 $aPlaceIDs = $this->oDB->getCol($sSQL);
1263 // If excluded place IDs are given, it is fair to assume that
1264 // there have been results in the small box, so no further
1265 // expansion in that case.
1266 // Also don't expand if bounded results were requested.
1267 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1269 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1270 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1271 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1272 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1273 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1274 $sSQL .= " limit $this->iLimit";
1275 if (CONST_Debug) var_dump($sSQL);
1276 $aPlaceIDs = $this->oDB->getCol($sSQL);
1281 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1282 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1283 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1284 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1285 $sSQL .= " limit $this->iLimit";
1286 if (CONST_Debug) var_dump($sSQL);
1287 $aPlaceIDs = $this->oDB->getCol($sSQL);
1293 $aPlaceIDs = array();
1295 // First we need a position, either aName or fLat or both
1299 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1301 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1302 $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
1305 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1306 // they might be right - but they are just too darned expensive to run
1307 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1308 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1309 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1311 // For infrequent name terms disable index usage for address
1312 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1313 sizeof($aSearch['aName']) == 1 &&
1314 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1316 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1320 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1321 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1324 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1325 if ($aSearch['sHouseNumber'])
1327 $aTerms[] = "address_rank between 16 and 27";
1331 if ($this->iMinAddressRank > 0)
1333 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1335 if ($this->iMaxAddressRank < 30)
1337 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1340 if ($aSearch['fLon'] && $aSearch['fLat'])
1342 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1343 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1345 if (sizeof($this->aExcludePlaceIDs))
1347 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1349 if ($sCountryCodesSQL)
1351 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1354 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1355 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1357 if ($aSearch['sHouseNumber'])
1359 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1363 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1365 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1366 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1368 $aOrder[] = "$sImportanceSQL DESC";
1369 if (sizeof($aSearch['aFullNameAddress']))
1371 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1372 $aOrder[] = 'exactmatch DESC';
1374 $sExactMatchSQL = '0::int as exactmatch';
1377 if (sizeof($aTerms))
1379 $sSQL = "select place_id, ";
1380 $sSQL .= $sExactMatchSQL;
1381 $sSQL .= " from search_name";
1382 $sSQL .= " where ".join(' and ',$aTerms);
1383 $sSQL .= " order by ".join(', ',$aOrder);
1384 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1385 $sSQL .= " limit 20";
1386 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1387 $sSQL .= " limit 1";
1389 $sSQL .= " limit ".$this->iLimit;
1391 if (CONST_Debug) { var_dump($sSQL); }
1392 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1393 if (PEAR::IsError($aViewBoxPlaceIDs))
1395 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1397 //var_dump($aViewBoxPlaceIDs);
1398 // Did we have an viewbox matches?
1399 $aPlaceIDs = array();
1400 $bViewBoxMatch = false;
1401 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1403 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1404 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1405 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1406 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1407 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1408 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1411 //var_Dump($aPlaceIDs);
1414 //now search for housenumber, if housenumber provided
1415 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1417 $aRoadPlaceIDs = $aPlaceIDs;
1418 $sPlaceIDs = join(',',$aPlaceIDs);
1420 // Now they are indexed look for a house attached to a street we found
1421 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1422 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1423 if (sizeof($this->aExcludePlaceIDs))
1425 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1427 $sSQL .= " limit $this->iLimit";
1428 if (CONST_Debug) var_dump($sSQL);
1429 $aPlaceIDs = $this->oDB->getCol($sSQL);
1431 // If nothing found try the aux fallback table
1432 if (!sizeof($aPlaceIDs))
1434 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1435 if (sizeof($this->aExcludePlaceIDs))
1437 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1439 //$sSQL .= " limit $this->iLimit";
1440 if (CONST_Debug) var_dump($sSQL);
1441 $aPlaceIDs = $this->oDB->getCol($sSQL);
1443 //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1444 $searchedHousenumber = intval($aSearch['sHouseNumber']);
1445 if (!sizeof($aPlaceIDs))
1447 //new query for lines, not housenumbers anymore
1448 if($searchedHousenumber%2==0){
1449 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1450 $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
1452 //look for housenumber in streets with interpolationtype odd or all
1453 $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
1456 if (sizeof($this->aExcludePlaceIDs))
1458 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1460 //$sSQL .= " limit $this->iLimit";
1461 if (CONST_Debug) var_dump($sSQL);
1463 $aPlaceIDs = $this->oDB->getCol($sSQL, 0);
1466 // Fallback to the road (if no housenumber was found)
1467 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1469 $aPlaceIDs = $aRoadPlaceIDs;
1470 //set to -1, if no housenumbers were found
1471 $searchedHousenumber=-1;
1473 //housenumber was found, remains saved in searchedHousenumber
1478 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1480 $sPlaceIDs = join(',', aPlaceIDs);
1481 $aClassPlaceIDs = array();
1483 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1485 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1486 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1487 $sSQL .= " and linked_place_id is null";
1488 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1489 $sSQL .= " order by rank_search asc limit $this->iLimit";
1490 if (CONST_Debug) var_dump($sSQL);
1491 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1494 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1496 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1497 $bCacheTable = $this->oDB->getOne($sSQL);
1499 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1501 if (CONST_Debug) var_dump($sSQL);
1502 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1504 // For state / country level searches the normal radius search doesn't work very well
1505 $sPlaceGeom = false;
1506 if ($this->iMaxRank < 9 && $bCacheTable)
1508 // Try and get a polygon to search in instead
1509 $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";
1510 if (CONST_Debug) var_dump($sSQL);
1511 $sPlaceGeom = $this->oDB->getOne($sSQL);
1520 $this->iMaxRank += 5;
1521 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1522 if (CONST_Debug) var_dump($sSQL);
1523 $aPlaceIDs = $this->oDB->getCol($sSQL);
1524 $sPlaceIDs = join(',',$aPlaceIDs);
1527 if ($sPlaceIDs || $sPlaceGeom)
1533 // More efficient - can make the range bigger
1537 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1538 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1539 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1541 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1542 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1545 $sSQL .= ",placex as f where ";
1546 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1551 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1553 if (sizeof($this->aExcludePlaceIDs))
1555 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1557 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1558 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1559 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1560 $sSQL .= " limit $this->iLimit";
1561 if (CONST_Debug) var_dump($sSQL);
1562 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1566 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1569 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1570 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1572 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1573 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1574 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1575 if (sizeof($this->aExcludePlaceIDs))
1577 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1579 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1580 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1581 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1582 $sSQL .= " limit $this->iLimit";
1583 if (CONST_Debug) var_dump($sSQL);
1584 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1589 $aPlaceIDs = $aClassPlaceIDs;
1595 if (PEAR::IsError($aPlaceIDs))
1597 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1600 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1602 foreach($aPlaceIDs as $iPlaceID)
1604 // array for placeID => -1 | Tiger housenumber
1605 $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1607 if ($iQueryLoop > 20) break;
1610 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1612 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1613 // reduces the number of place ids, like a filter
1614 $sSQL = "select place_id from placex where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1615 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1616 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1617 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1618 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1619 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1620 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1622 if (CONST_Debug) var_dump($sSQL);
1623 $aFilteredPlaceIDs = $this->oDB->getCol($sSQL);
1625 foreach($aFilteredPlaceIDs as $placeID){
1626 $tempIDs[$placeID]= $aResultPlaceIDs[$placeID]; //assign housenumber to placeID
1628 $aResultPlaceIDs=$tempIDs;
1632 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1633 if ($iGroupLoop > 4) break;
1634 if ($iQueryLoop > 30) break;
1637 // Did we find anything?
1638 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1640 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1646 // Just interpret as a reverse geocode
1647 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1649 $aSearchResults = $this->getDetails(array($iPlaceID));
1651 $aSearchResults = array();
1655 if (!sizeof($aSearchResults))
1657 if ($this->bFallback)
1659 if ($this->fallbackStructuredQuery())
1661 return $this->lookup();
1668 $aClassType = getClassTypesWithImportance();
1669 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1670 foreach($aRecheckWords as $i => $sWord)
1672 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1675 if (CONST_Debug) { echo '<i>Recheck words:<\i>'; var_dump($aRecheckWords); }
1677 foreach($aSearchResults as $iResNum => $aResult)
1680 $fDiameter = 0.0001;
1682 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1683 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1685 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'];
1687 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1688 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1690 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1692 $fRadius = $fDiameter / 2;
1694 if (CONST_Search_AreaPolygons)
1696 // Get the bounding box and outline polygon
1697 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1698 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1699 $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
1700 $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
1701 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1702 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1703 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1704 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1705 $sFrom = " from placex where place_id = ".$aResult['place_id'];
1706 if ($this->fPolygonSimplificationThreshold > 0)
1708 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
1715 $aPointPolygon = $this->oDB->getRow($sSQL);
1716 if (PEAR::IsError($aPointPolygon))
1718 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1721 if ($aPointPolygon['place_id'])
1723 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1724 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1725 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1726 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1728 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1730 $aResult['lat'] = $aPointPolygon['centrelat'];
1731 $aResult['lon'] = $aPointPolygon['centrelon'];
1734 if ($this->bIncludePolygonAsPoints)
1736 $aPolyPoints[] = geometryText2Points($aPointPolygon['astext'],$fRadius);
1738 // Output data suitable for display (points and a bounding box)
1739 if (isset($aPolyPoints))
1741 $aResult['aPolyPoints'] = array();
1742 foreach($aPolyPoints as $aPoint)
1744 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1749 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
1751 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1752 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1754 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
1756 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1757 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1759 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1763 if ($aResult['extra_place'] == 'city')
1765 $aResult['class'] = 'place';
1766 $aResult['type'] = 'city';
1767 $aResult['rank_search'] = 16;
1770 if (!isset($aResult['aBoundingBox']))
1772 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1773 $fStepSize = (2*pi())/$iSteps;
1774 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1775 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1776 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1777 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1779 // Output data suitable for display (points and a bounding box)
1780 if ($this->bIncludePolygonAsPoints)
1782 $aPolyPoints = array();
1783 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1785 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1787 $aResult['aPolyPoints'] = array();
1788 foreach($aPolyPoints as $aPoint)
1790 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1793 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1796 // Is there an icon set for this type of result?
1797 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1798 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1800 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1803 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1804 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1806 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1808 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1809 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1811 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1813 // if tag '&addressdetails=1' is set in query
1814 if ($this->bIncludeAddressDetails)
1816 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1817 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1818 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1820 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1823 if ($this->bIncludeExtraTags)
1825 if ($aResult['extra'])
1827 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1831 $aResult['sExtraTags'] = (object) array();
1835 if ($this->bIncludeNameDetails)
1837 if ($aResult['names'])
1839 $aResult['sNameDetails'] = json_decode($aResult['names']);
1843 $aResult['sNameDetails'] = (object) array();
1847 // Adjust importance for the number of exact string matches in the result
1848 $aResult['importance'] = max(0.001,$aResult['importance']);
1850 $sAddress = $aResult['langaddress'];
1851 foreach($aRecheckWords as $i => $sWord)
1853 if (stripos($sAddress, $sWord)!==false)
1856 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1860 $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
1862 $aResult['name'] = $aResult['langaddress'];
1863 // secondary ordering (for results with same importance (the smaller the better):
1864 // - approximate importance of address parts
1865 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1866 // - number of exact matches from the query
1867 if (isset($this->exactMatchCache[$aResult['place_id']]))
1868 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1869 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1870 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1871 // - importance of the class/type
1872 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1873 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1875 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1879 $aResult['foundorder'] += 0.01;
1881 if (CONST_Debug) { var_dump($aResult); }
1882 $aSearchResults[$iResNum] = $aResult;
1884 uasort($aSearchResults, 'byImportance');
1886 $aOSMIDDone = array();
1887 $aClassTypeNameDone = array();
1888 $aToFilter = $aSearchResults;
1889 $aSearchResults = array();
1892 foreach($aToFilter as $iResNum => $aResult)
1894 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1897 $fLat = $aResult['lat'];
1898 $fLon = $aResult['lon'];
1899 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1902 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1903 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1905 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1906 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1907 $aSearchResults[] = $aResult;
1910 // Absolute limit on number of results
1911 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1914 return $aSearchResults;