2 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
3 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
9 protected $aLangPrefOrder = array();
11 protected $bIncludeAddressDetails = false;
12 protected $bIncludeExtraTags = false;
13 protected $bIncludeNameDetails = false;
15 protected $bIncludePolygonAsPoints = false;
16 protected $bIncludePolygonAsText = false;
17 protected $bIncludePolygonAsGeoJSON = false;
18 protected $bIncludePolygonAsKML = false;
19 protected $bIncludePolygonAsSVG = false;
20 protected $fPolygonSimplificationThreshold = 0.0;
22 protected $aExcludePlaceIDs = array();
23 protected $bDeDupe = true;
24 protected $bReverseInPlan = true;
26 protected $iLimit = 20;
27 protected $iFinalLimit = 10;
28 protected $iOffset = 0;
29 protected $bFallback = false;
31 protected $aCountryCodes = false;
32 protected $aNearPoint = false;
34 protected $bBoundedSearch = false;
35 protected $aViewBox = false;
36 protected $sViewboxSmallSQL = false;
37 protected $sViewboxLargeSQL = false;
38 protected $aRoutePoints = false;
40 protected $iMaxRank = 20;
41 protected $iMinAddressRank = 0;
42 protected $iMaxAddressRank = 30;
43 protected $aAddressRankList = array();
44 protected $exactMatchCache = array();
46 protected $sAllowedTypesSQLList = false;
48 protected $sQuery = false;
49 protected $aStructuredQuery = false;
51 function Geocode(&$oDB)
56 function setReverseInPlan($bReverse)
58 $this->bReverseInPlan = $bReverse;
61 function setLanguagePreference($aLangPref)
63 $this->aLangPrefOrder = $aLangPref;
66 function getIncludeAddressDetails()
68 return $this->bIncludeAddressDetails;
71 function getIncludeExtraTags()
73 return $this->bIncludeExtraTags;
76 function getIncludeNameDetails()
78 return $this->bIncludeNameDetails;
81 function setIncludePolygonAsPoints($b = true)
83 $this->bIncludePolygonAsPoints = $b;
86 function setIncludePolygonAsText($b = true)
88 $this->bIncludePolygonAsText = $b;
91 function setIncludePolygonAsGeoJSON($b = true)
93 $this->bIncludePolygonAsGeoJSON = $b;
96 function setIncludePolygonAsKML($b = true)
98 $this->bIncludePolygonAsKML = $b;
101 function setIncludePolygonAsSVG($b = true)
103 $this->bIncludePolygonAsSVG = $b;
106 function setPolygonSimplificationThreshold($f)
108 $this->fPolygonSimplificationThreshold = $f;
111 function setLimit($iLimit = 10)
113 if ($iLimit > 50) $iLimit = 50;
114 if ($iLimit < 1) $iLimit = 1;
116 $this->iFinalLimit = $iLimit;
117 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
120 function getExcludedPlaceIDs()
122 return $this->aExcludePlaceIDs;
125 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
127 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
130 function getViewBoxString()
132 if (!$this->aViewBox) return null;
133 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
136 function setFeatureType($sFeatureType)
138 switch($sFeatureType)
141 $this->setRankRange(4, 4);
144 $this->setRankRange(8, 8);
147 $this->setRankRange(14, 16);
150 $this->setRankRange(8, 20);
155 function setRankRange($iMin, $iMax)
157 $this->iMinAddressRank = $iMin;
158 $this->iMaxAddressRank = $iMax;
161 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
163 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
166 function setQuery($sQueryString)
168 $this->sQuery = $sQueryString;
169 $this->aStructuredQuery = false;
172 function getQueryString()
174 return $this->sQuery;
178 function loadParamArray($aParams)
180 if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
181 if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags'];
182 if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails'];
184 if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
185 if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
187 if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
188 if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
190 if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
192 // List of excluded Place IDs - used for more acurate pageing
193 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
195 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
197 $iExcludedPlaceID = (int)$iExcludedPlaceID;
198 if ($iExcludedPlaceID)
199 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
202 if (isset($aExcludePlaceIDs))
203 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
206 // Only certain ranks of feature
207 if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
208 if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
211 if (isset($aParams['countrycodes']))
213 $aCountryCodes = array();
214 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
216 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
218 $aCountryCodes[] = strtolower($sCountryCode);
221 $this->aCountryCodes = $aCountryCodes;
224 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
226 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
227 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
229 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
231 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
232 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
235 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
237 $aPoints = explode(',',$aParams['route']);
238 if (sizeof($aPoints) % 2 != 0)
240 userError("Uneven number of points");
245 foreach($aPoints as $i => $fPoint)
249 $aRoute[] = array((float)$fPoint, $fPrevCoord);
253 $fPrevCoord = (float)$fPoint;
256 $this->aRoutePoints = $aRoute;
260 function setQueryFromParams($aParams)
263 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
266 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
267 $this->setReverseInPlan(false);
271 $this->setQuery($sQuery);
275 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
277 $sValue = trim($sValue);
278 if (!$sValue) return false;
279 $this->aStructuredQuery[$sKey] = $sValue;
280 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
282 $this->iMinAddressRank = $iNewMinAddressRank;
283 $this->iMaxAddressRank = $iNewMaxAddressRank;
285 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
289 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
291 $this->sQuery = false;
294 $this->iMinAddressRank = 0;
295 $this->iMaxAddressRank = 30;
296 $this->aAddressRankList = array();
298 $this->aStructuredQuery = array();
299 $this->sAllowedTypesSQLList = '';
301 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
302 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
303 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
304 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
305 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
306 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
307 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
309 if (sizeof($this->aStructuredQuery) > 0)
311 $this->sQuery = join(', ', $this->aStructuredQuery);
312 if ($this->iMaxAddressRank < 30)
314 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
319 function fallbackStructuredQuery()
321 if (!$this->aStructuredQuery) return false;
323 $aParams = $this->aStructuredQuery;
325 if (sizeof($aParams) == 1) return false;
327 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
329 foreach($aOrderToFallback as $sType)
331 if (isset($aParams[$sType]))
333 unset($aParams[$sType]);
334 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
342 function getDetails($aPlaceIDs)
344 //$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
345 if (sizeof($aPlaceIDs) == 0) return array();
347 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
349 // Get the details for display (is this a redundant extra step?)
350 $sPlaceIDs = join(',', array_keys($aPlaceIDs));
352 $sImportanceSQL = '';
353 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
354 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
356 $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,";
357 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
358 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
359 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
360 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
361 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
362 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
363 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
364 $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, ";
365 $sSQL .= "(extratags->'place') as extra_place ";
366 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
367 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
368 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
369 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
371 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
372 $sSQL .= "and linked_place_id is null ";
373 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
374 if (!$this->bDeDupe) $sSQL .= ",place_id";
375 $sSQL .= ",langaddress ";
376 $sSQL .= ",placename ";
378 if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
379 if ($this->bIncludeNameDetails) $sSQL .= ",name";
380 $sSQL .= ",extratags->'place' ";
382 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
384 //only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines
385 // with start- and endnumber, the common osm housenumbers are usually saved as points
388 $length = count($aPlaceIDs);
389 foreach($aPlaceIDs as $placeID => $housenumber)
392 $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
394 $sHousenumbers .= ", ";
396 if (CONST_Use_US_Tiger_Data)
398 //Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
400 $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";
401 $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
402 $sSQL .= ", null as placename";
403 $sSQL .= ", null as ref";
404 if ($this->bIncludeExtraTags) $sSQL .= ", null as extra";
405 if ($this->bIncludeNameDetails) $sSQL .= ", null as names";
406 $sSQL .= ", avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
407 $sSQL .= $sImportanceSQL."-1.15 as importance ";
408 $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 ";
409 $sSQL .= ", null as extra_place ";
410 $sSQL .= " from (select place_id";
411 //interpolate the Tiger housenumbers here
412 $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as centroid, parent_place_id, housenumber_for_place";
413 $sSQL .= " from (location_property_tiger ";
414 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
415 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
416 $sSQL .= " group by place_id, housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
417 if (!$this->bDeDupe) $sSQL .= ", place_id ";
420 // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
422 $sSQL .= "select 'W' 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, calculated_country_code as country_code, ";
423 $sSQL .= "get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress, ";
424 $sSQL .= "null as placename, ";
425 $sSQL .= "null as ref, ";
426 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
427 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
428 $sSQL .= " avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
429 $sSQL .= $sImportanceSQL."-0.1 as importance, "; // slightly smaller than the importance for normal houses with rank 30, which is 0
430 $sSQL .= " (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p";
431 $sSQL .= " 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,";
432 $sSQL .= " null as extra_place ";
433 $sSQL .= " from (select place_id, calculated_country_code ";
434 //interpolate the housenumbers here
435 $sSQL .= ", CASE WHEN startnumber != endnumber THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
436 $sSQL .= " ELSE ST_LineInterpolatePoint(linegeo, 0.5) END as centroid";
437 $sSQL .= ", parent_place_id, housenumber_for_place ";
438 $sSQL .= " from (location_property_osmline ";
439 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
440 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
441 $sSQL .= " group by place_id, housenumber_for_place, calculated_country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
442 if (!$this->bDeDupe) $sSQL .= ", place_id ";
444 if (CONST_Use_Aux_Location_data)
447 $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, ";
448 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress, ";
449 $sSQL .= "null as placename, ";
450 $sSQL .= "null as ref, ";
451 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
452 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
453 $sSQL .= "avg(ST_X(centroid)) as lon, avg(ST_Y(centroid)) as lat, ";
454 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
455 $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, ";
456 $sSQL .= "null as extra_place ";
457 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
458 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
459 $sSQL .= "group by place_id";
460 if (!$this->bDeDupe) $sSQL .= ", place_id";
461 $sSQL .= ", get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) ";
465 $sSQL .= " order by importance desc";
466 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
467 $aSearchResults = chksql($this->oDB->getAll($sSQL),
468 "Could not get details for place.");
470 return $aSearchResults;
473 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
476 Calculate all searches using aValidTokens i.e.
477 'Wodsworth Road, Sheffield' =>
481 0 1 (wodsworth)(road)
484 Score how good the search is so they can be ordered
486 foreach($aPhrases as $iPhrase => $sPhrase)
488 $aNewPhraseSearches = array();
489 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
490 else $sPhraseType = '';
492 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
494 // Too many permutations - too expensive
495 if ($iWordSet > 120) break;
497 $aWordsetSearches = $aSearches;
499 // Add all words from this wordset
500 foreach($aWordset as $iToken => $sToken)
502 //echo "<br><b>$sToken</b>";
503 $aNewWordsetSearches = array();
505 foreach($aWordsetSearches as $aCurrentSearch)
508 //var_dump($aCurrentSearch);
511 // If the token is valid
512 if (isset($aValidTokens[' '.$sToken]))
514 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
516 $aSearch = $aCurrentSearch;
517 $aSearch['iSearchRank']++;
518 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
520 if ($aSearch['sCountryCode'] === false)
522 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
523 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
524 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
526 $aSearch['iSearchRank'] += 5;
528 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
531 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
533 if ($aSearch['fLat'] === '')
535 $aSearch['fLat'] = $aSearchTerm['lat'];
536 $aSearch['fLon'] = $aSearchTerm['lon'];
537 $aSearch['fRadius'] = $aSearchTerm['radius'];
538 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
541 elseif ($sPhraseType == 'postalcode')
543 // 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
544 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
546 // If we already have a name try putting the postcode first
547 if (sizeof($aSearch['aName']))
549 $aNewSearch = $aSearch;
550 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
551 $aNewSearch['aName'] = array();
552 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
553 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
556 if (sizeof($aSearch['aName']))
558 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
560 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
564 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
565 $aSearch['iSearchRank'] += 1000; // skip;
570 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
571 //$aSearch['iNamePhrase'] = $iPhrase;
573 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
577 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
579 if ($aSearch['sHouseNumber'] === '')
581 $aSearch['sHouseNumber'] = $sToken;
582 // sanity check: if the housenumber is not mainly made
583 // up of numbers, add a penalty
584 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
585 // also housenumbers should appear in the first or second phrase
586 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
587 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
589 // Fall back to not searching for this item (better than nothing)
590 $aSearch = $aCurrentSearch;
591 $aSearch['iSearchRank'] += 1;
592 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
596 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
598 if ($aSearch['sClass'] === '')
600 $aSearch['sOperator'] = $aSearchTerm['operator'];
601 $aSearch['sClass'] = $aSearchTerm['class'];
602 $aSearch['sType'] = $aSearchTerm['type'];
603 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
604 else $aSearch['sOperator'] = 'near'; // near = in for the moment
605 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
607 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
610 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
612 if (sizeof($aSearch['aName']))
614 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
616 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
620 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
621 $aSearch['iSearchRank'] += 1000; // skip;
626 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
627 //$aSearch['iNamePhrase'] = $iPhrase;
629 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
633 // Look for partial matches.
634 // Note that there is no point in adding country terms here
635 // because country are omitted in the address.
636 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
638 // Allow searching for a word - but at extra cost
639 foreach($aValidTokens[$sToken] as $aSearchTerm)
641 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
643 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
645 $aSearch = $aCurrentSearch;
646 $aSearch['iSearchRank'] += 1;
647 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
649 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
650 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
652 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
654 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
655 $aSearch['iSearchRank'] += 1;
656 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
657 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
659 if (empty($aSearchTermToken['country_code'])
660 && empty($aSearchTermToken['lat'])
661 && empty($aSearchTermToken['class']))
663 $aSearch = $aCurrentSearch;
664 $aSearch['iSearchRank'] += 1;
665 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
666 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
672 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
673 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
674 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
678 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
680 $aSearch = $aCurrentSearch;
681 $aSearch['iSearchRank'] += 1;
682 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
683 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
684 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
685 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
687 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
688 $aSearch['iNamePhrase'] = $iPhrase;
689 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
696 // Allow skipping a word - but at EXTREAM cost
697 //$aSearch = $aCurrentSearch;
698 //$aSearch['iSearchRank']+=100;
699 //$aNewWordsetSearches[] = $aSearch;
703 usort($aNewWordsetSearches, 'bySearchRank');
704 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
706 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
708 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
709 usort($aNewPhraseSearches, 'bySearchRank');
711 $aSearchHash = array();
712 foreach($aNewPhraseSearches as $iSearch => $aSearch)
714 $sHash = serialize($aSearch);
715 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
716 else $aSearchHash[$sHash] = 1;
719 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
722 // Re-group the searches by their score, junk anything over 20 as just not worth trying
723 $aGroupedSearches = array();
724 foreach($aNewPhraseSearches as $aSearch)
726 if ($aSearch['iSearchRank'] < $this->iMaxRank)
728 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
729 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
732 ksort($aGroupedSearches);
735 $aSearches = array();
736 foreach($aGroupedSearches as $iScore => $aNewSearches)
738 $iSearchCount += sizeof($aNewSearches);
739 $aSearches = array_merge($aSearches, $aNewSearches);
740 if ($iSearchCount > 50) break;
743 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
746 return $aGroupedSearches;
750 /* Perform the actual query lookup.
752 Returns an ordered list of results, each with the following fields:
753 osm_type: type of corresponding OSM object
757 P - postcode (internally computed)
758 osm_id: id of corresponding OSM object
759 class: general object class (corresponds to tag key of primary OSM tag)
760 type: subclass of object (corresponds to tag value of primary OSM tag)
761 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
762 rank_search: rank in search hierarchy
763 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
764 rank_address: rank in address hierarchy (determines orer in address)
765 place_id: internal key (may differ between different instances)
766 country_code: ISO country code
767 langaddress: localized full address
768 placename: localized name of object
769 ref: content of ref tag (if available)
772 importance: importance of place based on Wikipedia link count
773 addressimportance: cumulated importance of address elements
774 extra_place: type of place (for admin boundaries, if there is a place tag)
775 aBoundingBox: bounding Box
776 label: short description of the object class/type (English only)
777 name: full name (currently the same as langaddress)
778 foundorder: secondary ordering for places with same importance
782 if (!$this->sQuery && !$this->aStructuredQuery) return false;
784 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
785 $sCountryCodesSQL = false;
786 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
788 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
791 $sQuery = $this->sQuery;
793 // Conflicts between US state abreviations and various words for 'the' in different languages
794 if (isset($this->aLangPrefOrder['name:en']))
796 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
797 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
798 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
802 $sViewboxCentreSQL = false;
803 $bBoundingBoxSearch = false;
806 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
807 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
808 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
809 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
810 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
811 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
813 $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)";
814 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
815 $bBoundingBoxSearch = $this->bBoundedSearch;
819 if ($this->aRoutePoints)
821 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
823 foreach($this->aRoutePoints as $aPoint)
825 if (!$bFirst) $sViewboxCentreSQL .= ",";
826 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
829 $sViewboxCentreSQL .= ")'::geometry,4326)";
831 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
832 $this->sViewboxSmallSQL = chksql($this->oDB->getOne($sSQL),
833 "Could not get small viewbox.");
834 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
836 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
837 $this->sViewboxLargeSQL = chksql($this->oDB->getOne($sSQL),
838 "Could not get large viewbox.");
839 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
840 $bBoundingBoxSearch = $this->bBoundedSearch;
843 // Do we have anything that looks like a lat/lon pair?
844 if ( $aLooksLike = looksLikeLatLonPair($sQuery) )
846 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
847 $sQuery = $aLooksLike['query'];
850 $aSearchResults = array();
851 if ($sQuery || $this->aStructuredQuery)
853 // Start with a blank search
855 array('iSearchRank' => 0,
857 'sCountryCode' => false,
859 'aAddress' => array(),
860 'aFullNameAddress' => array(),
861 'aNameNonSearch' => array(),
862 'aAddressNonSearch' => array(),
864 'aFeatureName' => array(),
867 'sHouseNumber' => '',
874 // Do we have a radius search?
875 $sNearPointSQL = false;
876 if ($this->aNearPoint)
878 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
879 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
880 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
881 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
884 // Any 'special' terms in the search?
885 $bSpecialTerms = false;
886 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
887 $aSpecialTerms = array();
888 foreach($aSpecialTermsRaw as $aSpecialTerm)
890 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
891 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
894 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
895 $aSpecialTerms = array();
896 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity'])
898 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
899 unset($this->aStructuredQuery['amenity']);
901 foreach($aSpecialTermsRaw as $aSpecialTerm)
903 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
904 $sToken = chksql($this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string"));
905 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
906 $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';
907 if (CONST_Debug) var_Dump($sSQL);
908 $aSearchWords = chksql($this->oDB->getAll($sSQL));
909 $aNewSearches = array();
910 foreach($aSearches as $aSearch)
912 foreach($aSearchWords as $aSearchTerm)
914 $aNewSearch = $aSearch;
915 if ($aSearchTerm['country_code'])
917 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
918 $aNewSearches[] = $aNewSearch;
919 $bSpecialTerms = true;
921 if ($aSearchTerm['class'])
923 $aNewSearch['sClass'] = $aSearchTerm['class'];
924 $aNewSearch['sType'] = $aSearchTerm['type'];
925 $aNewSearches[] = $aNewSearch;
926 $bSpecialTerms = true;
930 $aSearches = $aNewSearches;
933 // Split query into phrases
934 // Commas are used to reduce the search space by indicating where phrases split
935 if ($this->aStructuredQuery)
937 $aPhrases = $this->aStructuredQuery;
938 $bStructuredPhrases = true;
942 $aPhrases = explode(',',$sQuery);
943 $bStructuredPhrases = false;
946 // Convert each phrase to standard form
947 // Create a list of standard words
948 // Get all 'sets' of words
949 // Generate a complete list of all
951 foreach($aPhrases as $iPhrase => $sPhrase)
953 $aPhrase = chksql($this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string"),
954 "Cannot nomralize query string (is it an UTF-8 string?)");
955 if (trim($aPhrase['string']))
957 $aPhrases[$iPhrase] = $aPhrase;
958 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
959 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
960 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
964 unset($aPhrases[$iPhrase]);
968 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
969 $aPhraseTypes = array_keys($aPhrases);
970 $aPhrases = array_values($aPhrases);
972 if (sizeof($aTokens))
974 // Check which tokens we have, get the ID numbers
975 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
976 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
978 if (CONST_Debug) var_Dump($sSQL);
980 $aValidTokens = array();
981 if (sizeof($aTokens))
983 $aDatabaseWords = chksql($this->oDB->getAll($sSQL),
984 "Could not get word tokens.");
988 $aDatabaseWords = array();
990 $aPossibleMainWordIDs = array();
991 $aWordFrequencyScores = array();
992 foreach($aDatabaseWords as $aToken)
994 // Very special case - require 2 letter country param to match the country code found
995 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
996 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code'])
1001 if (isset($aValidTokens[$aToken['word_token']]))
1003 $aValidTokens[$aToken['word_token']][] = $aToken;
1007 $aValidTokens[$aToken['word_token']] = array($aToken);
1009 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1010 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1012 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1014 // Try and calculate GB postcodes we might be missing
1015 foreach($aTokens as $sToken)
1017 // Source of gb postcodes is now definitive - always use
1018 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
1020 if (substr($aData[1],-2,1) != ' ')
1022 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1023 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1025 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1026 if ($aGBPostcodeLocation)
1028 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1031 // US ZIP+4 codes - if there is no token,
1032 // merge in the 5-digit ZIP code
1033 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1035 if (isset($aValidTokens[$aData[1]]))
1037 foreach($aValidTokens[$aData[1]] as $aToken)
1039 if (!$aToken['class'])
1041 if (isset($aValidTokens[$sToken]))
1043 $aValidTokens[$sToken][] = $aToken;
1047 $aValidTokens[$sToken] = array($aToken);
1055 foreach($aTokens as $sToken)
1057 // Unknown single word token with a number - assume it is a house number
1058 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1060 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1064 // Any words that have failed completely?
1065 // TODO: suggestions
1067 // Start the search process
1068 // array with: placeid => -1 | tiger-housenumber
1069 $aResultPlaceIDs = array();
1071 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1073 if ($this->bReverseInPlan)
1075 // Reverse phrase array and also reverse the order of the wordsets in
1076 // the first and final phrase. Don't bother about phrases in the middle
1077 // because order in the address doesn't matter.
1078 $aPhrases = array_reverse($aPhrases);
1079 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1080 if (sizeof($aPhrases) > 1)
1082 $aFinalPhrase = end($aPhrases);
1083 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1085 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1087 foreach($aGroupedSearches as $aSearches)
1089 foreach($aSearches as $aSearch)
1091 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1093 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1094 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1100 $aGroupedSearches = $aReverseGroupedSearches;
1101 ksort($aGroupedSearches);
1106 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1107 $aGroupedSearches = array();
1108 foreach($aSearches as $aSearch)
1110 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1112 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1113 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1116 ksort($aGroupedSearches);
1119 if (CONST_Debug) var_Dump($aGroupedSearches);
1121 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0)
1123 $aCopyGroupedSearches = $aGroupedSearches;
1124 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1126 foreach($aSearches as $iSearch => $aSearch)
1128 $aReductionsList = array($aSearch['aAddress']);
1129 $iSearchRank = $aSearch['iSearchRank'];
1130 while(sizeof($aReductionsList) > 0)
1133 if ($iSearchRank > iMaxRank) break 3;
1134 $aNewReductionsList = array();
1135 foreach($aReductionsList as $aReductionsWordList)
1137 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1139 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1140 $aReverseSearch = $aSearch;
1141 $aSearch['aAddress'] = $aReductionsWordListResult;
1142 $aSearch['iSearchRank'] = $iSearchRank;
1143 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1144 if (sizeof($aReductionsWordListResult) > 0)
1146 $aNewReductionsList[] = $aReductionsWordListResult;
1150 $aReductionsList = $aNewReductionsList;
1154 ksort($aGroupedSearches);
1157 // Filter out duplicate searches
1158 $aSearchHash = array();
1159 foreach($aGroupedSearches as $iGroup => $aSearches)
1161 foreach($aSearches as $iSearch => $aSearch)
1163 $sHash = serialize($aSearch);
1164 if (isset($aSearchHash[$sHash]))
1166 unset($aGroupedSearches[$iGroup][$iSearch]);
1167 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1171 $aSearchHash[$sHash] = 1;
1176 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1180 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1183 foreach($aSearches as $aSearch)
1186 $searchedHousenumber = -1;
1188 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1189 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1191 // No location term?
1192 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1194 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1196 // Just looking for a country by code - look it up
1197 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1199 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1200 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1201 if ($bBoundingBoxSearch)
1202 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1203 $sSQL .= " order by st_area(geometry) desc limit 1";
1204 if (CONST_Debug) var_dump($sSQL);
1205 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1209 $aPlaceIDs = array();
1214 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1215 if (!$aSearch['sClass']) continue;
1216 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1217 if (chksql($this->oDB->getOne($sSQL)))
1219 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1220 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1221 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1222 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1223 if (sizeof($this->aExcludePlaceIDs))
1225 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1227 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1228 $sSQL .= " limit $this->iLimit";
1229 if (CONST_Debug) var_dump($sSQL);
1230 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1232 // If excluded place IDs are given, it is fair to assume that
1233 // there have been results in the small box, so no further
1234 // expansion in that case.
1235 // Also don't expand if bounded results were requested.
1236 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1238 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1239 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1240 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1241 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1242 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1243 $sSQL .= " limit $this->iLimit";
1244 if (CONST_Debug) var_dump($sSQL);
1245 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1250 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1251 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1252 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1253 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1254 $sSQL .= " limit $this->iLimit";
1255 if (CONST_Debug) var_dump($sSQL);
1256 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1260 // If a coordinate is given, the search must either
1261 // be for a name or a special search. Ignore everythin else.
1262 else if ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass'])
1264 $aPlaceIDs = array();
1268 $aPlaceIDs = array();
1270 // First we need a position, either aName or fLat or both
1274 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1276 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1278 $aOrder[0] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
1279 $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
1280 // also housenumbers from interpolation lines table are needed
1281 $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
1282 $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
1283 $aOrder[0] .= " desc";
1286 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1287 // they might be right - but they are just too darned expensive to run
1288 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1289 //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1290 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1292 // For infrequent name terms disable index usage for address
1293 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1294 sizeof($aSearch['aName']) == 1 &&
1295 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1297 //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1298 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1302 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1303 //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1306 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1307 if ($aSearch['sHouseNumber'])
1309 $aTerms[] = "address_rank between 16 and 27";
1313 if ($this->iMinAddressRank > 0)
1315 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1317 if ($this->iMaxAddressRank < 30)
1319 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1322 if ($aSearch['fLon'] && $aSearch['fLat'])
1324 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1325 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1327 if (sizeof($this->aExcludePlaceIDs))
1329 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1331 if ($sCountryCodesSQL)
1333 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1336 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1337 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1339 if ($aSearch['sHouseNumber'])
1341 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1345 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1347 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1348 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1350 $aOrder[] = "$sImportanceSQL DESC";
1351 if (sizeof($aSearch['aFullNameAddress']))
1353 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1354 $aOrder[] = 'exactmatch DESC';
1356 $sExactMatchSQL = '0::int as exactmatch';
1359 if (sizeof($aTerms))
1361 $sSQL = "select place_id, ";
1362 $sSQL .= $sExactMatchSQL;
1363 $sSQL .= " from search_name";
1364 $sSQL .= " where ".join(' and ',$aTerms);
1365 $sSQL .= " order by ".join(', ',$aOrder);
1366 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1367 $sSQL .= " limit 20";
1368 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1369 $sSQL .= " limit 1";
1371 $sSQL .= " limit ".$this->iLimit;
1373 if (CONST_Debug) { var_dump($sSQL); }
1374 $aViewBoxPlaceIDs = chksql($this->oDB->getAll($sSQL),
1375 "Could not get places for search terms.");
1376 //var_dump($aViewBoxPlaceIDs);
1377 // Did we have an viewbox matches?
1378 $aPlaceIDs = array();
1379 $bViewBoxMatch = false;
1380 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1382 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1383 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1384 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1385 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1386 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1387 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1390 //var_Dump($aPlaceIDs);
1393 //now search for housenumber, if housenumber provided
1394 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1396 $searchedHousenumber = intval($aSearch['sHouseNumber']);
1397 $aRoadPlaceIDs = $aPlaceIDs;
1398 $sPlaceIDs = join(',',$aPlaceIDs);
1400 // Now they are indexed, look for a house attached to a street we found
1401 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1402 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1403 if (sizeof($this->aExcludePlaceIDs))
1405 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1407 $sSQL .= " limit $this->iLimit";
1408 if (CONST_Debug) var_dump($sSQL);
1409 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1411 // if nothing found, search in the interpolation line table
1412 if(!sizeof($aPlaceIDs))
1414 // do we need to use transliteration and the regex for housenumbers???
1415 //new query for lines, not housenumbers anymore
1416 if($searchedHousenumber%2 == 0){
1417 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1418 $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
1420 //look for housenumber in streets with interpolationtype odd or all
1421 $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
1424 if (sizeof($this->aExcludePlaceIDs))
1426 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1428 //$sSQL .= " limit $this->iLimit";
1429 if (CONST_Debug) var_dump($sSQL);
1431 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1434 // If nothing found try the aux fallback table
1435 if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs))
1437 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1438 if (sizeof($this->aExcludePlaceIDs))
1440 $sSQL .= " and parent_place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1442 //$sSQL .= " limit $this->iLimit";
1443 if (CONST_Debug) var_dump($sSQL);
1444 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1448 //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1449 if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs))
1451 //new query for lines, not housenumbers anymore
1452 if($searchedHousenumber%2 == 0){
1453 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1454 $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";
1456 //look for housenumber in streets with interpolationtype odd or all
1457 $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";
1460 if (sizeof($this->aExcludePlaceIDs))
1462 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1464 //$sSQL .= " limit $this->iLimit";
1465 if (CONST_Debug) var_dump($sSQL);
1467 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1470 // Fallback to the road (if no housenumber was found)
1471 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1473 $aPlaceIDs = $aRoadPlaceIDs;
1474 //set to -1, if no housenumbers were found
1475 $searchedHousenumber = -1;
1477 //else: housenumber was found, remains saved in searchedHousenumber
1481 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1483 $sPlaceIDs = join(',', $aPlaceIDs);
1484 $aClassPlaceIDs = array();
1486 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1488 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1489 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1490 $sSQL .= " and linked_place_id is null";
1491 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1492 $sSQL .= " order by rank_search asc limit $this->iLimit";
1493 if (CONST_Debug) var_dump($sSQL);
1494 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
1497 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1499 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1500 $bCacheTable = chksql($this->oDB->getOne($sSQL));
1502 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1504 if (CONST_Debug) var_dump($sSQL);
1505 $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
1507 // For state / country level searches the normal radius search doesn't work very well
1508 $sPlaceGeom = false;
1509 if ($this->iMaxRank < 9 && $bCacheTable)
1511 // Try and get a polygon to search in instead
1512 $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";
1513 if (CONST_Debug) var_dump($sSQL);
1514 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
1523 $this->iMaxRank += 5;
1524 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1525 if (CONST_Debug) var_dump($sSQL);
1526 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1527 $sPlaceIDs = join(',',$aPlaceIDs);
1530 if ($sPlaceIDs || $sPlaceGeom)
1536 // More efficient - can make the range bigger
1540 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1541 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1542 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1544 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1545 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1548 $sSQL .= ",placex as f where ";
1549 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1554 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1556 if (sizeof($this->aExcludePlaceIDs))
1558 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1560 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1561 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1562 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1563 $sSQL .= " limit $this->iLimit";
1564 if (CONST_Debug) var_dump($sSQL);
1565 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1569 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1572 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1573 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1575 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1576 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1577 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1578 if (sizeof($this->aExcludePlaceIDs))
1580 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1582 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1583 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1584 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1585 $sSQL .= " limit $this->iLimit";
1586 if (CONST_Debug) var_dump($sSQL);
1587 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1592 $aPlaceIDs = $aClassPlaceIDs;
1598 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1600 foreach($aPlaceIDs as $iPlaceID)
1602 // array for placeID => -1 | Tiger housenumber
1603 $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1605 if ($iQueryLoop > 20) break;
1608 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1610 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1611 // reduces the number of place ids, like a filter
1612 // rank_address is 30 for interpolated housenumbers
1613 $sSQL = "select place_id from placex where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1614 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1615 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1616 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1617 if (CONST_Use_US_Tiger_Data)
1619 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1620 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1621 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1623 $sSQL .= ") UNION select place_id from location_property_osmline where place_id in (".join(',',array_keys($aResultPlaceIDs)).")";
1624 $sSQL .= " and (30 between $this->iMinAddressRank and $this->iMaxAddressRank)";
1625 if (CONST_Debug) var_dump($sSQL);
1626 $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1628 foreach($aFilteredPlaceIDs as $placeID)
1630 $tempIDs[$placeID] = $aResultPlaceIDs[$placeID]; //assign housenumber to placeID
1632 $aResultPlaceIDs = $tempIDs;
1636 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1637 if ($iGroupLoop > 4) break;
1638 if ($iQueryLoop > 30) break;
1641 // Did we find anything?
1642 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1644 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1650 // Just interpret as a reverse geocode
1651 $oReverse = new ReverseGeocode($this->oDB);
1652 $oReverse->setZoom(18);
1654 $aLookup = $oReverse->lookup((float)$this->aNearPoint[0],
1655 (float)$this->aNearPoint[1],
1658 if (CONST_Debug) var_dump("Reverse search", $aLookup);
1660 if ($aLookup['place_id'])
1661 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
1663 $aSearchResults = array();
1667 if (!sizeof($aSearchResults))
1669 if ($this->bFallback)
1671 if ($this->fallbackStructuredQuery())
1673 return $this->lookup();
1680 $aClassType = getClassTypesWithImportance();
1681 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1682 foreach($aRecheckWords as $i => $sWord)
1684 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1687 if (CONST_Debug) { echo '<i>Recheck words:<\i>'; var_dump($aRecheckWords); }
1689 $oPlaceLookup = new PlaceLookup($this->oDB);
1690 $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints);
1691 $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText);
1692 $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON);
1693 $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML);
1694 $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG);
1695 $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold);
1697 foreach($aSearchResults as $iResNum => $aResult)
1700 $fDiameter = getResultDiameter($aResult);
1702 $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2);
1703 if ($aOutlineResult)
1705 $aResult = array_merge($aResult, $aOutlineResult);
1708 if ($aResult['extra_place'] == 'city')
1710 $aResult['class'] = 'place';
1711 $aResult['type'] = 'city';
1712 $aResult['rank_search'] = 16;
1715 // Is there an icon set for this type of result?
1716 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1717 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1719 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1722 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1723 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1725 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1727 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1728 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1730 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1732 // if tag '&addressdetails=1' is set in query
1733 if ($this->bIncludeAddressDetails)
1735 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1736 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1737 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1739 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1742 if ($this->bIncludeExtraTags)
1744 if ($aResult['extra'])
1746 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1750 $aResult['sExtraTags'] = (object) array();
1754 if ($this->bIncludeNameDetails)
1756 if ($aResult['names'])
1758 $aResult['sNameDetails'] = json_decode($aResult['names']);
1762 $aResult['sNameDetails'] = (object) array();
1766 // Adjust importance for the number of exact string matches in the result
1767 $aResult['importance'] = max(0.001,$aResult['importance']);
1769 $sAddress = $aResult['langaddress'];
1770 foreach($aRecheckWords as $i => $sWord)
1772 if (stripos($sAddress, $sWord)!==false)
1775 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1779 $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
1781 $aResult['name'] = $aResult['langaddress'];
1782 // secondary ordering (for results with same importance (the smaller the better):
1783 // - approximate importance of address parts
1784 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1785 // - number of exact matches from the query
1786 if (isset($this->exactMatchCache[$aResult['place_id']]))
1787 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1788 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1789 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1790 // - importance of the class/type
1791 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1792 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1794 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1798 $aResult['foundorder'] += 0.01;
1800 if (CONST_Debug) { var_dump($aResult); }
1801 $aSearchResults[$iResNum] = $aResult;
1803 uasort($aSearchResults, 'byImportance');
1805 $aOSMIDDone = array();
1806 $aClassTypeNameDone = array();
1807 $aToFilter = $aSearchResults;
1808 $aSearchResults = array();
1811 foreach($aToFilter as $iResNum => $aResult)
1813 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1816 $fLat = $aResult['lat'];
1817 $fLon = $aResult['lon'];
1818 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1821 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1822 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1824 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1825 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1826 $aSearchResults[] = $aResult;
1829 // Absolute limit on number of results
1830 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1833 return $aSearchResults;