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 = false;
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 setIncludeAddressDetails($bAddressDetails = true)
68 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
71 function getIncludeAddressDetails()
73 return $this->bIncludeAddressDetails;
76 function getIncludeExtraTags()
78 return $this->bIncludeExtraTags;
81 function getIncludeNameDetails()
83 return $this->bIncludeNameDetails;
86 function setIncludePolygonAsPoints($b = true)
88 $this->bIncludePolygonAsPoints = $b;
91 function getIncludePolygonAsPoints()
93 return $this->bIncludePolygonAsPoints;
96 function setIncludePolygonAsText($b = true)
98 $this->bIncludePolygonAsText = $b;
101 function getIncludePolygonAsText()
103 return $this->bIncludePolygonAsText;
106 function setIncludePolygonAsGeoJSON($b = true)
108 $this->bIncludePolygonAsGeoJSON = $b;
111 function setIncludePolygonAsKML($b = true)
113 $this->bIncludePolygonAsKML = $b;
116 function setIncludePolygonAsSVG($b = true)
118 $this->bIncludePolygonAsSVG = $b;
121 function setPolygonSimplificationThreshold($f)
123 $this->fPolygonSimplificationThreshold = $f;
126 function setDeDupe($bDeDupe = true)
128 $this->bDeDupe = (bool)$bDeDupe;
131 function setLimit($iLimit = 10)
133 if ($iLimit > 50) $iLimit = 50;
134 if ($iLimit < 1) $iLimit = 1;
136 $this->iFinalLimit = $iLimit;
137 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
140 function setOffset($iOffset = 0)
142 $this->iOffset = $iOffset;
145 function setFallback($bFallback = true)
147 $this->bFallback = (bool)$bFallback;
150 function setExcludedPlaceIDs($a)
152 // TODO: force to int
153 $this->aExcludePlaceIDs = $a;
156 function getExcludedPlaceIDs()
158 return $this->aExcludePlaceIDs;
161 function setBounded($bBoundedSearch = true)
163 $this->bBoundedSearch = (bool)$bBoundedSearch;
166 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
168 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
171 function getViewBoxString()
173 if (!$this->aViewBox) return null;
174 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
177 function setRoute($aRoutePoints)
179 $this->aRoutePoints = $aRoutePoints;
182 function setFeatureType($sFeatureType)
184 switch($sFeatureType)
187 $this->setRankRange(4, 4);
190 $this->setRankRange(8, 8);
193 $this->setRankRange(14, 16);
196 $this->setRankRange(8, 20);
201 function setRankRange($iMin, $iMax)
203 $this->iMinAddressRank = (int)$iMin;
204 $this->iMaxAddressRank = (int)$iMax;
207 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
209 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
212 function setCountryCodesList($aCountryCodes)
214 $this->aCountryCodes = $aCountryCodes;
217 function setQuery($sQueryString)
219 $this->sQuery = $sQueryString;
220 $this->aStructuredQuery = false;
223 function getQueryString()
225 return $this->sQuery;
229 function loadParamArray($aParams)
231 if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
232 if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags'];
233 if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails'];
235 if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
236 if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
238 if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
239 if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
241 if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
243 // List of excluded Place IDs - used for more acurate pageing
244 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
246 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
248 $iExcludedPlaceID = (int)$iExcludedPlaceID;
249 if ($iExcludedPlaceID)
250 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
253 if (isset($aExcludePlaceIDs))
254 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
257 // Only certain ranks of feature
258 if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
259 if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
262 if (isset($aParams['countrycodes']))
264 $aCountryCodes = array();
265 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
267 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
269 $aCountryCodes[] = strtolower($sCountryCode);
272 $this->aCountryCodes = $aCountryCodes;
275 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
277 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
278 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
280 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
282 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
283 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
286 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
288 $aPoints = explode(',',$aParams['route']);
289 if (sizeof($aPoints) % 2 != 0)
291 userError("Uneven number of points");
296 foreach($aPoints as $i => $fPoint)
300 $aRoute[] = array((float)$fPoint, $fPrevCoord);
304 $fPrevCoord = (float)$fPoint;
307 $this->aRoutePoints = $aRoute;
311 function setQueryFromParams($aParams)
314 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
317 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
318 $this->setReverseInPlan(false);
322 $this->setQuery($sQuery);
326 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
328 $sValue = trim($sValue);
329 if (!$sValue) return false;
330 $this->aStructuredQuery[$sKey] = $sValue;
331 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
333 $this->iMinAddressRank = $iNewMinAddressRank;
334 $this->iMaxAddressRank = $iNewMaxAddressRank;
336 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
340 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
342 $this->sQuery = false;
345 $this->iMinAddressRank = 0;
346 $this->iMaxAddressRank = 30;
347 $this->aAddressRankList = array();
349 $this->aStructuredQuery = array();
350 $this->sAllowedTypesSQLList = '';
352 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
353 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
354 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
355 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
356 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
357 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
358 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
360 if (sizeof($this->aStructuredQuery) > 0)
362 $this->sQuery = join(', ', $this->aStructuredQuery);
363 if ($this->iMaxAddressRank < 30)
365 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
370 function fallbackStructuredQuery()
372 if (!$this->aStructuredQuery) return false;
374 $aParams = $this->aStructuredQuery;
376 if (sizeof($aParams) == 1) return false;
378 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
380 foreach($aOrderToFallback as $sType)
382 if (isset($aParams[$sType]))
384 unset($aParams[$sType]);
385 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
393 function getDetails($aPlaceIDs)
395 //$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
396 if (sizeof($aPlaceIDs) == 0) return array();
398 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
400 // Get the details for display (is this a redundant extra step?)
401 $sPlaceIDs = join(',', array_keys($aPlaceIDs));
403 $sImportanceSQL = '';
404 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
405 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
407 $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,";
408 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
409 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
410 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
411 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
412 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
413 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
414 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
415 $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, ";
416 $sSQL .= "(extratags->'place') as extra_place ";
417 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
418 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
419 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
420 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
422 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
423 $sSQL .= "and linked_place_id is null ";
424 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
425 if (!$this->bDeDupe) $sSQL .= ",place_id";
426 $sSQL .= ",langaddress ";
427 $sSQL .= ",placename ";
429 if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
430 if ($this->bIncludeNameDetails) $sSQL .= ",name";
431 $sSQL .= ",extratags->'place' ";
433 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
435 //only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines
436 // with start- and endnumber, the common osm housenumbers are usually saved as points
439 $length = count($aPlaceIDs);
440 foreach($aPlaceIDs as $placeID => $housenumber)
443 $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
445 $sHousenumbers .= ", ";
447 if (CONST_Use_US_Tiger_Data)
449 //Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
451 $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";
452 $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
453 $sSQL .= ", null as placename";
454 $sSQL .= ", null as ref";
455 if ($this->bIncludeExtraTags) $sSQL .= ", null as extra";
456 if ($this->bIncludeNameDetails) $sSQL .= ", null as names";
457 $sSQL .= ", avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
458 $sSQL .= $sImportanceSQL."-1.15 as importance ";
459 $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 ";
460 $sSQL .= ", null as extra_place ";
461 $sSQL .= " from (select place_id";
462 //interpolate the Tiger housenumbers here
463 $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as centroid, parent_place_id, housenumber_for_place";
464 $sSQL .= " from (location_property_tiger ";
465 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
466 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
467 $sSQL .= " group by place_id, housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
468 if (!$this->bDeDupe) $sSQL .= ", place_id ";
471 // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
473 $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, ";
474 $sSQL .= "get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress, ";
475 $sSQL .= "null as placename, ";
476 $sSQL .= "null as ref, ";
477 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
478 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
479 $sSQL .= " avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
480 $sSQL .= $sImportanceSQL."-0.1 as importance, "; // slightly smaller than the importance for normal houses with rank 30, which is 0
481 $sSQL .= " (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p";
482 $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,";
483 $sSQL .= " null as extra_place ";
484 $sSQL .= " from (select place_id, calculated_country_code ";
485 //interpolate the housenumbers here
486 $sSQL .= ", CASE WHEN startnumber != endnumber THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
487 $sSQL .= " ELSE ST_LineInterpolatePoint(linegeo, 0.5) END as centroid";
488 $sSQL .= ", parent_place_id, housenumber_for_place ";
489 $sSQL .= " from (location_property_osmline ";
490 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
491 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
492 $sSQL .= " group by place_id, housenumber_for_place, calculated_country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
493 if (!$this->bDeDupe) $sSQL .= ", place_id ";
495 if (CONST_Use_Aux_Location_data)
498 $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, ";
499 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress, ";
500 $sSQL .= "null as placename, ";
501 $sSQL .= "null as ref, ";
502 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
503 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
504 $sSQL .= "avg(ST_X(centroid)) as lon, avg(ST_Y(centroid)) as lat, ";
505 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
506 $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, ";
507 $sSQL .= "null as extra_place ";
508 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
509 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
510 $sSQL .= "group by place_id";
511 if (!$this->bDeDupe) $sSQL .= ", place_id";
512 $sSQL .= ", get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) ";
516 $sSQL .= " order by importance desc";
517 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
518 $aSearchResults = chksql($this->oDB->getAll($sSQL),
519 "Could not get details for place.");
521 return $aSearchResults;
524 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
527 Calculate all searches using aValidTokens i.e.
528 'Wodsworth Road, Sheffield' =>
532 0 1 (wodsworth)(road)
535 Score how good the search is so they can be ordered
537 foreach($aPhrases as $iPhrase => $sPhrase)
539 $aNewPhraseSearches = array();
540 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
541 else $sPhraseType = '';
543 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
545 // Too many permutations - too expensive
546 if ($iWordSet > 120) break;
548 $aWordsetSearches = $aSearches;
550 // Add all words from this wordset
551 foreach($aWordset as $iToken => $sToken)
553 //echo "<br><b>$sToken</b>";
554 $aNewWordsetSearches = array();
556 foreach($aWordsetSearches as $aCurrentSearch)
559 //var_dump($aCurrentSearch);
562 // If the token is valid
563 if (isset($aValidTokens[' '.$sToken]))
565 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
567 $aSearch = $aCurrentSearch;
568 $aSearch['iSearchRank']++;
569 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
571 if ($aSearch['sCountryCode'] === false)
573 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
574 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
575 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
577 $aSearch['iSearchRank'] += 5;
579 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
582 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
584 if ($aSearch['fLat'] === '')
586 $aSearch['fLat'] = $aSearchTerm['lat'];
587 $aSearch['fLon'] = $aSearchTerm['lon'];
588 $aSearch['fRadius'] = $aSearchTerm['radius'];
589 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
592 elseif ($sPhraseType == 'postalcode')
594 // 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
595 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
597 // If we already have a name try putting the postcode first
598 if (sizeof($aSearch['aName']))
600 $aNewSearch = $aSearch;
601 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
602 $aNewSearch['aName'] = array();
603 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
604 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
607 if (sizeof($aSearch['aName']))
609 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
611 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
615 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
616 $aSearch['iSearchRank'] += 1000; // skip;
621 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
622 //$aSearch['iNamePhrase'] = $iPhrase;
624 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
628 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
630 if ($aSearch['sHouseNumber'] === '')
632 $aSearch['sHouseNumber'] = $sToken;
633 // sanity check: if the housenumber is not mainly made
634 // up of numbers, add a penalty
635 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
636 // also housenumbers should appear in the first or second phrase
637 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
638 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
640 // Fall back to not searching for this item (better than nothing)
641 $aSearch = $aCurrentSearch;
642 $aSearch['iSearchRank'] += 1;
643 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
647 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
649 if ($aSearch['sClass'] === '')
651 $aSearch['sOperator'] = $aSearchTerm['operator'];
652 $aSearch['sClass'] = $aSearchTerm['class'];
653 $aSearch['sType'] = $aSearchTerm['type'];
654 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
655 else $aSearch['sOperator'] = 'near'; // near = in for the moment
656 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
658 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
661 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
663 if (sizeof($aSearch['aName']))
665 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
667 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
671 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
672 $aSearch['iSearchRank'] += 1000; // skip;
677 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
678 //$aSearch['iNamePhrase'] = $iPhrase;
680 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
684 // Look for partial matches.
685 // Note that there is no point in adding country terms here
686 // because country are omitted in the address.
687 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
689 // Allow searching for a word - but at extra cost
690 foreach($aValidTokens[$sToken] as $aSearchTerm)
692 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
694 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
696 $aSearch = $aCurrentSearch;
697 $aSearch['iSearchRank'] += 1;
698 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
700 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
701 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
703 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
705 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
706 $aSearch['iSearchRank'] += 1;
707 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
708 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
710 if (empty($aSearchTermToken['country_code'])
711 && empty($aSearchTermToken['lat'])
712 && empty($aSearchTermToken['class']))
714 $aSearch = $aCurrentSearch;
715 $aSearch['iSearchRank'] += 1;
716 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
717 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
723 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
724 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
725 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
729 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
731 $aSearch = $aCurrentSearch;
732 $aSearch['iSearchRank'] += 1;
733 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
734 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
735 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
736 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
738 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
739 $aSearch['iNamePhrase'] = $iPhrase;
740 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
747 // Allow skipping a word - but at EXTREAM cost
748 //$aSearch = $aCurrentSearch;
749 //$aSearch['iSearchRank']+=100;
750 //$aNewWordsetSearches[] = $aSearch;
754 usort($aNewWordsetSearches, 'bySearchRank');
755 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
757 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
759 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
760 usort($aNewPhraseSearches, 'bySearchRank');
762 $aSearchHash = array();
763 foreach($aNewPhraseSearches as $iSearch => $aSearch)
765 $sHash = serialize($aSearch);
766 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
767 else $aSearchHash[$sHash] = 1;
770 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
773 // Re-group the searches by their score, junk anything over 20 as just not worth trying
774 $aGroupedSearches = array();
775 foreach($aNewPhraseSearches as $aSearch)
777 if ($aSearch['iSearchRank'] < $this->iMaxRank)
779 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
780 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
783 ksort($aGroupedSearches);
786 $aSearches = array();
787 foreach($aGroupedSearches as $iScore => $aNewSearches)
789 $iSearchCount += sizeof($aNewSearches);
790 $aSearches = array_merge($aSearches, $aNewSearches);
791 if ($iSearchCount > 50) break;
794 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
797 return $aGroupedSearches;
801 /* Perform the actual query lookup.
803 Returns an ordered list of results, each with the following fields:
804 osm_type: type of corresponding OSM object
808 P - postcode (internally computed)
809 osm_id: id of corresponding OSM object
810 class: general object class (corresponds to tag key of primary OSM tag)
811 type: subclass of object (corresponds to tag value of primary OSM tag)
812 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
813 rank_search: rank in search hierarchy
814 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
815 rank_address: rank in address hierarchy (determines orer in address)
816 place_id: internal key (may differ between different instances)
817 country_code: ISO country code
818 langaddress: localized full address
819 placename: localized name of object
820 ref: content of ref tag (if available)
823 importance: importance of place based on Wikipedia link count
824 addressimportance: cumulated importance of address elements
825 extra_place: type of place (for admin boundaries, if there is a place tag)
826 aBoundingBox: bounding Box
827 label: short description of the object class/type (English only)
828 name: full name (currently the same as langaddress)
829 foundorder: secondary ordering for places with same importance
833 if (!$this->sQuery && !$this->aStructuredQuery) return false;
835 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
836 $sCountryCodesSQL = false;
837 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
839 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
842 $sQuery = $this->sQuery;
844 // Conflicts between US state abreviations and various words for 'the' in different languages
845 if (isset($this->aLangPrefOrder['name:en']))
847 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
848 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
849 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
853 $sViewboxCentreSQL = false;
854 $bBoundingBoxSearch = false;
857 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
858 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
859 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
860 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
861 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
862 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
864 $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)";
865 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
866 $bBoundingBoxSearch = $this->bBoundedSearch;
870 if ($this->aRoutePoints)
872 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
874 foreach($this->aRoutePoints as $aPoint)
876 if (!$bFirst) $sViewboxCentreSQL .= ",";
877 $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
880 $sViewboxCentreSQL .= ")'::geometry,4326)";
882 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
883 $this->sViewboxSmallSQL = chksql($this->oDB->getOne($sSQL),
884 "Could not get small viewbox.");
885 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
887 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
888 $this->sViewboxLargeSQL = chksql($this->oDB->getOne($sSQL),
889 "Could not get large viewbox.");
890 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
891 $bBoundingBoxSearch = $this->bBoundedSearch;
894 // Do we have anything that looks like a lat/lon pair?
895 if ( $aLooksLike = looksLikeLatLonPair($sQuery) )
897 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
898 $sQuery = $aLooksLike['query'];
901 $aSearchResults = array();
902 if ($sQuery || $this->aStructuredQuery)
904 // Start with a blank search
906 array('iSearchRank' => 0,
908 'sCountryCode' => false,
910 'aAddress' => array(),
911 'aFullNameAddress' => array(),
912 'aNameNonSearch' => array(),
913 'aAddressNonSearch' => array(),
915 'aFeatureName' => array(),
918 'sHouseNumber' => '',
925 // Do we have a radius search?
926 $sNearPointSQL = false;
927 if ($this->aNearPoint)
929 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
930 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
931 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
932 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
935 // Any 'special' terms in the search?
936 $bSpecialTerms = false;
937 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
938 $aSpecialTerms = array();
939 foreach($aSpecialTermsRaw as $aSpecialTerm)
941 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
942 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
945 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
946 $aSpecialTerms = array();
947 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity'])
949 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
950 unset($this->aStructuredQuery['amenity']);
952 foreach($aSpecialTermsRaw as $aSpecialTerm)
954 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
955 $sToken = chksql($this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string"));
956 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
957 $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';
958 if (CONST_Debug) var_Dump($sSQL);
959 $aSearchWords = chksql($this->oDB->getAll($sSQL));
960 $aNewSearches = array();
961 foreach($aSearches as $aSearch)
963 foreach($aSearchWords as $aSearchTerm)
965 $aNewSearch = $aSearch;
966 if ($aSearchTerm['country_code'])
968 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
969 $aNewSearches[] = $aNewSearch;
970 $bSpecialTerms = true;
972 if ($aSearchTerm['class'])
974 $aNewSearch['sClass'] = $aSearchTerm['class'];
975 $aNewSearch['sType'] = $aSearchTerm['type'];
976 $aNewSearches[] = $aNewSearch;
977 $bSpecialTerms = true;
981 $aSearches = $aNewSearches;
984 // Split query into phrases
985 // Commas are used to reduce the search space by indicating where phrases split
986 if ($this->aStructuredQuery)
988 $aPhrases = $this->aStructuredQuery;
989 $bStructuredPhrases = true;
993 $aPhrases = explode(',',$sQuery);
994 $bStructuredPhrases = false;
997 // Convert each phrase to standard form
998 // Create a list of standard words
999 // Get all 'sets' of words
1000 // Generate a complete list of all
1002 foreach($aPhrases as $iPhrase => $sPhrase)
1004 $aPhrase = chksql($this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string"),
1005 "Cannot nomralize query string (is it an UTF-8 string?)");
1006 if (trim($aPhrase['string']))
1008 $aPhrases[$iPhrase] = $aPhrase;
1009 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
1010 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
1011 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
1015 unset($aPhrases[$iPhrase]);
1019 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
1020 $aPhraseTypes = array_keys($aPhrases);
1021 $aPhrases = array_values($aPhrases);
1023 if (sizeof($aTokens))
1025 // Check which tokens we have, get the ID numbers
1026 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
1027 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
1029 if (CONST_Debug) var_Dump($sSQL);
1031 $aValidTokens = array();
1032 if (sizeof($aTokens))
1034 $aDatabaseWords = chksql($this->oDB->getAll($sSQL),
1035 "Could not get word tokens.");
1039 $aDatabaseWords = array();
1041 $aPossibleMainWordIDs = array();
1042 $aWordFrequencyScores = array();
1043 foreach($aDatabaseWords as $aToken)
1045 // Very special case - require 2 letter country param to match the country code found
1046 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
1047 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code'])
1052 if (isset($aValidTokens[$aToken['word_token']]))
1054 $aValidTokens[$aToken['word_token']][] = $aToken;
1058 $aValidTokens[$aToken['word_token']] = array($aToken);
1060 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1061 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1063 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1065 // Try and calculate GB postcodes we might be missing
1066 foreach($aTokens as $sToken)
1068 // Source of gb postcodes is now definitive - always use
1069 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
1071 if (substr($aData[1],-2,1) != ' ')
1073 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1074 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1076 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1077 if ($aGBPostcodeLocation)
1079 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1082 // US ZIP+4 codes - if there is no token,
1083 // merge in the 5-digit ZIP code
1084 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1086 if (isset($aValidTokens[$aData[1]]))
1088 foreach($aValidTokens[$aData[1]] as $aToken)
1090 if (!$aToken['class'])
1092 if (isset($aValidTokens[$sToken]))
1094 $aValidTokens[$sToken][] = $aToken;
1098 $aValidTokens[$sToken] = array($aToken);
1106 foreach($aTokens as $sToken)
1108 // Unknown single word token with a number - assume it is a house number
1109 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1111 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1115 // Any words that have failed completely?
1116 // TODO: suggestions
1118 // Start the search process
1119 // array with: placeid => -1 | tiger-housenumber
1120 $aResultPlaceIDs = array();
1122 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1124 if ($this->bReverseInPlan)
1126 // Reverse phrase array and also reverse the order of the wordsets in
1127 // the first and final phrase. Don't bother about phrases in the middle
1128 // because order in the address doesn't matter.
1129 $aPhrases = array_reverse($aPhrases);
1130 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1131 if (sizeof($aPhrases) > 1)
1133 $aFinalPhrase = end($aPhrases);
1134 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1136 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1138 foreach($aGroupedSearches as $aSearches)
1140 foreach($aSearches as $aSearch)
1142 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1144 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1145 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1151 $aGroupedSearches = $aReverseGroupedSearches;
1152 ksort($aGroupedSearches);
1157 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1158 $aGroupedSearches = array();
1159 foreach($aSearches as $aSearch)
1161 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1163 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1164 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1167 ksort($aGroupedSearches);
1170 if (CONST_Debug) var_Dump($aGroupedSearches);
1172 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0)
1174 $aCopyGroupedSearches = $aGroupedSearches;
1175 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1177 foreach($aSearches as $iSearch => $aSearch)
1179 $aReductionsList = array($aSearch['aAddress']);
1180 $iSearchRank = $aSearch['iSearchRank'];
1181 while(sizeof($aReductionsList) > 0)
1184 if ($iSearchRank > iMaxRank) break 3;
1185 $aNewReductionsList = array();
1186 foreach($aReductionsList as $aReductionsWordList)
1188 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1190 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1191 $aReverseSearch = $aSearch;
1192 $aSearch['aAddress'] = $aReductionsWordListResult;
1193 $aSearch['iSearchRank'] = $iSearchRank;
1194 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1195 if (sizeof($aReductionsWordListResult) > 0)
1197 $aNewReductionsList[] = $aReductionsWordListResult;
1201 $aReductionsList = $aNewReductionsList;
1205 ksort($aGroupedSearches);
1208 // Filter out duplicate searches
1209 $aSearchHash = array();
1210 foreach($aGroupedSearches as $iGroup => $aSearches)
1212 foreach($aSearches as $iSearch => $aSearch)
1214 $sHash = serialize($aSearch);
1215 if (isset($aSearchHash[$sHash]))
1217 unset($aGroupedSearches[$iGroup][$iSearch]);
1218 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1222 $aSearchHash[$sHash] = 1;
1227 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1231 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1234 foreach($aSearches as $aSearch)
1237 $searchedHousenumber = -1;
1239 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1240 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1242 // No location term?
1243 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1245 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1247 // Just looking for a country by code - look it up
1248 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1250 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1251 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1252 if ($bBoundingBoxSearch)
1253 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1254 $sSQL .= " order by st_area(geometry) desc limit 1";
1255 if (CONST_Debug) var_dump($sSQL);
1256 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1260 $aPlaceIDs = array();
1265 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1266 if (!$aSearch['sClass']) continue;
1267 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1268 if (chksql($this->oDB->getOne($sSQL)))
1270 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1271 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1272 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1273 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1274 if (sizeof($this->aExcludePlaceIDs))
1276 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1278 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1279 $sSQL .= " limit $this->iLimit";
1280 if (CONST_Debug) var_dump($sSQL);
1281 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1283 // If excluded place IDs are given, it is fair to assume that
1284 // there have been results in the small box, so no further
1285 // expansion in that case.
1286 // Also don't expand if bounded results were requested.
1287 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1289 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1290 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1291 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1292 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1293 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1294 $sSQL .= " limit $this->iLimit";
1295 if (CONST_Debug) var_dump($sSQL);
1296 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1301 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1302 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1303 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1304 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1305 $sSQL .= " limit $this->iLimit";
1306 if (CONST_Debug) var_dump($sSQL);
1307 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1311 // If a coordinate is given, the search must either
1312 // be for a name or a special search. Ignore everythin else.
1313 else if ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass'])
1315 $aPlaceIDs = array();
1319 $aPlaceIDs = array();
1321 // First we need a position, either aName or fLat or both
1325 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1327 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1329 $aOrder[0] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
1330 $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
1331 // also housenumbers from interpolation lines table are needed
1332 $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
1333 $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
1334 $aOrder[0] .= " desc";
1337 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1338 // they might be right - but they are just too darned expensive to run
1339 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1340 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1341 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1343 // For infrequent name terms disable index usage for address
1344 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1345 sizeof($aSearch['aName']) == 1 &&
1346 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1348 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1352 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1353 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1356 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1357 if ($aSearch['sHouseNumber'])
1359 $aTerms[] = "address_rank between 16 and 27";
1363 if ($this->iMinAddressRank > 0)
1365 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1367 if ($this->iMaxAddressRank < 30)
1369 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1372 if ($aSearch['fLon'] && $aSearch['fLat'])
1374 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1375 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1377 if (sizeof($this->aExcludePlaceIDs))
1379 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1381 if ($sCountryCodesSQL)
1383 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1386 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1387 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1389 if ($aSearch['sHouseNumber'])
1391 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1395 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1397 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1398 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1400 $aOrder[] = "$sImportanceSQL DESC";
1401 if (sizeof($aSearch['aFullNameAddress']))
1403 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1404 $aOrder[] = 'exactmatch DESC';
1406 $sExactMatchSQL = '0::int as exactmatch';
1409 if (sizeof($aTerms))
1411 $sSQL = "select place_id, ";
1412 $sSQL .= $sExactMatchSQL;
1413 $sSQL .= " from search_name";
1414 $sSQL .= " where ".join(' and ',$aTerms);
1415 $sSQL .= " order by ".join(', ',$aOrder);
1416 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1417 $sSQL .= " limit 20";
1418 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1419 $sSQL .= " limit 1";
1421 $sSQL .= " limit ".$this->iLimit;
1423 if (CONST_Debug) { var_dump($sSQL); }
1424 $aViewBoxPlaceIDs = chksql($this->oDB->getAll($sSQL),
1425 "Could not get places for search terms.");
1426 //var_dump($aViewBoxPlaceIDs);
1427 // Did we have an viewbox matches?
1428 $aPlaceIDs = array();
1429 $bViewBoxMatch = false;
1430 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1432 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1433 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1434 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1435 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1436 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1437 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1440 //var_Dump($aPlaceIDs);
1443 //now search for housenumber, if housenumber provided
1444 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1446 $searchedHousenumber = intval($aSearch['sHouseNumber']);
1447 $aRoadPlaceIDs = $aPlaceIDs;
1448 $sPlaceIDs = join(',',$aPlaceIDs);
1450 // Now they are indexed, look for a house attached to a street we found
1451 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1452 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1453 if (sizeof($this->aExcludePlaceIDs))
1455 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1457 $sSQL .= " limit $this->iLimit";
1458 if (CONST_Debug) var_dump($sSQL);
1459 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1461 // if nothing found, search in the interpolation line table
1462 if(!sizeof($aPlaceIDs))
1464 // do we need to use transliteration and the regex for housenumbers???
1465 //new query for lines, not housenumbers anymore
1466 if($searchedHousenumber%2 == 0){
1467 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1468 $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";
1470 //look for housenumber in streets with interpolationtype odd or all
1471 $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";
1474 if (sizeof($this->aExcludePlaceIDs))
1476 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1478 //$sSQL .= " limit $this->iLimit";
1479 if (CONST_Debug) var_dump($sSQL);
1481 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1484 // If nothing found try the aux fallback table
1485 if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs))
1487 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1488 if (sizeof($this->aExcludePlaceIDs))
1490 $sSQL .= " and parent_place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1492 //$sSQL .= " limit $this->iLimit";
1493 if (CONST_Debug) var_dump($sSQL);
1494 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1497 //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1498 if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs))
1500 //new query for lines, not housenumbers anymore
1501 if($searchedHousenumber%2 == 0){
1502 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1503 $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";
1505 //look for housenumber in streets with interpolationtype odd or all
1506 $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";
1509 if (sizeof($this->aExcludePlaceIDs))
1511 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1513 //$sSQL .= " limit $this->iLimit";
1514 if (CONST_Debug) var_dump($sSQL);
1516 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1519 // Fallback to the road (if no housenumber was found)
1520 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1522 $aPlaceIDs = $aRoadPlaceIDs;
1523 //set to -1, if no housenumbers were found
1524 $searchedHousenumber = -1;
1526 //else: housenumber was found, remains saved in searchedHousenumber
1530 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1532 $sPlaceIDs = join(',', $aPlaceIDs);
1533 $aClassPlaceIDs = array();
1535 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1537 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1538 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1539 $sSQL .= " and linked_place_id is null";
1540 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1541 $sSQL .= " order by rank_search asc limit $this->iLimit";
1542 if (CONST_Debug) var_dump($sSQL);
1543 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
1546 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1548 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1549 $bCacheTable = chksql($this->oDB->getOne($sSQL));
1551 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1553 if (CONST_Debug) var_dump($sSQL);
1554 $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
1556 // For state / country level searches the normal radius search doesn't work very well
1557 $sPlaceGeom = false;
1558 if ($this->iMaxRank < 9 && $bCacheTable)
1560 // Try and get a polygon to search in instead
1561 $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";
1562 if (CONST_Debug) var_dump($sSQL);
1563 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
1572 $this->iMaxRank += 5;
1573 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1574 if (CONST_Debug) var_dump($sSQL);
1575 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1576 $sPlaceIDs = join(',',$aPlaceIDs);
1579 if ($sPlaceIDs || $sPlaceGeom)
1585 // More efficient - can make the range bigger
1589 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1590 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1591 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1593 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1594 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1597 $sSQL .= ",placex as f where ";
1598 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1603 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1605 if (sizeof($this->aExcludePlaceIDs))
1607 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1609 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1610 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1611 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1612 $sSQL .= " limit $this->iLimit";
1613 if (CONST_Debug) var_dump($sSQL);
1614 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1618 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1621 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1622 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1624 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1625 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1626 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1627 if (sizeof($this->aExcludePlaceIDs))
1629 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1631 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1632 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1633 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1634 $sSQL .= " limit $this->iLimit";
1635 if (CONST_Debug) var_dump($sSQL);
1636 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1641 $aPlaceIDs = $aClassPlaceIDs;
1647 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1649 foreach($aPlaceIDs as $iPlaceID)
1651 // array for placeID => -1 | Tiger housenumber
1652 $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1654 if ($iQueryLoop > 20) break;
1657 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1659 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1660 // reduces the number of place ids, like a filter
1661 // rank_address is 30 for interpolated housenumbers
1662 $sSQL = "select place_id from placex where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1663 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1664 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1665 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1666 if (CONST_Use_US_Tiger_Data)
1668 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
1669 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1670 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1672 $sSQL .= ") UNION select place_id from location_property_osmline where place_id in (".join(',',array_keys($aResultPlaceIDs)).")";
1673 $sSQL .= " and (30 between $this->iMinAddressRank and $this->iMaxAddressRank)";
1674 if (CONST_Debug) var_dump($sSQL);
1675 $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1677 foreach($aFilteredPlaceIDs as $placeID)
1679 $tempIDs[$placeID] = $aResultPlaceIDs[$placeID]; //assign housenumber to placeID
1681 $aResultPlaceIDs = $tempIDs;
1685 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1686 if ($iGroupLoop > 4) break;
1687 if ($iQueryLoop > 30) break;
1690 // Did we find anything?
1691 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1693 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1699 // Just interpret as a reverse geocode
1700 $oReverse = new ReverseGeocode($this->oDB);
1701 $oReverse->setZoom(18);
1703 $aLookup = $oReverse->lookup((float)$this->aNearPoint[0],
1704 (float)$this->aNearPoint[1],
1707 if (CONST_Debug) var_dump("Reverse search", $aLookup);
1709 if ($aLookup['place_id'])
1710 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
1712 $aSearchResults = array();
1716 if (!sizeof($aSearchResults))
1718 if ($this->bFallback)
1720 if ($this->fallbackStructuredQuery())
1722 return $this->lookup();
1729 $aClassType = getClassTypesWithImportance();
1730 $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1731 foreach($aRecheckWords as $i => $sWord)
1733 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1736 if (CONST_Debug) { echo '<i>Recheck words:<\i>'; var_dump($aRecheckWords); }
1738 foreach($aSearchResults as $iResNum => $aResult)
1741 $fDiameter = getResultDiameter($aResult);
1743 $oPlaceLookup = new PlaceLookup($this->oDB);
1744 $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints);
1745 $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText);
1746 $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON);
1747 $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML);
1748 $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG);
1749 $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold);
1751 $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2);
1752 if ($aOutlineResult)
1754 $aResult = array_merge($aResult, $aOutlineResult);
1757 if ($aResult['extra_place'] == 'city')
1759 $aResult['class'] = 'place';
1760 $aResult['type'] = 'city';
1761 $aResult['rank_search'] = 16;
1764 // Is there an icon set for this type of result?
1765 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1766 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1768 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1771 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1772 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1774 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1776 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1777 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1779 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1781 // if tag '&addressdetails=1' is set in query
1782 if ($this->bIncludeAddressDetails)
1784 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1785 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1786 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1788 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1791 if ($this->bIncludeExtraTags)
1793 if ($aResult['extra'])
1795 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1799 $aResult['sExtraTags'] = (object) array();
1803 if ($this->bIncludeNameDetails)
1805 if ($aResult['names'])
1807 $aResult['sNameDetails'] = json_decode($aResult['names']);
1811 $aResult['sNameDetails'] = (object) array();
1815 // Adjust importance for the number of exact string matches in the result
1816 $aResult['importance'] = max(0.001,$aResult['importance']);
1818 $sAddress = $aResult['langaddress'];
1819 foreach($aRecheckWords as $i => $sWord)
1821 if (stripos($sAddress, $sWord)!==false)
1824 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1828 $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
1830 $aResult['name'] = $aResult['langaddress'];
1831 // secondary ordering (for results with same importance (the smaller the better):
1832 // - approximate importance of address parts
1833 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1834 // - number of exact matches from the query
1835 if (isset($this->exactMatchCache[$aResult['place_id']]))
1836 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1837 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1838 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1839 // - importance of the class/type
1840 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1841 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1843 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1847 $aResult['foundorder'] += 0.01;
1849 if (CONST_Debug) { var_dump($aResult); }
1850 $aSearchResults[$iResNum] = $aResult;
1852 uasort($aSearchResults, 'byImportance');
1854 $aOSMIDDone = array();
1855 $aClassTypeNameDone = array();
1856 $aToFilter = $aSearchResults;
1857 $aSearchResults = array();
1860 foreach($aToFilter as $iResNum => $aResult)
1862 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1865 $fLat = $aResult['lat'];
1866 $fLon = $aResult['lon'];
1867 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1870 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1871 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1873 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1874 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1875 $aSearchResults[] = $aResult;
1878 // Absolute limit on number of results
1879 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1882 return $aSearchResults;