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 $sViewboxCentreSQL = false;
37 protected $sViewboxSmallSQL = false;
38 protected $sViewboxLargeSQL = 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;
52 function Geocode(&$oDB)
57 function setReverseInPlan($bReverse)
59 $this->bReverseInPlan = $bReverse;
62 function setLanguagePreference($aLangPref)
64 $this->aLangPrefOrder = $aLangPref;
67 function getIncludeAddressDetails()
69 return $this->bIncludeAddressDetails;
72 function getIncludeExtraTags()
74 return $this->bIncludeExtraTags;
77 function getIncludeNameDetails()
79 return $this->bIncludeNameDetails;
82 function setIncludePolygonAsPoints($b = true)
84 $this->bIncludePolygonAsPoints = $b;
87 function setIncludePolygonAsText($b = true)
89 $this->bIncludePolygonAsText = $b;
92 function setIncludePolygonAsGeoJSON($b = true)
94 $this->bIncludePolygonAsGeoJSON = $b;
97 function setIncludePolygonAsKML($b = true)
99 $this->bIncludePolygonAsKML = $b;
102 function setIncludePolygonAsSVG($b = true)
104 $this->bIncludePolygonAsSVG = $b;
107 function setPolygonSimplificationThreshold($f)
109 $this->fPolygonSimplificationThreshold = $f;
112 function setLimit($iLimit = 10)
114 if ($iLimit > 50) $iLimit = 50;
115 if ($iLimit < 1) $iLimit = 1;
117 $this->iFinalLimit = $iLimit;
118 $this->iLimit = $iLimit + min($iLimit, 10);
121 function getExcludedPlaceIDs()
123 return $this->aExcludePlaceIDs;
126 function getViewBoxString()
128 if (!$this->aViewBox) return null;
129 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
132 function setFeatureType($sFeatureType)
134 switch ($sFeatureType) {
136 $this->setRankRange(4, 4);
139 $this->setRankRange(8, 8);
142 $this->setRankRange(14, 16);
145 $this->setRankRange(8, 20);
150 function setRankRange($iMin, $iMax)
152 $this->iMinAddressRank = $iMin;
153 $this->iMaxAddressRank = $iMax;
156 function setRoute($aRoutePoints, $fRouteWidth)
158 $this->aViewBox = false;
160 $this->sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
162 foreach ($this->aRoutePoints as $aPoint) {
163 $fPoint = (float)$aPoint;
164 $this->sViewboxCentreSQL .= $sSep.$fPoint;
165 $sSep = ($sSep == ' ') ? ',' : ' ';
167 $this->sViewboxCentreSQL .= ")'::geometry,4326)";
169 $this->sViewboxSmallSQL = 'st_buffer('.$this->sViewboxCentreSQL;
170 $this->sViewboxSmallSQL .= ','.($fRouteWidth/69).')';
172 $this->sViewboxLargeSQL = 'st_buffer('.$this->sViewboxCentreSQL;
173 $this->sViewboxLargeSQL .= ','.($fRouteWidth/30).')';
176 function setViewbox($aViewbox)
178 $this->aViewBox = array_map('floatval', $aViewbox);
180 $fHeight = $this->aViewBox[0] - $this->aViewBox[2];
181 $fWidth = $this->aViewBox[1] - $this->aViewBox[3];
182 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
183 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
184 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
185 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
187 $this->sViewboxCentreSQL = false;
188 $this->sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".$this->aViewBox[0].",".$this->aViewBox[1]."),ST_Point(".$this->aViewBox[2].",".$this->aViewBox[3].")),4326)";
189 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".$aBigViewBox[0].",".$aBigViewBox[1]."),ST_Point(".$aBigViewBox[2].",".$aBigViewBox[3].")),4326)";
192 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
194 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
197 function setQuery($sQueryString)
199 $this->sQuery = $sQueryString;
200 $this->aStructuredQuery = false;
203 function getQueryString()
205 return $this->sQuery;
209 function loadParamArray($oParams)
211 $this->bIncludeAddressDetails
212 = $oParams->getBool('addressdetails', $this->bIncludeAddressDetails);
213 $this->bIncludeExtraTags
214 = $oParams->getBool('extratags', $this->bIncludeExtraTags);
215 $this->bIncludeNameDetails
216 = $oParams->getBool('namedetails', $this->bIncludeNameDetails);
218 $this->bBoundedSearch = $oParams->getBool('bounded', $this->bBoundedSearch);
219 $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
221 $this->setLimit($oParams->getInt('limit', $this->iFinalLimit));
222 $this->iOffset = $oParams->getInt('offset', $this->iOffset);
224 $this->bFallback = $oParams->getBool('fallback', $this->bFallback);
226 // List of excluded Place IDs - used for more acurate pageing
227 $sExcluded = $oParams->getStringList('exclude_place_ids');
229 foreach ($sExcluded as $iExcludedPlaceID) {
230 $iExcludedPlaceID = (int)$iExcludedPlaceID;
231 if ($iExcludedPlaceID)
232 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
235 if (isset($aExcludePlaceIDs))
236 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
239 // Only certain ranks of feature
240 $sFeatureType = $oParams->getString('featureType');
241 if (!$sFeatureType) $sFeatureType = $oParams->getString('featuretype');
242 if ($sFeatureType) $this->setFeatureType($sFeatureType);
245 $sCountries = $oParams->getStringList('countrycodes');
247 foreach ($sCountries as $sCountryCode) {
248 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode)) {
249 $aCountries[] = strtolower($sCountryCode);
252 if (isset($aCountryCodes))
253 $this->aCountryCodes = $aCountries;
256 $aViewbox = $oParams->getStringList('viewboxlbrt');
258 $this->setViewbox($aViewbox);
260 $aViewbox = $oParams->getStringList('viewbox');
262 $this->setViewBox(array(
269 $aRoute = $oParams->getStringList('route');
270 $fRouteWidth = $oParams->getFloat('routewidth');
271 if ($aRoute && $fRouteWidth) {
272 $this->setRoute($aRoute, $fRouteWidth);
278 function setQueryFromParams($oParams)
281 $sQuery = $oParams->getString('q');
283 $this->setStructuredQuery(
284 $oParams->getString('amenity'),
285 $oParams->getString('street'),
286 $oParams->getString('city'),
287 $oParams->getString('county'),
288 $oParams->getString('state'),
289 $oParams->getString('country'),
290 $oParams->getString('postalcode')
292 $this->setReverseInPlan(false);
294 $this->setQuery($sQuery);
298 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
300 $sValue = trim($sValue);
301 if (!$sValue) return false;
302 $this->aStructuredQuery[$sKey] = $sValue;
303 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30) {
304 $this->iMinAddressRank = $iNewMinAddressRank;
305 $this->iMaxAddressRank = $iNewMaxAddressRank;
307 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
311 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
313 $this->sQuery = false;
316 $this->iMinAddressRank = 0;
317 $this->iMaxAddressRank = 30;
318 $this->aAddressRankList = array();
320 $this->aStructuredQuery = array();
321 $this->sAllowedTypesSQLList = '';
323 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
324 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
325 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
326 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
327 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
328 $this->loadStructuredAddressElement($sPostalCode, 'postalcode', 5, 11, array(5, 11));
329 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
331 if (sizeof($this->aStructuredQuery) > 0) {
332 $this->sQuery = join(', ', $this->aStructuredQuery);
333 if ($this->iMaxAddressRank < 30) {
334 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
339 function fallbackStructuredQuery()
341 if (!$this->aStructuredQuery) return false;
343 $aParams = $this->aStructuredQuery;
345 if (sizeof($aParams) == 1) return false;
347 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
349 foreach ($aOrderToFallback as $sType) {
350 if (isset($aParams[$sType])) {
351 unset($aParams[$sType]);
352 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
360 function getDetails($aPlaceIDs)
362 //$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
363 if (sizeof($aPlaceIDs) == 0) return array();
365 $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
367 // Get the details for display (is this a redundant extra step?)
368 $sPlaceIDs = join(',', array_keys($aPlaceIDs));
370 $sImportanceSQL = '';
371 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
372 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
374 $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,";
375 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress,";
376 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
377 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
378 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
379 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
380 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
381 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
382 $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, ";
383 $sSQL .= "(extratags->'place') as extra_place ";
384 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
385 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
386 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
387 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
389 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
390 $sSQL .= "and linked_place_id is null ";
391 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
392 if (!$this->bDeDupe) $sSQL .= ",place_id";
393 $sSQL .= ",langaddress ";
394 $sSQL .= ",placename ";
396 if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
397 if ($this->bIncludeNameDetails) $sSQL .= ",name";
398 $sSQL .= ",extratags->'place' ";
400 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank) {
401 // only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines
402 // with start- and endnumber, the common osm housenumbers are usually saved as points
405 $length = count($aPlaceIDs);
406 foreach ($aPlaceIDs as $placeID => $housenumber) {
408 $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
409 if ($i<$length) $sHousenumbers .= ", ";
411 if (CONST_Use_US_Tiger_Data) {
412 // Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
414 $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";
415 $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
416 $sSQL .= ", null as placename";
417 $sSQL .= ", null as ref";
418 if ($this->bIncludeExtraTags) $sSQL .= ", null as extra";
419 if ($this->bIncludeNameDetails) $sSQL .= ", null as names";
420 $sSQL .= ", avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
421 $sSQL .= $sImportanceSQL."-1.15 as importance ";
422 $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 ";
423 $sSQL .= ", null as extra_place ";
424 $sSQL .= " from (select place_id";
425 // interpolate the Tiger housenumbers here
426 $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as centroid, parent_place_id, housenumber_for_place";
427 $sSQL .= " from (location_property_tiger ";
428 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
429 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
430 $sSQL .= " group by place_id, housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
431 if (!$this->bDeDupe) $sSQL .= ", place_id ";
434 // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
436 $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, ";
437 $sSQL .= "get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress, ";
438 $sSQL .= "null as placename, ";
439 $sSQL .= "null as ref, ";
440 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
441 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
442 $sSQL .= " avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
443 $sSQL .= $sImportanceSQL."-0.1 as importance, "; // slightly smaller than the importance for normal houses with rank 30, which is 0
444 $sSQL .= " (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p";
445 $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,";
446 $sSQL .= " null as extra_place ";
447 $sSQL .= " from (select place_id, calculated_country_code ";
448 // interpolate the housenumbers here
449 $sSQL .= ", CASE WHEN startnumber != endnumber THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
450 $sSQL .= " ELSE ST_LineInterpolatePoint(linegeo, 0.5) END as centroid";
451 $sSQL .= ", parent_place_id, housenumber_for_place ";
452 $sSQL .= " from (location_property_osmline ";
453 $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
454 $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
455 $sSQL .= " group by place_id, housenumber_for_place, calculated_country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
456 if (!$this->bDeDupe) $sSQL .= ", place_id ";
458 if (CONST_Use_Aux_Location_data) {
460 $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, ";
461 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress, ";
462 $sSQL .= "null as placename, ";
463 $sSQL .= "null as ref, ";
464 if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
465 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
466 $sSQL .= "avg(ST_X(centroid)) as lon, avg(ST_Y(centroid)) as lat, ";
467 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
468 $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, ";
469 $sSQL .= "null as extra_place ";
470 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
471 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
472 $sSQL .= "group by place_id";
473 if (!$this->bDeDupe) $sSQL .= ", place_id";
474 $sSQL .= ", get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) ";
478 $sSQL .= " order by importance desc";
483 $aSearchResults = chksql(
484 $this->oDB->getAll($sSQL),
485 "Could not get details for place."
488 return $aSearchResults;
491 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
494 Calculate all searches using aValidTokens i.e.
495 'Wodsworth Road, Sheffield' =>
499 0 1 (wodsworth)(road)
502 Score how good the search is so they can be ordered
504 foreach ($aPhrases as $iPhrase => $sPhrase) {
505 $aNewPhraseSearches = array();
506 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
507 else $sPhraseType = '';
509 foreach ($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset) {
510 // Too many permutations - too expensive
511 if ($iWordSet > 120) break;
513 $aWordsetSearches = $aSearches;
515 // Add all words from this wordset
516 foreach ($aWordset as $iToken => $sToken) {
517 //echo "<br><b>$sToken</b>";
518 $aNewWordsetSearches = array();
520 foreach ($aWordsetSearches as $aCurrentSearch) {
522 //var_dump($aCurrentSearch);
525 // If the token is valid
526 if (isset($aValidTokens[' '.$sToken])) {
527 foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
528 $aSearch = $aCurrentSearch;
529 $aSearch['iSearchRank']++;
530 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0') {
531 if ($aSearch['sCountryCode'] === false) {
532 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
533 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
534 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases))) {
535 $aSearch['iSearchRank'] += 5;
537 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
539 } elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null) {
540 if ($aSearch['fLat'] === '') {
541 $aSearch['fLat'] = $aSearchTerm['lat'];
542 $aSearch['fLon'] = $aSearchTerm['lon'];
543 $aSearch['fRadius'] = $aSearchTerm['radius'];
544 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
546 } elseif ($sPhraseType == 'postalcode') {
547 // 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
548 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
549 // If we already have a name try putting the postcode first
550 if (sizeof($aSearch['aName'])) {
551 $aNewSearch = $aSearch;
552 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
553 $aNewSearch['aName'] = array();
554 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
555 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
558 if (sizeof($aSearch['aName'])) {
559 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
560 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
562 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
563 $aSearch['iSearchRank'] += 1000; // skip;
566 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
567 //$aSearch['iNamePhrase'] = $iPhrase;
569 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
571 } elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house') {
572 if ($aSearch['sHouseNumber'] === '') {
573 $aSearch['sHouseNumber'] = $sToken;
574 // sanity check: if the housenumber is not mainly made
575 // up of numbers, add a penalty
576 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
577 // also housenumbers should appear in the first or second phrase
578 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
579 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
581 // Fall back to not searching for this item (better than nothing)
582 $aSearch = $aCurrentSearch;
583 $aSearch['iSearchRank'] += 1;
584 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
587 } elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null) {
588 if ($aSearch['sClass'] === '') {
589 $aSearch['sOperator'] = $aSearchTerm['operator'];
590 $aSearch['sClass'] = $aSearchTerm['class'];
591 $aSearch['sType'] = $aSearchTerm['type'];
592 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
593 else $aSearch['sOperator'] = 'near'; // near = in for the moment
594 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
596 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
598 } elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
599 if (sizeof($aSearch['aName'])) {
600 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
601 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
603 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
604 $aSearch['iSearchRank'] += 1000; // skip;
607 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
608 //$aSearch['iNamePhrase'] = $iPhrase;
610 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
614 // Look for partial matches.
615 // Note that there is no point in adding country terms here
616 // because country are omitted in the address.
617 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country') {
618 // Allow searching for a word - but at extra cost
619 foreach ($aValidTokens[$sToken] as $aSearchTerm) {
620 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
621 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false) {
622 $aSearch = $aCurrentSearch;
623 $aSearch['iSearchRank'] += 1;
624 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
625 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
626 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
627 } elseif (isset($aValidTokens[' '.$sToken])) { // revert to the token version?
628 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
629 $aSearch['iSearchRank'] += 1;
630 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
631 foreach ($aValidTokens[' '.$sToken] as $aSearchTermToken) {
632 if (empty($aSearchTermToken['country_code'])
633 && empty($aSearchTermToken['lat'])
634 && empty($aSearchTermToken['class'])
636 $aSearch = $aCurrentSearch;
637 $aSearch['iSearchRank'] += 1;
638 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
639 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
643 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
644 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
645 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
649 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase) {
650 $aSearch = $aCurrentSearch;
651 $aSearch['iSearchRank'] += 1;
652 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
653 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
654 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
655 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
657 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
659 $aSearch['iNamePhrase'] = $iPhrase;
660 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
665 // Allow skipping a word - but at EXTREAM cost
666 //$aSearch = $aCurrentSearch;
667 //$aSearch['iSearchRank']+=100;
668 //$aNewWordsetSearches[] = $aSearch;
672 usort($aNewWordsetSearches, 'bySearchRank');
673 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
675 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
677 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
678 usort($aNewPhraseSearches, 'bySearchRank');
680 $aSearchHash = array();
681 foreach ($aNewPhraseSearches as $iSearch => $aSearch) {
682 $sHash = serialize($aSearch);
683 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
684 else $aSearchHash[$sHash] = 1;
687 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
690 // Re-group the searches by their score, junk anything over 20 as just not worth trying
691 $aGroupedSearches = array();
692 foreach ($aNewPhraseSearches as $aSearch) {
693 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
694 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
695 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
698 ksort($aGroupedSearches);
701 $aSearches = array();
702 foreach ($aGroupedSearches as $iScore => $aNewSearches) {
703 $iSearchCount += sizeof($aNewSearches);
704 $aSearches = array_merge($aSearches, $aNewSearches);
705 if ($iSearchCount > 50) break;
708 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
710 return $aGroupedSearches;
713 /* Perform the actual query lookup.
715 Returns an ordered list of results, each with the following fields:
716 osm_type: type of corresponding OSM object
720 P - postcode (internally computed)
721 osm_id: id of corresponding OSM object
722 class: general object class (corresponds to tag key of primary OSM tag)
723 type: subclass of object (corresponds to tag value of primary OSM tag)
724 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
725 rank_search: rank in search hierarchy
726 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
727 rank_address: rank in address hierarchy (determines orer in address)
728 place_id: internal key (may differ between different instances)
729 country_code: ISO country code
730 langaddress: localized full address
731 placename: localized name of object
732 ref: content of ref tag (if available)
735 importance: importance of place based on Wikipedia link count
736 addressimportance: cumulated importance of address elements
737 extra_place: type of place (for admin boundaries, if there is a place tag)
738 aBoundingBox: bounding Box
739 label: short description of the object class/type (English only)
740 name: full name (currently the same as langaddress)
741 foundorder: secondary ordering for places with same importance
747 if (!$this->sQuery && !$this->aStructuredQuery) return false;
749 $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
750 $sCountryCodesSQL = false;
751 if ($this->aCountryCodes) {
752 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
755 $sQuery = $this->sQuery;
757 // Conflicts between US state abreviations and various words for 'the' in different languages
758 if (isset($this->aLangPrefOrder['name:en'])) {
759 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/', '\1illinois\2', $sQuery);
760 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/', '\1alabama\2', $sQuery);
761 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/', '\1louisiana\2', $sQuery);
764 $bBoundingBoxSearch = $this->bBoundedSearch && $this->sViewboxSmallSQL;
765 if ($this->sViewboxCentreSQL) {
766 // For complex viewboxes (routes) precompute the bounding geometry
768 $this->oDB->getOne("select ".$this->sViewboxSmallSQL),
769 "Could not get small viewbox"
771 $this->sViewboxSmallSQL = "'".$sGeom."'::geometry";
774 $this->oDB->getOne("select ".$this->sViewboxLargeSQL),
775 "Could not get large viewbox"
777 $this->sViewboxLargeSQL = "'".$sGeom."'::geometry";
780 // Do we have anything that looks like a lat/lon pair?
781 if ($aLooksLike = looksLikeLatLonPair($sQuery)) {
782 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
783 $sQuery = $aLooksLike['query'];
786 $aSearchResults = array();
787 if ($sQuery || $this->aStructuredQuery) {
788 // Start with a blank search
793 'sCountryCode' => false,
795 'aAddress' => array(),
796 'aFullNameAddress' => array(),
797 'aNameNonSearch' => array(),
798 'aAddressNonSearch' => array(),
800 'aFeatureName' => array(),
803 'sHouseNumber' => '',
810 // Do we have a radius search?
811 $sNearPointSQL = false;
812 if ($this->aNearPoint) {
813 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
814 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
815 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
816 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
819 // Any 'special' terms in the search?
820 $bSpecialTerms = false;
821 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
822 $aSpecialTerms = array();
823 foreach ($aSpecialTermsRaw as $aSpecialTerm) {
824 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
825 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
828 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
829 $aSpecialTerms = array();
830 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity']) {
831 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
832 unset($this->aStructuredQuery['amenity']);
835 foreach ($aSpecialTermsRaw as $aSpecialTerm) {
836 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
837 $sToken = chksql($this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string"));
838 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
839 $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';
840 if (CONST_Debug) var_Dump($sSQL);
841 $aSearchWords = chksql($this->oDB->getAll($sSQL));
842 $aNewSearches = array();
843 foreach ($aSearches as $aSearch) {
844 foreach ($aSearchWords as $aSearchTerm) {
845 $aNewSearch = $aSearch;
846 if ($aSearchTerm['country_code']) {
847 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
848 $aNewSearches[] = $aNewSearch;
849 $bSpecialTerms = true;
851 if ($aSearchTerm['class']) {
852 $aNewSearch['sClass'] = $aSearchTerm['class'];
853 $aNewSearch['sType'] = $aSearchTerm['type'];
854 $aNewSearches[] = $aNewSearch;
855 $bSpecialTerms = true;
859 $aSearches = $aNewSearches;
862 // Split query into phrases
863 // Commas are used to reduce the search space by indicating where phrases split
864 if ($this->aStructuredQuery) {
865 $aPhrases = $this->aStructuredQuery;
866 $bStructuredPhrases = true;
868 $aPhrases = explode(',', $sQuery);
869 $bStructuredPhrases = false;
872 // Convert each phrase to standard form
873 // Create a list of standard words
874 // Get all 'sets' of words
875 // Generate a complete list of all
877 foreach ($aPhrases as $iPhrase => $sPhrase) {
879 $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string"),
880 "Cannot nomralize query string (is it an UTF-8 string?)"
882 if (trim($aPhrase['string'])) {
883 $aPhrases[$iPhrase] = $aPhrase;
884 $aPhrases[$iPhrase]['words'] = explode(' ', $aPhrases[$iPhrase]['string']);
885 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
886 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
888 unset($aPhrases[$iPhrase]);
892 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
893 $aPhraseTypes = array_keys($aPhrases);
894 $aPhrases = array_values($aPhrases);
896 if (sizeof($aTokens)) {
897 // Check which tokens we have, get the ID numbers
898 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
899 $sSQL .= ' from word where word_token in ('.join(',', array_map("getDBQuoted", $aTokens)).')';
901 if (CONST_Debug) var_Dump($sSQL);
903 $aValidTokens = array();
904 if (sizeof($aTokens)) {
905 $aDatabaseWords = chksql(
906 $this->oDB->getAll($sSQL),
907 "Could not get word tokens."
910 $aDatabaseWords = array();
912 $aPossibleMainWordIDs = array();
913 $aWordFrequencyScores = array();
914 foreach ($aDatabaseWords as $aToken) {
915 // Very special case - require 2 letter country param to match the country code found
916 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
917 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code']
922 if (isset($aValidTokens[$aToken['word_token']])) {
923 $aValidTokens[$aToken['word_token']][] = $aToken;
925 $aValidTokens[$aToken['word_token']] = array($aToken);
927 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
928 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
930 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
932 // Try and calculate GB postcodes we might be missing
933 foreach ($aTokens as $sToken) {
934 // Source of gb postcodes is now definitive - always use
935 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData)) {
936 if (substr($aData[1], -2, 1) != ' ') {
937 $aData[0] = substr($aData[0], 0, strlen($aData[1])-1).' '.substr($aData[0], strlen($aData[1])-1);
938 $aData[1] = substr($aData[1], 0, -1).' '.substr($aData[1], -1, 1);
940 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
941 if ($aGBPostcodeLocation) {
942 $aValidTokens[$sToken] = $aGBPostcodeLocation;
944 } elseif (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
945 // US ZIP+4 codes - if there is no token,
946 // merge in the 5-digit ZIP code
947 if (isset($aValidTokens[$aData[1]])) {
948 foreach ($aValidTokens[$aData[1]] as $aToken) {
949 if (!$aToken['class']) {
950 if (isset($aValidTokens[$sToken])) {
951 $aValidTokens[$sToken][] = $aToken;
953 $aValidTokens[$sToken] = array($aToken);
961 foreach ($aTokens as $sToken) {
962 // Unknown single word token with a number - assume it is a house number
963 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken, ' ') === false && preg_match('/[0-9]/', $sToken)) {
964 $aValidTokens[' '.$sToken] = array(array('class' => 'place', 'type' => 'house'));
968 // Any words that have failed completely?
971 // Start the search process
972 // array with: placeid => -1 | tiger-housenumber
973 $aResultPlaceIDs = array();
975 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
977 if ($this->bReverseInPlan) {
978 // Reverse phrase array and also reverse the order of the wordsets in
979 // the first and final phrase. Don't bother about phrases in the middle
980 // because order in the address doesn't matter.
981 $aPhrases = array_reverse($aPhrases);
982 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
983 if (sizeof($aPhrases) > 1) {
984 $aFinalPhrase = end($aPhrases);
985 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
987 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
989 foreach ($aGroupedSearches as $aSearches) {
990 foreach ($aSearches as $aSearch) {
991 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
992 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
993 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
998 $aGroupedSearches = $aReverseGroupedSearches;
999 ksort($aGroupedSearches);
1002 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1003 $aGroupedSearches = array();
1004 foreach ($aSearches as $aSearch) {
1005 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
1006 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1007 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1010 ksort($aGroupedSearches);
1013 if (CONST_Debug) var_Dump($aGroupedSearches);
1014 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0) {
1015 $aCopyGroupedSearches = $aGroupedSearches;
1016 foreach ($aCopyGroupedSearches as $iGroup => $aSearches) {
1017 foreach ($aSearches as $iSearch => $aSearch) {
1018 $aReductionsList = array($aSearch['aAddress']);
1019 $iSearchRank = $aSearch['iSearchRank'];
1020 while (sizeof($aReductionsList) > 0) {
1022 if ($iSearchRank > iMaxRank) break 3;
1023 $aNewReductionsList = array();
1024 foreach ($aReductionsList as $aReductionsWordList) {
1025 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++) {
1026 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1027 $aReverseSearch = $aSearch;
1028 $aSearch['aAddress'] = $aReductionsWordListResult;
1029 $aSearch['iSearchRank'] = $iSearchRank;
1030 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1031 if (sizeof($aReductionsWordListResult) > 0) {
1032 $aNewReductionsList[] = $aReductionsWordListResult;
1036 $aReductionsList = $aNewReductionsList;
1040 ksort($aGroupedSearches);
1043 // Filter out duplicate searches
1044 $aSearchHash = array();
1045 foreach ($aGroupedSearches as $iGroup => $aSearches) {
1046 foreach ($aSearches as $iSearch => $aSearch) {
1047 $sHash = serialize($aSearch);
1048 if (isset($aSearchHash[$sHash])) {
1049 unset($aGroupedSearches[$iGroup][$iSearch]);
1050 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1052 $aSearchHash[$sHash] = 1;
1057 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1061 foreach ($aGroupedSearches as $iGroupedRank => $aSearches) {
1063 foreach ($aSearches as $aSearch) {
1065 $searchedHousenumber = -1;
1067 if (CONST_Debug) echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>";
1068 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1070 // No location term?
1071 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon']) {
1072 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber']) {
1073 // Just looking for a country by code - look it up
1074 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank) {
1075 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1076 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1077 if ($bBoundingBoxSearch)
1078 $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1079 $sSQL .= " order by st_area(geometry) desc limit 1";
1080 if (CONST_Debug) var_dump($sSQL);
1081 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1083 $aPlaceIDs = array();
1086 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1087 if (!$aSearch['sClass']) continue;
1089 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1090 if (chksql($this->oDB->getOne($sSQL))) {
1091 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1092 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1093 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1094 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1095 if (sizeof($this->aExcludePlaceIDs)) {
1096 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1098 if ($this->sViewboxCentreSQL) $sSQL .= " order by st_distance($this->sViewboxCentreSQL, ct.centroid) asc";
1099 $sSQL .= " limit $this->iLimit";
1100 if (CONST_Debug) var_dump($sSQL);
1101 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1103 // If excluded place IDs are given, it is fair to assume that
1104 // there have been results in the small box, so no further
1105 // expansion in that case.
1106 // Also don't expand if bounded results were requested.
1107 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch) {
1108 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1109 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1110 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1111 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1112 if ($this->sViewboxCentreSQL) $sSQL .= " order by st_distance($this->sViewboxCentreSQL, ct.centroid) asc";
1113 $sSQL .= " limit $this->iLimit";
1114 if (CONST_Debug) var_dump($sSQL);
1115 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1118 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1119 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1120 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1121 if ($this->sViewboxCentreSQL) $sSQL .= " order by st_distance($this->sViewboxCentreSQL, centroid) asc";
1122 $sSQL .= " limit $this->iLimit";
1123 if (CONST_Debug) var_dump($sSQL);
1124 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1127 } elseif ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
1128 // If a coordinate is given, the search must either
1129 // be for a name or a special search. Ignore everythin else.
1130 $aPlaceIDs = array();
1132 $aPlaceIDs = array();
1134 // First we need a position, either aName or fLat or both
1138 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress'])) {
1139 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1141 $aOrder[0] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
1142 $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
1143 // also housenumbers from interpolation lines table are needed
1144 $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
1145 $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
1146 $aOrder[0] .= " desc";
1149 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1150 // they might be right - but they are just too darned expensive to run
1151 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'], ",")."]";
1152 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'], ",")."]";
1153 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) {
1154 // For infrequent name terms disable index usage for address
1155 if (CONST_Search_NameOnlySearchFrequencyThreshold
1156 && sizeof($aSearch['aName']) == 1
1157 && $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold
1159 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'], $aSearch['aAddressNonSearch']), ",")."]";
1161 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'], ",")."]";
1162 if (sizeof($aSearch['aAddressNonSearch'])) {
1163 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'], ",")."]";
1167 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1168 if ($aSearch['sHouseNumber']) {
1169 $aTerms[] = "address_rank between 16 and 27";
1171 if ($this->iMinAddressRank > 0) {
1172 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1174 if ($this->iMaxAddressRank < 30) {
1175 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1178 if ($aSearch['fLon'] && $aSearch['fLat']) {
1179 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1180 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1182 if (sizeof($this->aExcludePlaceIDs)) {
1183 $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1185 if ($sCountryCodesSQL) {
1186 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1189 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1190 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1192 if ($aSearch['sHouseNumber']) {
1193 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1195 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1197 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1198 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1200 $aOrder[] = "$sImportanceSQL DESC";
1201 if (sizeof($aSearch['aFullNameAddress'])) {
1202 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'], ",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1203 $aOrder[] = 'exactmatch DESC';
1205 $sExactMatchSQL = '0::int as exactmatch';
1208 if (sizeof($aTerms)) {
1209 $sSQL = "select place_id, ";
1210 $sSQL .= $sExactMatchSQL;
1211 $sSQL .= " from search_name";
1212 $sSQL .= " where ".join(' and ', $aTerms);
1213 $sSQL .= " order by ".join(', ', $aOrder);
1214 if ($aSearch['sHouseNumber'] || $aSearch['sClass']) {
1215 $sSQL .= " limit 20";
1216 } elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass']) {
1217 $sSQL .= " limit 1";
1219 $sSQL .= " limit ".$this->iLimit;
1222 if (CONST_Debug) var_dump($sSQL);
1223 $aViewBoxPlaceIDs = chksql(
1224 $this->oDB->getAll($sSQL),
1225 "Could not get places for search terms."
1227 //var_dump($aViewBoxPlaceIDs);
1228 // Did we have an viewbox matches?
1229 $aPlaceIDs = array();
1230 $bViewBoxMatch = false;
1231 foreach ($aViewBoxPlaceIDs as $aViewBoxRow) {
1232 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1233 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1234 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1235 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1236 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1237 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1240 //var_Dump($aPlaceIDs);
1243 //now search for housenumber, if housenumber provided
1244 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs)) {
1245 $searchedHousenumber = intval($aSearch['sHouseNumber']);
1246 $aRoadPlaceIDs = $aPlaceIDs;
1247 $sPlaceIDs = join(',', $aPlaceIDs);
1249 // Now they are indexed, look for a house attached to a street we found
1250 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1251 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1252 if (sizeof($this->aExcludePlaceIDs)) {
1253 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1255 $sSQL .= " limit $this->iLimit";
1256 if (CONST_Debug) var_dump($sSQL);
1257 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1259 // if nothing found, search in the interpolation line table
1260 if (!sizeof($aPlaceIDs)) {
1261 // do we need to use transliteration and the regex for housenumbers???
1262 //new query for lines, not housenumbers anymore
1263 if ($searchedHousenumber%2 == 0) {
1264 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1265 $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";
1267 //look for housenumber in streets with interpolationtype odd or all
1268 $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";
1271 if (sizeof($this->aExcludePlaceIDs)) {
1272 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1274 //$sSQL .= " limit $this->iLimit";
1275 if (CONST_Debug) var_dump($sSQL);
1277 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1280 // If nothing found try the aux fallback table
1281 if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs)) {
1282 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1283 if (sizeof($this->aExcludePlaceIDs)) {
1284 $sSQL .= " and parent_place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1286 //$sSQL .= " limit $this->iLimit";
1287 if (CONST_Debug) var_dump($sSQL);
1288 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1291 //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1292 if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs)) {
1293 //new query for lines, not housenumbers anymore
1294 if ($searchedHousenumber%2 == 0) {
1295 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1296 $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";
1298 //look for housenumber in streets with interpolationtype odd or all
1299 $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";
1302 if (sizeof($this->aExcludePlaceIDs)) {
1303 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1305 //$sSQL .= " limit $this->iLimit";
1306 if (CONST_Debug) var_dump($sSQL);
1308 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1311 // Fallback to the road (if no housenumber was found)
1312 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber'])) {
1313 $aPlaceIDs = $aRoadPlaceIDs;
1314 //set to -1, if no housenumbers were found
1315 $searchedHousenumber = -1;
1317 //else: housenumber was found, remains saved in searchedHousenumber
1321 if ($aSearch['sClass'] && sizeof($aPlaceIDs)) {
1322 $sPlaceIDs = join(',', $aPlaceIDs);
1323 $aClassPlaceIDs = array();
1325 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name') {
1326 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1327 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1328 $sSQL .= " and linked_place_id is null";
1329 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1330 $sSQL .= " order by rank_search asc limit $this->iLimit";
1331 if (CONST_Debug) var_dump($sSQL);
1332 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
1335 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') { // & in
1336 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1337 $bCacheTable = chksql($this->oDB->getOne($sSQL));
1339 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1341 if (CONST_Debug) var_dump($sSQL);
1342 $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
1344 // For state / country level searches the normal radius search doesn't work very well
1345 $sPlaceGeom = false;
1346 if ($this->iMaxRank < 9 && $bCacheTable) {
1347 // Try and get a polygon to search in instead
1348 $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";
1349 if (CONST_Debug) var_dump($sSQL);
1350 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
1356 $this->iMaxRank += 5;
1357 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1358 if (CONST_Debug) var_dump($sSQL);
1359 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1360 $sPlaceIDs = join(',', $aPlaceIDs);
1363 if ($sPlaceIDs || $sPlaceGeom) {
1366 // More efficient - can make the range bigger
1370 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1371 elseif ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1372 elseif ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1374 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1375 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1377 $sSQL .= ",placex as f where ";
1378 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1382 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1384 if (sizeof($this->aExcludePlaceIDs)) {
1385 $sSQL .= " and l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1387 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1388 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1389 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1390 $sSQL .= " limit $this->iLimit";
1391 if (CONST_Debug) var_dump($sSQL);
1392 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1394 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1397 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1398 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1400 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1401 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1402 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1403 if (sizeof($this->aExcludePlaceIDs)) {
1404 $sSQL .= " and l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1406 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1407 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1408 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1409 $sSQL .= " limit $this->iLimit";
1410 if (CONST_Debug) var_dump($sSQL);
1411 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1415 $aPlaceIDs = $aClassPlaceIDs;
1420 echo "<br><b>Place IDs:</b> ";
1421 var_Dump($aPlaceIDs);
1424 foreach ($aPlaceIDs as $iPlaceID) {
1425 // array for placeID => -1 | Tiger housenumber
1426 $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1428 if ($iQueryLoop > 20) break;
1431 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
1432 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1433 // reduces the number of place ids, like a filter
1434 // rank_address is 30 for interpolated housenumbers
1435 $sSQL = "select place_id from placex where place_id in (".join(',', array_keys($aResultPlaceIDs)).") ";
1436 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1437 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1438 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
1439 if (CONST_Use_US_Tiger_Data) {
1440 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',', array_keys($aResultPlaceIDs)).") ";
1441 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1442 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',', $this->aAddressRankList).")";
1444 $sSQL .= ") UNION select place_id from location_property_osmline where place_id in (".join(',', array_keys($aResultPlaceIDs)).")";
1445 $sSQL .= " and (30 between $this->iMinAddressRank and $this->iMaxAddressRank)";
1446 if (CONST_Debug) var_dump($sSQL);
1447 $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1449 foreach ($aFilteredPlaceIDs as $placeID) {
1450 $tempIDs[$placeID] = $aResultPlaceIDs[$placeID]; //assign housenumber to placeID
1452 $aResultPlaceIDs = $tempIDs;
1456 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1457 if ($iGroupLoop > 4) break;
1458 if ($iQueryLoop > 30) break;
1461 // Did we find anything?
1462 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) {
1463 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1466 // Just interpret as a reverse geocode
1467 $oReverse = new ReverseGeocode($this->oDB);
1468 $oReverse->setZoom(18);
1470 $aLookup = $oReverse->lookup(
1471 (float)$this->aNearPoint[0],
1472 (float)$this->aNearPoint[1],
1476 if (CONST_Debug) var_dump("Reverse search", $aLookup);
1478 if ($aLookup['place_id']) {
1479 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
1481 $aSearchResults = array();
1486 if (!sizeof($aSearchResults)) {
1487 if ($this->bFallback) {
1488 if ($this->fallbackStructuredQuery()) {
1489 return $this->lookup();
1496 $aClassType = getClassTypesWithImportance();
1497 $aRecheckWords = preg_split('/\b[\s,\\-]*/u', $sQuery);
1498 foreach ($aRecheckWords as $i => $sWord) {
1499 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1503 echo '<i>Recheck words:<\i>';
1504 var_dump($aRecheckWords);
1507 $oPlaceLookup = new PlaceLookup($this->oDB);
1508 $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints);
1509 $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText);
1510 $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON);
1511 $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML);
1512 $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG);
1513 $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold);
1515 foreach ($aSearchResults as $iResNum => $aResult) {
1517 $fDiameter = getResultDiameter($aResult);
1519 $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2);
1520 if ($aOutlineResult) {
1521 $aResult = array_merge($aResult, $aOutlineResult);
1524 if ($aResult['extra_place'] == 'city') {
1525 $aResult['class'] = 'place';
1526 $aResult['type'] = 'city';
1527 $aResult['rank_search'] = 16;
1530 // Is there an icon set for this type of result?
1531 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1532 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon']
1534 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1537 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1538 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label']
1540 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1541 } elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1542 && $aClassType[$aResult['class'].':'.$aResult['type']]['label']
1544 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1546 // if tag '&addressdetails=1' is set in query
1547 if ($this->bIncludeAddressDetails) {
1548 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1549 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1550 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city'])) {
1551 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1555 if ($this->bIncludeExtraTags) {
1556 if ($aResult['extra']) {
1557 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1559 $aResult['sExtraTags'] = (object) array();
1563 if ($this->bIncludeNameDetails) {
1564 if ($aResult['names']) {
1565 $aResult['sNameDetails'] = json_decode($aResult['names']);
1567 $aResult['sNameDetails'] = (object) array();
1571 // Adjust importance for the number of exact string matches in the result
1572 $aResult['importance'] = max(0.001, $aResult['importance']);
1574 $sAddress = $aResult['langaddress'];
1575 foreach ($aRecheckWords as $i => $sWord) {
1576 if (stripos($sAddress, $sWord)!==false) {
1578 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1582 $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
1584 $aResult['name'] = $aResult['langaddress'];
1585 // secondary ordering (for results with same importance (the smaller the better):
1586 // - approximate importance of address parts
1587 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1588 // - number of exact matches from the query
1589 if (isset($this->exactMatchCache[$aResult['place_id']])) {
1590 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1591 } elseif (isset($this->exactMatchCache[$aResult['parent_place_id']])) {
1592 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1594 // - importance of the class/type
1595 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1596 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance']
1598 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1600 $aResult['foundorder'] += 0.01;
1602 if (CONST_Debug) var_dump($aResult);
1603 $aSearchResults[$iResNum] = $aResult;
1605 uasort($aSearchResults, 'byImportance');
1607 $aOSMIDDone = array();
1608 $aClassTypeNameDone = array();
1609 $aToFilter = $aSearchResults;
1610 $aSearchResults = array();
1613 foreach ($aToFilter as $iResNum => $aResult) {
1614 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1616 $fLat = $aResult['lat'];
1617 $fLon = $aResult['lon'];
1618 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1621 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1622 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']]))
1624 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1625 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1626 $aSearchResults[] = $aResult;
1629 // Absolute limit on number of results
1630 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1633 return $aSearchResults;