6 protected $aLangPrefOrder = array();
8 protected $bIncludeAddressDetails = false;
10 protected $bIncludePolygonAsPoints = false;
11 protected $bIncludePolygonAsText = false;
12 protected $bIncludePolygonAsGeoJSON = false;
13 protected $bIncludePolygonAsKML = false;
14 protected $bIncludePolygonAsSVG = false;
16 protected $aExcludePlaceIDs = array();
17 protected $bDeDupe = true;
18 protected $bReverseInPlan = false;
20 protected $iLimit = 20;
21 protected $iFinalLimit = 10;
22 protected $iOffset = 0;
24 protected $aCountryCodes = false;
25 protected $aNearPoint = false;
27 protected $bBoundedSearch = false;
28 protected $aViewBox = false;
29 protected $aRoutePoints = false;
31 protected $iMaxRank = 20;
32 protected $iMinAddressRank = 0;
33 protected $iMaxAddressRank = 30;
34 protected $aAddressRankList = array();
36 protected $sAllowedTypesSQLList = false;
38 protected $sQuery = false;
39 protected $aStructuredQuery = false;
41 function Geocode(&$oDB)
46 function setLanguagePreference($aLangPref)
48 $this->aLangPrefOrder = $aLangPref;
51 function setIncludeAddressDetails($bAddressDetails = true)
53 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
56 function getIncludeAddressDetails()
58 return $this->bIncludeAddressDetails;
61 function setIncludePolygonAsPoints($b = true)
63 $this->bIncludePolygonAsPoints = $b;
66 function getIncludePolygonAsPoints()
68 return $this->bIncludePolygonAsPoints;
71 function setIncludePolygonAsText($b = true)
73 $this->bIncludePolygonAsText = $b;
76 function getIncludePolygonAsText()
78 return $this->bIncludePolygonAsText;
81 function setIncludePolygonAsGeoJSON($b = true)
83 $this->bIncludePolygonAsGeoJSON = $b;
86 function setIncludePolygonAsKML($b = true)
88 $this->bIncludePolygonAsKML = $b;
91 function setIncludePolygonAsSVG($b = true)
93 $this->bIncludePolygonAsSVG = $b;
96 function setDeDupe($bDeDupe = true)
98 $this->bDeDupe = (bool)$bDeDupe;
101 function setLimit($iLimit = 10)
103 if ($iLimit > 50) $iLimit = 50;
104 if ($iLimit < 1) $iLimit = 1;
106 $this->iFinalLimit = $iLimit;
107 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
110 function setOffset($iOffset = 0)
112 $this->iOffset = $iOffset;
115 function setExcludedPlaceIDs($a)
117 // TODO: force to int
118 $this->aExcludePlaceIDs = $a;
121 function getExcludedPlaceIDs()
123 return $this->aExcludePlaceIDs;
126 function setBounded($bBoundedSearch = true)
128 $this->bBoundedSearch = (bool)$bBoundedSearch;
131 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
133 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
136 function getViewBoxString()
138 if (!$this->aViewBox) return null;
139 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
142 function setRoute($aRoutePoints)
144 $this->aRoutePoints = $aRoutePoints;
147 function setFeatureType($sFeatureType)
149 switch($sFeatureType)
152 $this->setRankRange(4, 4);
155 $this->setRankRange(8, 8);
158 $this->setRankRange(14, 16);
161 $this->setRankRange(8, 20);
166 function setRankRange($iMin, $iMax)
168 $this->iMinAddressRank = (int)$iMin;
169 $this->iMaxAddressRank = (int)$iMax;
172 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
174 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
177 function setCountryCodesList($aCountryCodes)
179 $this->aCountryCodes = $aCountryCodes;
182 function setQuery($sQueryString)
184 $this->sQuery = $sQueryString;
185 $this->aStructuredQuery = false;
188 function getQueryString()
190 return $this->sQuery;
193 function loadStructuredAddressElement(&$aStructuredQuery, &$iMinAddressRank, &$iMaxAddressRank, &$aAddressRankList, $sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
195 $sValue = trim($sValue);
196 if (!$sValue) return false;
197 $aStructuredQuery[$sKey] = $sValue;
198 if ($iMinAddressRank == 0 && $iMaxAddressRank == 30)
200 $iMinAddressRank = $iNewMinAddressRank;
201 $iMaxAddressRank = $iNewMaxAddressRank;
203 if ($aItemListValues) $aAddressRankList = array_merge($aAddressRankList, $aItemListValues);
207 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
209 $this->sQuery = false;
211 $this->aStructuredQuery = array();
212 $this->sAllowedTypesSQLList = '';
214 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sAmentiy, 'amenity', 26, 30, false);
215 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sStreet, 'street', 26, 30, false);
216 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCity, 'city', 14, 24, false);
217 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCounty, 'county', 9, 13, false);
218 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sState, 'state', 8, 8, false);
219 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCountry, 'country', 4, 4, false);
220 $this->loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sPostalCode, 'postalcode' , 5, 11, array(5, 11));
222 if (sizeof($this->aStructuredQuery) > 0)
224 $this->sQuery = join(', ', $this->aStructuredQuery);
225 if ($this->iMaxAddressRank < 30)
227 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
233 function getDetails($aPlaceIDs)
235 if (sizeof($aPlaceIDs) == 0) return array();
237 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
239 // Get the details for display (is this a redundant extra step?)
240 $sPlaceIDs = join(',',$aPlaceIDs);
242 $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id,calculated_country_code as country_code,";
243 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
244 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
245 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
246 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
247 $sSQL .= "coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
248 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(placex.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
249 $sSQL .= "(extratags->'place') as extra_place ";
250 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
251 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
252 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
253 if ($aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$aAddressRankList).")";
255 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
256 $sSQL .= "and linked_place_id is null ";
257 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
258 if (!$this->bDeDupe) $sSQL .= ",place_id";
259 $sSQL .= ",langaddress ";
260 $sSQL .= ",placename ";
262 $sSQL .= ",extratags->'place' ";
264 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
267 $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,'us' as country_code,";
268 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
269 $sSQL .= "null as placename,";
270 $sSQL .= "null as ref,";
271 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
272 $sSQL .= "-0.15 as importance, ";
273 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
274 $sSQL .= "null as extra_place ";
275 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
276 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
277 $sSQL .= "group by place_id";
278 if (!$this->bDeDupe) $sSQL .= ",place_id";
280 $sSQL .= "select 'L' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id,'us' as country_code,";
281 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
282 $sSQL .= "null as placename,";
283 $sSQL .= "null as ref,";
284 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
285 $sSQL .= "-0.10 as importance, ";
286 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
287 $sSQL .= "null as extra_place ";
288 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
289 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
290 $sSQL .= "group by place_id";
291 if (!$this->bDeDupe) $sSQL .= ",place_id";
292 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
295 $sSQL .= "order by importance desc";
296 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
297 $aSearchResults = $this->oDB->getAll($sSQL);
299 if (PEAR::IsError($aSearchResults))
301 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
304 return $aSearchResults;
309 if (!$this->sQuery && !$this->aStructuredQuery) return false;
311 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
313 $sCountryCodesSQL = false;
314 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
316 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
319 // Hack to make it handle "new york, ny" (and variants) correctly
320 $sQuery = str_ireplace(array('New York, ny','new york, new york', 'New York ny','new york new york'), 'new york city, ny', $this->sQuery);
322 // Conflicts between US state abreviations and various words for 'the' in different languages
323 if (isset($this->aLangPrefOrder['name:en']))
325 $sQuery = preg_replace('/,\s*il\s*(,|$)/',', illinois\1', $sQuery);
326 $sQuery = preg_replace('/,\s*al\s*(,|$)/',', alabama\1', $sQuery);
327 $sQuery = preg_replace('/,\s*la\s*(,|$)/',', louisiana\1', $sQuery);
331 $sViewboxCentreSQL = $sViewboxSmallSQL = $sViewboxLargeSQL = false;
332 $bBoundingBoxSearch = false;
335 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
336 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
337 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
338 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
339 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
340 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
342 $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)";
343 $sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
344 $bBoundingBoxSearch = $this->bBoundedSearch;
348 if ($this->aRoutePoints)
350 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
352 foreach($this->aRouteaPoints as $aPoint)
354 if (!$bFirst) $sViewboxCentreSQL .= ",";
355 $sViewboxCentreSQL .= $aPoint[1].' '.$aPoint[0];
357 $sViewboxCentreSQL .= ")'::geometry,4326)";
359 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
360 $sViewboxSmallSQL = $this->oDB->getOne($sSQL);
361 if (PEAR::isError($sViewboxSmallSQL))
363 failInternalError("Could not get small viewbox.", $sSQL, $sViewboxSmallSQL);
365 $sViewboxSmallSQL = "'".$sViewboxSmallSQL."'::geometry";
367 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
368 $sViewboxLargeSQL = $this->oDB->getOne($sSQL);
369 if (PEAR::isError($sViewboxLargeSQL))
371 failInternalError("Could not get large viewbox.", $sSQL, $sViewboxLargeSQL);
373 $sViewboxLargeSQL = "'".$sViewboxLargeSQL."'::geometry";
374 $bBoundingBoxSearch = $this->bBoundedSearch;
377 // Do we have anything that looks like a lat/lon pair?
378 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
380 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
381 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
382 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
384 $this->setNearPoint(array($fQueryLat, $fQueryLon));
385 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
388 elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
390 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
391 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
392 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
394 $this->setNearPoint(array($fQueryLat, $fQueryLon));
395 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
398 elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
400 $fQueryLat = $aData[2];
401 $fQueryLon = $aData[3];
402 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
404 $this->setNearPoint(array($fQueryLat, $fQueryLon));
405 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
409 $aSearchResults = array();
410 if ($sQuery || $this->aStructuredQuery)
412 // Start with a blank search
414 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
415 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
416 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
419 // Do we have a radius search?
420 $sNearPointSQL = false;
421 if ($this->aNearPoint)
423 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
424 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
425 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
426 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
429 // Any 'special' terms in the search?
430 $bSpecialTerms = false;
431 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
432 $aSpecialTerms = array();
433 foreach($aSpecialTermsRaw as $aSpecialTerm)
435 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
436 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
439 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
440 $aSpecialTerms = array();
441 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
443 $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
444 unset($aStructuredQuery['amenity']);
446 foreach($aSpecialTermsRaw as $aSpecialTerm)
448 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
449 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
450 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
451 $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';
452 if (CONST_Debug) var_Dump($sSQL);
453 $aSearchWords = $this->oDB->getAll($sSQL);
454 $aNewSearches = array();
455 foreach($aSearches as $aSearch)
457 foreach($aSearchWords as $aSearchTerm)
459 $aNewSearch = $aSearch;
460 if ($aSearchTerm['country_code'])
462 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
463 $aNewSearches[] = $aNewSearch;
464 $bSpecialTerms = true;
466 if ($aSearchTerm['class'])
468 $aNewSearch['sClass'] = $aSearchTerm['class'];
469 $aNewSearch['sType'] = $aSearchTerm['type'];
470 $aNewSearches[] = $aNewSearch;
471 $bSpecialTerms = true;
475 $aSearches = $aNewSearches;
478 // Split query into phrases
479 // Commas are used to reduce the search space by indicating where phrases split
480 if ($this->aStructuredQuery)
482 $aPhrases = $this->aStructuredQuery;
483 $bStructuredPhrases = true;
487 $aPhrases = explode(',',$sQuery);
488 $bStructuredPhrases = false;
491 // Convert each phrase to standard form
492 // Create a list of standard words
493 // Get all 'sets' of words
494 // Generate a complete list of all
496 foreach($aPhrases as $iPhrase => $sPhrase)
498 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
499 if (PEAR::isError($aPhrase))
501 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
502 if (CONST_Debug) var_dump($aPhrase);
505 if (trim($aPhrase['string']))
507 $aPhrases[$iPhrase] = $aPhrase;
508 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
509 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
510 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
514 unset($aPhrases[$iPhrase]);
518 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
519 $aPhraseTypes = array_keys($aPhrases);
520 $aPhrases = array_values($aPhrases);
522 if (sizeof($aTokens))
524 // Check which tokens we have, get the ID numbers
525 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
526 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
528 if (CONST_Debug) var_Dump($sSQL);
530 $aValidTokens = array();
531 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
532 else $aDatabaseWords = array();
533 if (PEAR::IsError($aDatabaseWords))
535 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
537 $aPossibleMainWordIDs = array();
538 $aWordFrequencyScores = array();
539 foreach($aDatabaseWords as $aToken)
541 // Very special case - require 2 letter country param to match the country code found
542 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
543 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
548 if (isset($aValidTokens[$aToken['word_token']]))
550 $aValidTokens[$aToken['word_token']][] = $aToken;
554 $aValidTokens[$aToken['word_token']] = array($aToken);
556 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
557 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
559 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
561 // Try and calculate GB postcodes we might be missing
562 foreach($aTokens as $sToken)
564 // Source of gb postcodes is now definitive - always use
565 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
567 if (substr($aData[1],-2,1) != ' ')
569 $aData[0] = substr($aData[0],0,strlen($aData[1]-1)).' '.substr($aData[0],strlen($aData[1]-1));
570 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
572 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
573 if ($aGBPostcodeLocation)
575 $aValidTokens[$sToken] = $aGBPostcodeLocation;
578 // US ZIP+4 codes - if there is no token,
579 // merge in the 5-digit ZIP code
580 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
582 if (isset($aValidTokens[$aData[1]]))
584 foreach($aValidTokens[$aData[1]] as $aToken)
586 if (!$aToken['class'])
588 if (isset($aValidTokens[$sToken]))
590 $aValidTokens[$sToken][] = $aToken;
594 $aValidTokens[$sToken] = array($aToken);
602 foreach($aTokens as $sToken)
604 // Unknown single word token with a number - assume it is a house number
605 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
607 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
611 // Any words that have failed completely?
614 // Start the search process
615 $aResultPlaceIDs = array();
618 Calculate all searches using aValidTokens i.e.
619 'Wodsworth Road, Sheffield' =>
623 0 1 (wodsworth)(road)
626 Score how good the search is so they can be ordered
628 foreach($aPhrases as $iPhrase => $sPhrase)
630 $aNewPhraseSearches = array();
631 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
632 else $sPhraseType = '';
634 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
636 // Too many permutations - too expensive
637 if ($iWordSet > 120) break;
639 $aWordsetSearches = $aSearches;
641 // Add all words from this wordset
642 foreach($aWordset as $iToken => $sToken)
644 //echo "<br><b>$sToken</b>";
645 $aNewWordsetSearches = array();
647 foreach($aWordsetSearches as $aCurrentSearch)
650 //var_dump($aCurrentSearch);
653 // If the token is valid
654 if (isset($aValidTokens[' '.$sToken]))
656 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
658 $aSearch = $aCurrentSearch;
659 $aSearch['iSearchRank']++;
660 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
662 if ($aSearch['sCountryCode'] === false)
664 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
665 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
666 // If reverse order is enabled, it may appear at the beginning as well.
667 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) &&
668 (!$this->bReverseInPlan || $iToken > 0 || $iPhrase > 0))
670 $aSearch['iSearchRank'] += 5;
672 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
675 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
677 if ($aSearch['fLat'] === '')
679 $aSearch['fLat'] = $aSearchTerm['lat'];
680 $aSearch['fLon'] = $aSearchTerm['lon'];
681 $aSearch['fRadius'] = $aSearchTerm['radius'];
682 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
685 elseif ($sPhraseType == 'postalcode')
687 // 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
688 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
690 // If we already have a name try putting the postcode first
691 if (sizeof($aSearch['aName']))
693 $aNewSearch = $aSearch;
694 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
695 $aNewSearch['aName'] = array();
696 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
697 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
700 if (sizeof($aSearch['aName']))
702 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
704 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
708 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
709 $aSearch['iSearchRank'] += 1000; // skip;
714 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
715 //$aSearch['iNamePhrase'] = $iPhrase;
717 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
721 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
723 if ($aSearch['sHouseNumber'] === '')
725 $aSearch['sHouseNumber'] = $sToken;
726 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
728 // Fall back to not searching for this item (better than nothing)
729 $aSearch = $aCurrentSearch;
730 $aSearch['iSearchRank'] += 1;
731 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
735 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
737 if ($aSearch['sClass'] === '')
739 $aSearch['sOperator'] = $aSearchTerm['operator'];
740 $aSearch['sClass'] = $aSearchTerm['class'];
741 $aSearch['sType'] = $aSearchTerm['type'];
742 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
743 else $aSearch['sOperator'] = 'near'; // near = in for the moment
745 // Do we have a shortcut id?
746 if ($aSearch['sOperator'] == 'name')
748 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
749 if ($iAmenityID = $this->oDB->getOne($sSQL))
751 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
752 $aSearch['aName'][$iAmenityID] = $iAmenityID;
753 $aSearch['sClass'] = '';
754 $aSearch['sType'] = '';
757 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
760 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
762 if (sizeof($aSearch['aName']))
764 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
766 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
770 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
771 $aSearch['iSearchRank'] += 1000; // skip;
776 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
777 //$aSearch['iNamePhrase'] = $iPhrase;
779 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
783 if (isset($aValidTokens[$sToken]))
785 // Allow searching for a word - but at extra cost
786 foreach($aValidTokens[$sToken] as $aSearchTerm)
788 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
790 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strlen($sToken) >= 4)
792 $aSearch = $aCurrentSearch;
793 $aSearch['iSearchRank'] += 1;
794 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
796 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
797 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
799 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
801 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
803 if (empty($aSearchTermToken['country_code'])
804 && empty($aSearchTermToken['lat'])
805 && empty($aSearchTermToken['class']))
807 $aSearch = $aCurrentSearch;
808 $aSearch['iSearchRank'] += 1;
809 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
810 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
816 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
817 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
821 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
823 $aSearch = $aCurrentSearch;
824 $aSearch['iSearchRank'] += 2;
825 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
826 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
827 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
829 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
830 $aSearch['iNamePhrase'] = $iPhrase;
831 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
838 // Allow skipping a word - but at EXTREAM cost
839 //$aSearch = $aCurrentSearch;
840 //$aSearch['iSearchRank']+=100;
841 //$aNewWordsetSearches[] = $aSearch;
845 usort($aNewWordsetSearches, 'bySearchRank');
846 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
848 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
850 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
851 usort($aNewPhraseSearches, 'bySearchRank');
853 $aSearchHash = array();
854 foreach($aNewPhraseSearches as $iSearch => $aSearch)
856 $sHash = serialize($aSearch);
857 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
858 else $aSearchHash[$sHash] = 1;
861 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
864 // Re-group the searches by their score, junk anything over 20 as just not worth trying
865 $aGroupedSearches = array();
866 foreach($aNewPhraseSearches as $aSearch)
868 if ($aSearch['iSearchRank'] < $this->iMaxRank)
870 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
871 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
874 ksort($aGroupedSearches);
877 $aSearches = array();
878 foreach($aGroupedSearches as $iScore => $aNewSearches)
880 $iSearchCount += sizeof($aNewSearches);
881 $aSearches = array_merge($aSearches, $aNewSearches);
882 if ($iSearchCount > 50) break;
885 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
892 // Re-group the searches by their score, junk anything over 20 as just not worth trying
893 $aGroupedSearches = array();
894 foreach($aSearches as $aSearch)
896 if ($aSearch['iSearchRank'] < $this->iMaxRank)
898 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
899 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
902 ksort($aGroupedSearches);
905 if (CONST_Debug) var_Dump($aGroupedSearches);
907 if ($this->bReverseInPlan)
909 $aCopyGroupedSearches = $aGroupedSearches;
910 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
912 foreach($aSearches as $iSearch => $aSearch)
914 if (sizeof($aSearch['aAddress']))
916 $iReverseItem = array_pop($aSearch['aAddress']);
917 if (isset($aPossibleMainWordIDs[$iReverseItem]))
919 $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
920 $aSearch['aName'] = array($iReverseItem);
921 $aGroupedSearches[$iGroup][] = $aSearch;
923 //$aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
924 //$aGroupedSearches[$iGroup][] = $aReverseSearch;
930 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
932 $aCopyGroupedSearches = $aGroupedSearches;
933 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
935 foreach($aSearches as $iSearch => $aSearch)
937 $aReductionsList = array($aSearch['aAddress']);
938 $iSearchRank = $aSearch['iSearchRank'];
939 while(sizeof($aReductionsList) > 0)
942 if ($iSearchRank > iMaxRank) break 3;
943 $aNewReductionsList = array();
944 foreach($aReductionsList as $aReductionsWordList)
946 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
948 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
949 $aReverseSearch = $aSearch;
950 $aSearch['aAddress'] = $aReductionsWordListResult;
951 $aSearch['iSearchRank'] = $iSearchRank;
952 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
953 if (sizeof($aReductionsWordListResult) > 0)
955 $aNewReductionsList[] = $aReductionsWordListResult;
959 $aReductionsList = $aNewReductionsList;
963 ksort($aGroupedSearches);
966 // Filter out duplicate searches
967 $aSearchHash = array();
968 foreach($aGroupedSearches as $iGroup => $aSearches)
970 foreach($aSearches as $iSearch => $aSearch)
972 $sHash = serialize($aSearch);
973 if (isset($aSearchHash[$sHash]))
975 unset($aGroupedSearches[$iGroup][$iSearch]);
976 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
980 $aSearchHash[$sHash] = 1;
985 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
989 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
992 foreach($aSearches as $aSearch)
996 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
997 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1000 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1002 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1004 // Just looking for a country by code - look it up
1005 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1007 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1008 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1009 $sSQL .= " order by st_area(geometry) desc limit 1";
1010 if (CONST_Debug) var_dump($sSQL);
1011 $aPlaceIDs = $this->oDB->getCol($sSQL);
1016 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1017 if (!$aSearch['sClass']) continue;
1018 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1019 if ($this->oDB->getOne($sSQL))
1021 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1022 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1023 $sSQL .= " where st_contains($sViewboxSmallSQL, ct.centroid)";
1024 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1025 if (sizeof($this->aExcludePlaceIDs))
1027 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1029 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1030 $sSQL .= " limit $this->iLimit";
1031 if (CONST_Debug) var_dump($sSQL);
1032 $aPlaceIDs = $this->oDB->getCol($sSQL);
1034 // If excluded place IDs are given, it is fair to assume that
1035 // there have been results in the small box, so no further
1036 // expansion in that case.
1037 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs))
1039 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1040 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1041 $sSQL .= " where st_contains($sViewboxLargeSQL, ct.centroid)";
1042 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1043 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1044 $sSQL .= " limit $this->iLimit";
1045 if (CONST_Debug) var_dump($sSQL);
1046 $aPlaceIDs = $this->oDB->getCol($sSQL);
1051 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1052 $sSQL .= " and st_contains($sViewboxSmallSQL, geometry) and linked_place_id is null";
1053 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1054 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1055 $sSQL .= " limit $this->iLimit";
1056 if (CONST_Debug) var_dump($sSQL);
1057 $aPlaceIDs = $this->oDB->getCol($sSQL);
1063 $aPlaceIDs = array();
1065 // First we need a position, either aName or fLat or both
1069 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1070 // they might be right - but they are just too darned expensive to run
1071 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1072 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1073 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1075 // For infrequent name terms disable index usage for address
1076 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1077 sizeof($aSearch['aName']) == 1 &&
1078 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1080 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1084 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1085 if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1088 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1089 if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank between 16 and 27";
1090 if ($aSearch['fLon'] && $aSearch['fLat'])
1092 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1093 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1095 if (sizeof($this->aExcludePlaceIDs))
1097 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1099 if ($sCountryCodesSQL)
1101 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1104 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $sViewboxSmallSQL";
1105 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1107 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1108 if ($sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1109 if ($sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1110 $aOrder[] = "$sImportanceSQL DESC";
1111 if (sizeof($aSearch['aFullNameAddress']))
1113 $aOrder[] = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) DESC';
1116 if (sizeof($aTerms))
1118 $sSQL = "select place_id";
1119 $sSQL .= " from search_name";
1120 $sSQL .= " where ".join(' and ',$aTerms);
1121 $sSQL .= " order by ".join(', ',$aOrder);
1122 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1123 $sSQL .= " limit 50";
1124 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1125 $sSQL .= " limit 1";
1127 $sSQL .= " limit ".$this->iLimit;
1129 if (CONST_Debug) { var_dump($sSQL); }
1130 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1131 if (PEAR::IsError($aViewBoxPlaceIDs))
1133 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1135 //var_dump($aViewBoxPlaceIDs);
1136 // Did we have an viewbox matches?
1137 $aPlaceIDs = array();
1138 $bViewBoxMatch = false;
1139 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1141 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1142 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1143 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1144 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1145 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1148 //var_Dump($aPlaceIDs);
1151 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1153 $aRoadPlaceIDs = $aPlaceIDs;
1154 $sPlaceIDs = join(',',$aPlaceIDs);
1156 // Now they are indexed look for a house attached to a street we found
1157 $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-,/ ]',$aSearch['sHouseNumber']).'\\\\M';
1158 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
1159 if (sizeof($this->aExcludePlaceIDs))
1161 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1163 $sSQL .= " limit $this->iLimit";
1164 if (CONST_Debug) var_dump($sSQL);
1165 $aPlaceIDs = $this->oDB->getCol($sSQL);
1167 // If not try the aux fallback table
1168 if (!sizeof($aPlaceIDs))
1170 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1171 if (sizeof($this->aExcludePlaceIDs))
1173 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1175 //$sSQL .= " limit $this->iLimit";
1176 if (CONST_Debug) var_dump($sSQL);
1177 $aPlaceIDs = $this->oDB->getCol($sSQL);
1180 if (!sizeof($aPlaceIDs))
1182 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1183 if (sizeof($this->aExcludePlaceIDs))
1185 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1187 //$sSQL .= " limit $this->iLimit";
1188 if (CONST_Debug) var_dump($sSQL);
1189 $aPlaceIDs = $this->oDB->getCol($sSQL);
1192 // Fallback to the road
1193 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1195 $aPlaceIDs = $aRoadPlaceIDs;
1200 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1202 $sPlaceIDs = join(',',$aPlaceIDs);
1203 $aClassPlaceIDs = array();
1205 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1207 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1208 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1209 $sSQL .= " and linked_place_id is null";
1210 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1211 $sSQL .= " order by rank_search asc limit $this->iLimit";
1212 if (CONST_Debug) var_dump($sSQL);
1213 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1216 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1218 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1219 $bCacheTable = $this->oDB->getOne($sSQL);
1221 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1223 if (CONST_Debug) var_dump($sSQL);
1224 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1226 // For state / country level searches the normal radius search doesn't work very well
1227 $sPlaceGeom = false;
1228 if ($this->iMaxRank < 9 && $bCacheTable)
1230 // Try and get a polygon to search in instead
1231 $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";
1232 if (CONST_Debug) var_dump($sSQL);
1233 $sPlaceGeom = $this->oDB->getOne($sSQL);
1242 $this->iMaxRank += 5;
1243 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1244 if (CONST_Debug) var_dump($sSQL);
1245 $aPlaceIDs = $this->oDB->getCol($sSQL);
1246 $sPlaceIDs = join(',',$aPlaceIDs);
1249 if ($sPlaceIDs || $sPlaceGeom)
1255 // More efficient - can make the range bigger
1259 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1260 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1261 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1263 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1264 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1267 $sSQL .= ",placex as f where ";
1268 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1273 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1275 if (sizeof($this->aExcludePlaceIDs))
1277 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1279 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1280 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1281 if ($iOffset) $sSQL .= " offset $iOffset";
1282 $sSQL .= " limit $this->iLimit";
1283 if (CONST_Debug) var_dump($sSQL);
1284 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1288 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1291 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1292 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1294 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1295 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1296 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1297 if (sizeof($this->aExcludePlaceIDs))
1299 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1301 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1302 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1303 if ($iOffset) $sSQL .= " offset $iOffset";
1304 $sSQL .= " limit $this->iLimit";
1305 if (CONST_Debug) var_dump($sSQL);
1306 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1311 $aPlaceIDs = $aClassPlaceIDs;
1317 if (PEAR::IsError($aPlaceIDs))
1319 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1322 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1324 foreach($aPlaceIDs as $iPlaceID)
1326 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1328 if ($iQueryLoop > 20) break;
1331 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1333 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1334 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1335 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1336 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1337 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1338 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1339 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1340 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1342 if (CONST_Debug) var_dump($sSQL);
1343 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1347 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1348 if ($iGroupLoop > 4) break;
1349 if ($iQueryLoop > 30) break;
1352 // Did we find anything?
1353 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1355 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1361 // Just interpret as a reverse geocode
1362 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1364 $aSearchResults = $this->getDetails(array($iPlaceID));
1366 $aSearchResults = array();
1370 if (!sizeof($aSearchResults))
1375 $aClassType = getClassTypesWithImportance();
1376 $aRecheckWords = preg_split('/\b/u',$sQuery);
1377 foreach($aRecheckWords as $i => $sWord)
1379 if (!$sWord) unset($aRecheckWords[$i]);
1382 foreach($aSearchResults as $iResNum => $aResult)
1384 if (CONST_Search_AreaPolygons)
1386 // Get the bounding box and outline polygon
1387 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1388 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1389 $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1390 $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1391 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1392 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1393 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1394 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1395 $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1396 $aPointPolygon = $this->oDB->getRow($sSQL);
1397 if (PEAR::IsError($aPointPolygon))
1399 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1402 if ($aPointPolygon['place_id'])
1404 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1405 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1406 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1407 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1409 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1411 $aResult['lat'] = $aPointPolygon['centrelat'];
1412 $aResult['lon'] = $aPointPolygon['centrelon'];
1415 if ($this->bIncludePolygonAsPoints)
1417 // Translate geometary string to point array
1418 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1420 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1422 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1424 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1426 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1429 $iSteps = ($fRadius * 40000)^2;
1430 $fStepSize = (2*pi())/$iSteps;
1431 $aPolyPoints = array();
1432 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1434 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1436 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1437 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1438 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1439 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1443 // Output data suitable for display (points and a bounding box)
1444 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1446 $aResult['aPolyPoints'] = array();
1447 foreach($aPolyPoints as $aPoint)
1449 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1452 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1456 if ($aResult['extra_place'] == 'city')
1458 $aResult['class'] = 'place';
1459 $aResult['type'] = 'city';
1460 $aResult['rank_search'] = 16;
1463 if (!isset($aResult['aBoundingBox']))
1466 $fDiameter = 0.0001;
1468 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1469 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1471 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1473 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1474 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1476 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1478 $fRadius = $fDiameter / 2;
1480 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1481 $fStepSize = (2*pi())/$iSteps;
1482 $aPolyPoints = array();
1483 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1485 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1487 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1488 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1489 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1490 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1492 // Output data suitable for display (points and a bounding box)
1493 if ($this->bIncludePolygonAsPoints)
1495 $aResult['aPolyPoints'] = array();
1496 foreach($aPolyPoints as $aPoint)
1498 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1501 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1504 // Is there an icon set for this type of result?
1505 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1506 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1508 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1511 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1512 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1514 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1516 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1517 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1519 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1522 if ($this->bIncludeAddressDetails)
1524 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1525 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1527 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1531 // Adjust importance for the number of exact string matches in the result
1532 $aResult['importance'] = max(0.001,$aResult['importance']);
1534 $sAddress = $aResult['langaddress'];
1535 foreach($aRecheckWords as $i => $sWord)
1537 if (stripos($sAddress, $sWord)!==false) $iCountWords++;
1540 $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
1542 $aResult['name'] = $aResult['langaddress'];
1543 $aResult['foundorder'] = -$aResult['addressimportance'];
1544 $aSearchResults[$iResNum] = $aResult;
1546 uasort($aSearchResults, 'byImportance');
1548 $aOSMIDDone = array();
1549 $aClassTypeNameDone = array();
1550 $aToFilter = $aSearchResults;
1551 $aSearchResults = array();
1554 foreach($aToFilter as $iResNum => $aResult)
1556 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1557 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1560 $fLat = $aResult['lat'];
1561 $fLon = $aResult['lon'];
1562 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1565 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1566 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1568 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1569 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1570 $aSearchResults[] = $aResult;
1573 // Absolute limit on number of results
1574 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1577 return $aSearchResults;
1586 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
1588 $aPoints = explode(',',$_GET['route']);
1589 if (sizeof($aPoints) % 2 != 0)
1591 userError("Uneven number of points");
1594 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
1595 $fPrevCoord = false;