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 = true;
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 $sViewboxSmallSQL = false;
30 protected $sViewboxLargeSQL = false;
31 protected $aRoutePoints = false;
33 protected $iMaxRank = 20;
34 protected $iMinAddressRank = 0;
35 protected $iMaxAddressRank = 30;
36 protected $aAddressRankList = array();
38 protected $sAllowedTypesSQLList = false;
40 protected $sQuery = false;
41 protected $aStructuredQuery = false;
43 function Geocode(&$oDB)
48 function setReverseInPlan($bReverse)
50 $this->bReverseInPlan = $bReverse;
53 function setLanguagePreference($aLangPref)
55 $this->aLangPrefOrder = $aLangPref;
58 function setIncludeAddressDetails($bAddressDetails = true)
60 $this->bIncludeAddressDetails = (bool)$bAddressDetails;
63 function getIncludeAddressDetails()
65 return $this->bIncludeAddressDetails;
68 function setIncludePolygonAsPoints($b = true)
70 $this->bIncludePolygonAsPoints = $b;
73 function getIncludePolygonAsPoints()
75 return $this->bIncludePolygonAsPoints;
78 function setIncludePolygonAsText($b = true)
80 $this->bIncludePolygonAsText = $b;
83 function getIncludePolygonAsText()
85 return $this->bIncludePolygonAsText;
88 function setIncludePolygonAsGeoJSON($b = true)
90 $this->bIncludePolygonAsGeoJSON = $b;
93 function setIncludePolygonAsKML($b = true)
95 $this->bIncludePolygonAsKML = $b;
98 function setIncludePolygonAsSVG($b = true)
100 $this->bIncludePolygonAsSVG = $b;
103 function setDeDupe($bDeDupe = true)
105 $this->bDeDupe = (bool)$bDeDupe;
108 function setLimit($iLimit = 10)
110 if ($iLimit > 50) $iLimit = 50;
111 if ($iLimit < 1) $iLimit = 1;
113 $this->iFinalLimit = $iLimit;
114 $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
117 function setOffset($iOffset = 0)
119 $this->iOffset = $iOffset;
122 function setExcludedPlaceIDs($a)
124 // TODO: force to int
125 $this->aExcludePlaceIDs = $a;
128 function getExcludedPlaceIDs()
130 return $this->aExcludePlaceIDs;
133 function setBounded($bBoundedSearch = true)
135 $this->bBoundedSearch = (bool)$bBoundedSearch;
138 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
140 $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
143 function getViewBoxString()
145 if (!$this->aViewBox) return null;
146 return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
149 function setRoute($aRoutePoints)
151 $this->aRoutePoints = $aRoutePoints;
154 function setFeatureType($sFeatureType)
156 switch($sFeatureType)
159 $this->setRankRange(4, 4);
162 $this->setRankRange(8, 8);
165 $this->setRankRange(14, 16);
168 $this->setRankRange(8, 20);
173 function setRankRange($iMin, $iMax)
175 $this->iMinAddressRank = (int)$iMin;
176 $this->iMaxAddressRank = (int)$iMax;
179 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
181 $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
184 function setCountryCodesList($aCountryCodes)
186 $this->aCountryCodes = $aCountryCodes;
189 function setQuery($sQueryString)
191 $this->sQuery = $sQueryString;
192 $this->aStructuredQuery = false;
195 function getQueryString()
197 return $this->sQuery;
200 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
202 $sValue = trim($sValue);
203 if (!$sValue) return false;
204 $this->aStructuredQuery[$sKey] = $sValue;
205 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
207 $this->iMinAddressRank = $iNewMinAddressRank;
208 $this->iMaxAddressRank = $iNewMaxAddressRank;
210 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
214 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
216 $this->sQuery = false;
218 $this->aStructuredQuery = array();
219 $this->sAllowedTypesSQLList = '';
221 $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
222 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
223 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
224 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
225 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
226 $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
227 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
229 if (sizeof($this->aStructuredQuery) > 0)
231 $this->sQuery = join(', ', $this->aStructuredQuery);
232 if ($this->iMaxAddressRank < 30)
234 $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
240 function getDetails($aPlaceIDs)
242 if (sizeof($aPlaceIDs) == 0) return array();
244 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
246 // Get the details for display (is this a redundant extra step?)
247 $sPlaceIDs = join(',',$aPlaceIDs);
249 $sImportanceSQL = '';
250 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
251 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
253 $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,";
254 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
255 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
256 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
257 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
258 $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
259 $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, ";
260 $sSQL .= "(extratags->'place') as extra_place ";
261 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
262 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
263 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
264 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
266 if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
267 $sSQL .= "and linked_place_id is null ";
268 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
269 if (!$this->bDeDupe) $sSQL .= ",place_id";
270 $sSQL .= ",langaddress ";
271 $sSQL .= ",placename ";
273 $sSQL .= ",extratags->'place' ";
275 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
278 $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,";
279 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
280 $sSQL .= "null as placename,";
281 $sSQL .= "null as ref,";
282 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
283 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
284 $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, ";
285 $sSQL .= "null as extra_place ";
286 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
287 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
288 $sSQL .= "group by place_id";
289 if (!$this->bDeDupe) $sSQL .= ",place_id ";
292 $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,";
293 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
294 $sSQL .= "null as placename,";
295 $sSQL .= "null as ref,";
296 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
297 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
298 $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, ";
299 $sSQL .= "null as extra_place ";
300 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
301 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
302 $sSQL .= "group by place_id";
303 if (!$this->bDeDupe) $sSQL .= ",place_id";
304 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
308 $sSQL .= " order by importance desc";
309 if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
310 $aSearchResults = $this->oDB->getAll($sSQL);
312 if (PEAR::IsError($aSearchResults))
314 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
317 return $aSearchResults;
322 if (!$this->sQuery && !$this->aStructuredQuery) return false;
324 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
326 $sCountryCodesSQL = false;
327 if ($this->aCountryCodes && sizeof($this->aCountryCodes))
329 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
332 // Hack to make it handle "new york, ny" (and variants) correctly
333 //$sQuery = str_ireplace(array('New York, ny','new york, new york', 'New York ny','new york new york'), 'new york city, ny', $this->sQuery);
334 $sQuery = $this->sQuery;
336 // Conflicts between US state abreviations and various words for 'the' in different languages
337 if (isset($this->aLangPrefOrder['name:en']))
339 $sQuery = preg_replace('/,\s*il\s*(,|$)/',', illinois\1', $sQuery);
340 $sQuery = preg_replace('/,\s*al\s*(,|$)/',', alabama\1', $sQuery);
341 $sQuery = preg_replace('/,\s*la\s*(,|$)/',', louisiana\1', $sQuery);
346 $bBoundingBoxSearch = false;
349 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
350 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
351 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
352 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
353 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
354 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
356 $this->sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$this->aViewBox[0].",".(float)$this->aViewBox[1]."),ST_Point(".(float)$this->aViewBox[2].",".(float)$this->aViewBox[3].")),4326)";
357 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
358 $bBoundingBoxSearch = $this->bBoundedSearch;
362 if ($this->aRoutePoints)
364 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
366 foreach($this->aRouteaPoints as $aPoint)
368 if (!$bFirst) $sViewboxCentreSQL .= ",";
369 $sViewboxCentreSQL .= $aPoint[1].' '.$aPoint[0];
371 $sViewboxCentreSQL .= ")'::geometry,4326)";
373 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
374 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
375 if (PEAR::isError($this->sViewboxSmallSQL))
377 failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
379 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
381 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
382 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
383 if (PEAR::isError($this->sViewboxLargeSQL))
385 failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
387 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
388 $bBoundingBoxSearch = $this->bBoundedSearch;
391 // Do we have anything that looks like a lat/lon pair?
392 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
394 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
395 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
396 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
398 $this->setNearPoint(array($fQueryLat, $fQueryLon));
399 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
402 elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
404 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
405 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
406 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
408 $this->setNearPoint(array($fQueryLat, $fQueryLon));
409 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
412 elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
414 $fQueryLat = $aData[2];
415 $fQueryLon = $aData[3];
416 if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
418 $this->setNearPoint(array($fQueryLat, $fQueryLon));
419 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
423 $aSearchResults = array();
424 if ($sQuery || $this->aStructuredQuery)
426 // Start with a blank search
428 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
429 'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
430 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
433 // Do we have a radius search?
434 $sNearPointSQL = false;
435 if ($this->aNearPoint)
437 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
438 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
439 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
440 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
443 // Any 'special' terms in the search?
444 $bSpecialTerms = false;
445 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
446 $aSpecialTerms = array();
447 foreach($aSpecialTermsRaw as $aSpecialTerm)
449 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
450 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
453 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
454 $aSpecialTerms = array();
455 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
457 $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
458 unset($aStructuredQuery['amenity']);
460 foreach($aSpecialTermsRaw as $aSpecialTerm)
462 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
463 $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
464 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
465 $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';
466 if (CONST_Debug) var_Dump($sSQL);
467 $aSearchWords = $this->oDB->getAll($sSQL);
468 $aNewSearches = array();
469 foreach($aSearches as $aSearch)
471 foreach($aSearchWords as $aSearchTerm)
473 $aNewSearch = $aSearch;
474 if ($aSearchTerm['country_code'])
476 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
477 $aNewSearches[] = $aNewSearch;
478 $bSpecialTerms = true;
480 if ($aSearchTerm['class'])
482 $aNewSearch['sClass'] = $aSearchTerm['class'];
483 $aNewSearch['sType'] = $aSearchTerm['type'];
484 $aNewSearches[] = $aNewSearch;
485 $bSpecialTerms = true;
489 $aSearches = $aNewSearches;
492 // Split query into phrases
493 // Commas are used to reduce the search space by indicating where phrases split
494 if ($this->aStructuredQuery)
496 $aPhrases = $this->aStructuredQuery;
497 $bStructuredPhrases = true;
501 $aPhrases = explode(',',$sQuery);
502 $bStructuredPhrases = false;
505 // Convert each phrase to standard form
506 // Create a list of standard words
507 // Get all 'sets' of words
508 // Generate a complete list of all
510 foreach($aPhrases as $iPhrase => $sPhrase)
512 $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
513 if (PEAR::isError($aPhrase))
515 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
516 if (CONST_Debug) var_dump($aPhrase);
519 if (trim($aPhrase['string']))
521 $aPhrases[$iPhrase] = $aPhrase;
522 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
523 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
524 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
528 unset($aPhrases[$iPhrase]);
532 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
533 $aPhraseTypes = array_keys($aPhrases);
534 $aPhrases = array_values($aPhrases);
536 if (sizeof($aTokens))
538 // Check which tokens we have, get the ID numbers
539 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
540 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
542 if (CONST_Debug) var_Dump($sSQL);
544 $aValidTokens = array();
545 if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
546 else $aDatabaseWords = array();
547 if (PEAR::IsError($aDatabaseWords))
549 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
551 $aPossibleMainWordIDs = array();
552 $aWordFrequencyScores = array();
553 foreach($aDatabaseWords as $aToken)
555 // Very special case - require 2 letter country param to match the country code found
556 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
557 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
562 if (isset($aValidTokens[$aToken['word_token']]))
564 $aValidTokens[$aToken['word_token']][] = $aToken;
568 $aValidTokens[$aToken['word_token']] = array($aToken);
570 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
571 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
573 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
575 // Try and calculate GB postcodes we might be missing
576 foreach($aTokens as $sToken)
578 // Source of gb postcodes is now definitive - always use
579 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
581 if (substr($aData[1],-2,1) != ' ')
583 $aData[0] = substr($aData[0],0,strlen($aData[1]-1)).' '.substr($aData[0],strlen($aData[1]-1));
584 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
586 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
587 if ($aGBPostcodeLocation)
589 $aValidTokens[$sToken] = $aGBPostcodeLocation;
592 // US ZIP+4 codes - if there is no token,
593 // merge in the 5-digit ZIP code
594 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
596 if (isset($aValidTokens[$aData[1]]))
598 foreach($aValidTokens[$aData[1]] as $aToken)
600 if (!$aToken['class'])
602 if (isset($aValidTokens[$sToken]))
604 $aValidTokens[$sToken][] = $aToken;
608 $aValidTokens[$sToken] = array($aToken);
616 foreach($aTokens as $sToken)
618 // Unknown single word token with a number - assume it is a house number
619 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
621 $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
625 // Any words that have failed completely?
628 // Start the search process
629 $aResultPlaceIDs = array();
632 Calculate all searches using aValidTokens i.e.
633 'Wodsworth Road, Sheffield' =>
637 0 1 (wodsworth)(road)
640 Score how good the search is so they can be ordered
642 foreach($aPhrases as $iPhrase => $sPhrase)
644 $aNewPhraseSearches = array();
645 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
646 else $sPhraseType = '';
648 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
650 // Too many permutations - too expensive
651 if ($iWordSet > 120) break;
653 $aWordsetSearches = $aSearches;
655 // Add all words from this wordset
656 foreach($aWordset as $iToken => $sToken)
658 //echo "<br><b>$sToken</b>";
659 $aNewWordsetSearches = array();
661 foreach($aWordsetSearches as $aCurrentSearch)
664 //var_dump($aCurrentSearch);
667 // If the token is valid
668 if (isset($aValidTokens[' '.$sToken]))
670 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
672 $aSearch = $aCurrentSearch;
673 $aSearch['iSearchRank']++;
674 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
676 if ($aSearch['sCountryCode'] === false)
678 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
679 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
680 // If reverse order is enabled, it may appear at the beginning as well.
681 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)) &&
682 (!$this->bReverseInPlan || $iToken > 0 || $iPhrase > 0))
684 $aSearch['iSearchRank'] += 5;
686 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
689 elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
691 if ($aSearch['fLat'] === '')
693 $aSearch['fLat'] = $aSearchTerm['lat'];
694 $aSearch['fLon'] = $aSearchTerm['lon'];
695 $aSearch['fRadius'] = $aSearchTerm['radius'];
696 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
699 elseif ($sPhraseType == 'postalcode')
701 // 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
702 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
704 // If we already have a name try putting the postcode first
705 if (sizeof($aSearch['aName']))
707 $aNewSearch = $aSearch;
708 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
709 $aNewSearch['aName'] = array();
710 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
711 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
714 if (sizeof($aSearch['aName']))
716 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
718 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
722 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
723 $aSearch['iSearchRank'] += 1000; // skip;
728 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
729 //$aSearch['iNamePhrase'] = $iPhrase;
731 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
735 elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
737 if ($aSearch['sHouseNumber'] === '')
739 $aSearch['sHouseNumber'] = $sToken;
740 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
742 // Fall back to not searching for this item (better than nothing)
743 $aSearch = $aCurrentSearch;
744 $aSearch['iSearchRank'] += 1;
745 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
749 elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
751 if ($aSearch['sClass'] === '')
753 $aSearch['sOperator'] = $aSearchTerm['operator'];
754 $aSearch['sClass'] = $aSearchTerm['class'];
755 $aSearch['sType'] = $aSearchTerm['type'];
756 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
757 else $aSearch['sOperator'] = 'near'; // near = in for the moment
758 if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
760 // Do we have a shortcut id?
761 if ($aSearch['sOperator'] == 'name')
763 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
764 if ($iAmenityID = $this->oDB->getOne($sSQL))
766 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
767 $aSearch['aName'][$iAmenityID] = $iAmenityID;
768 $aSearch['sClass'] = '';
769 $aSearch['sType'] = '';
772 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
775 elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
777 if (sizeof($aSearch['aName']))
779 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
781 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
785 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
786 $aSearch['iSearchRank'] += 1000; // skip;
791 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
792 //$aSearch['iNamePhrase'] = $iPhrase;
794 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
798 if (isset($aValidTokens[$sToken]))
800 // Allow searching for a word - but at extra cost
801 foreach($aValidTokens[$sToken] as $aSearchTerm)
803 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
805 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strlen($sToken) >= 4)
807 $aSearch = $aCurrentSearch;
808 $aSearch['iSearchRank'] += 1;
809 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
811 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
812 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
814 elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
816 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
818 if (empty($aSearchTermToken['country_code'])
819 && empty($aSearchTermToken['lat'])
820 && empty($aSearchTermToken['class']))
822 $aSearch = $aCurrentSearch;
823 $aSearch['iSearchRank'] += 1;
824 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
825 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
831 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
832 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
836 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
838 $aSearch = $aCurrentSearch;
839 $aSearch['iSearchRank'] += 2;
840 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
841 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
842 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
844 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
845 $aSearch['iNamePhrase'] = $iPhrase;
846 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
853 // Allow skipping a word - but at EXTREAM cost
854 //$aSearch = $aCurrentSearch;
855 //$aSearch['iSearchRank']+=100;
856 //$aNewWordsetSearches[] = $aSearch;
860 usort($aNewWordsetSearches, 'bySearchRank');
861 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
863 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
865 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
866 usort($aNewPhraseSearches, 'bySearchRank');
868 $aSearchHash = array();
869 foreach($aNewPhraseSearches as $iSearch => $aSearch)
871 $sHash = serialize($aSearch);
872 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
873 else $aSearchHash[$sHash] = 1;
876 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
879 // Re-group the searches by their score, junk anything over 20 as just not worth trying
880 $aGroupedSearches = array();
881 foreach($aNewPhraseSearches as $aSearch)
883 if ($aSearch['iSearchRank'] < $this->iMaxRank)
885 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
886 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
889 ksort($aGroupedSearches);
892 $aSearches = array();
893 foreach($aGroupedSearches as $iScore => $aNewSearches)
895 $iSearchCount += sizeof($aNewSearches);
896 $aSearches = array_merge($aSearches, $aNewSearches);
897 if ($iSearchCount > 50) break;
900 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
907 // Re-group the searches by their score, junk anything over 20 as just not worth trying
908 $aGroupedSearches = array();
909 foreach($aSearches as $aSearch)
911 if ($aSearch['iSearchRank'] < $this->iMaxRank)
913 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
914 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
917 ksort($aGroupedSearches);
920 if (CONST_Debug) var_Dump($aGroupedSearches);
922 if ($this->bReverseInPlan)
924 $aCopyGroupedSearches = $aGroupedSearches;
925 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
927 foreach($aSearches as $iSearch => $aSearch)
929 if (sizeof($aSearch['aAddress']))
931 $iReverseItem = array_pop($aSearch['aAddress']);
932 if (isset($aPossibleMainWordIDs[$iReverseItem]))
934 $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
935 $aSearch['aName'] = array($iReverseItem);
936 $aGroupedSearches[$iGroup][] = $aSearch;
938 //$aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
939 //$aGroupedSearches[$iGroup][] = $aReverseSearch;
945 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
947 $aCopyGroupedSearches = $aGroupedSearches;
948 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
950 foreach($aSearches as $iSearch => $aSearch)
952 $aReductionsList = array($aSearch['aAddress']);
953 $iSearchRank = $aSearch['iSearchRank'];
954 while(sizeof($aReductionsList) > 0)
957 if ($iSearchRank > iMaxRank) break 3;
958 $aNewReductionsList = array();
959 foreach($aReductionsList as $aReductionsWordList)
961 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
963 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
964 $aReverseSearch = $aSearch;
965 $aSearch['aAddress'] = $aReductionsWordListResult;
966 $aSearch['iSearchRank'] = $iSearchRank;
967 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
968 if (sizeof($aReductionsWordListResult) > 0)
970 $aNewReductionsList[] = $aReductionsWordListResult;
974 $aReductionsList = $aNewReductionsList;
978 ksort($aGroupedSearches);
981 // Filter out duplicate searches
982 $aSearchHash = array();
983 foreach($aGroupedSearches as $iGroup => $aSearches)
985 foreach($aSearches as $iSearch => $aSearch)
987 $sHash = serialize($aSearch);
988 if (isset($aSearchHash[$sHash]))
990 unset($aGroupedSearches[$iGroup][$iSearch]);
991 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
995 $aSearchHash[$sHash] = 1;
1000 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1004 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1007 foreach($aSearches as $aSearch)
1011 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1012 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1014 // No location term?
1015 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1017 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1019 // Just looking for a country by code - look it up
1020 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1022 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1023 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1024 $sSQL .= " order by st_area(geometry) desc limit 1";
1025 if (CONST_Debug) var_dump($sSQL);
1026 $aPlaceIDs = $this->oDB->getCol($sSQL);
1031 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1032 if (!$aSearch['sClass']) continue;
1033 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1034 if ($this->oDB->getOne($sSQL))
1036 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1037 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1038 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1039 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1040 if (sizeof($this->aExcludePlaceIDs))
1042 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1044 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1045 $sSQL .= " limit $this->iLimit";
1046 if (CONST_Debug) var_dump($sSQL);
1047 $aPlaceIDs = $this->oDB->getCol($sSQL);
1049 // If excluded place IDs are given, it is fair to assume that
1050 // there have been results in the small box, so no further
1051 // expansion in that case.
1052 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs))
1054 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1055 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1056 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1057 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1058 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1059 $sSQL .= " limit $this->iLimit";
1060 if (CONST_Debug) var_dump($sSQL);
1061 $aPlaceIDs = $this->oDB->getCol($sSQL);
1066 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1067 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1068 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1069 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1070 $sSQL .= " limit $this->iLimit";
1071 if (CONST_Debug) var_dump($sSQL);
1072 $aPlaceIDs = $this->oDB->getCol($sSQL);
1078 $aPlaceIDs = array();
1080 // First we need a position, either aName or fLat or both
1084 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1085 // they might be right - but they are just too darned expensive to run
1086 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1087 //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1088 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1090 // For infrequent name terms disable index usage for address
1091 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1092 sizeof($aSearch['aName']) == 1 &&
1093 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1095 //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1096 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1100 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1101 //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1104 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1105 if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank between 16 and 27";
1106 if ($aSearch['fLon'] && $aSearch['fLat'])
1108 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1109 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1111 if (sizeof($this->aExcludePlaceIDs))
1113 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1115 if ($sCountryCodesSQL)
1117 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1120 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1121 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1123 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1124 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1125 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1126 $aOrder[] = "$sImportanceSQL DESC";
1127 if (sizeof($aSearch['aFullNameAddress']))
1129 $aOrder[] = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) DESC';
1132 if (sizeof($aTerms))
1134 $sSQL = "select place_id";
1135 $sSQL .= " from search_name";
1136 $sSQL .= " where ".join(' and ',$aTerms);
1137 $sSQL .= " order by ".join(', ',$aOrder);
1138 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1139 $sSQL .= " limit 50";
1140 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1141 $sSQL .= " limit 1";
1143 $sSQL .= " limit ".$this->iLimit;
1145 if (CONST_Debug) { var_dump($sSQL); }
1146 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1147 if (PEAR::IsError($aViewBoxPlaceIDs))
1149 failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1151 //var_dump($aViewBoxPlaceIDs);
1152 // Did we have an viewbox matches?
1153 $aPlaceIDs = array();
1154 $bViewBoxMatch = false;
1155 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1157 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1158 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1159 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1160 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1161 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1164 //var_Dump($aPlaceIDs);
1167 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1169 $aRoadPlaceIDs = $aPlaceIDs;
1170 $sPlaceIDs = join(',',$aPlaceIDs);
1172 // Now they are indexed look for a house attached to a street we found
1173 $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-,/ ]',$aSearch['sHouseNumber']).'\\\\M';
1174 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
1175 if (sizeof($this->aExcludePlaceIDs))
1177 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1179 $sSQL .= " limit $this->iLimit";
1180 if (CONST_Debug) var_dump($sSQL);
1181 $aPlaceIDs = $this->oDB->getCol($sSQL);
1183 // If not try the aux fallback table
1185 if (!sizeof($aPlaceIDs))
1187 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1188 if (sizeof($this->aExcludePlaceIDs))
1190 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1192 //$sSQL .= " limit $this->iLimit";
1193 if (CONST_Debug) var_dump($sSQL);
1194 $aPlaceIDs = $this->oDB->getCol($sSQL);
1198 if (!sizeof($aPlaceIDs))
1200 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1201 if (sizeof($this->aExcludePlaceIDs))
1203 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1205 //$sSQL .= " limit $this->iLimit";
1206 if (CONST_Debug) var_dump($sSQL);
1207 $aPlaceIDs = $this->oDB->getCol($sSQL);
1210 // Fallback to the road
1211 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1213 $aPlaceIDs = $aRoadPlaceIDs;
1218 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1220 $sPlaceIDs = join(',',$aPlaceIDs);
1221 $aClassPlaceIDs = array();
1223 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1225 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1226 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1227 $sSQL .= " and linked_place_id is null";
1228 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1229 $sSQL .= " order by rank_search asc limit $this->iLimit";
1230 if (CONST_Debug) var_dump($sSQL);
1231 $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1234 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1236 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1237 $bCacheTable = $this->oDB->getOne($sSQL);
1239 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1241 if (CONST_Debug) var_dump($sSQL);
1242 $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1244 // For state / country level searches the normal radius search doesn't work very well
1245 $sPlaceGeom = false;
1246 if ($this->iMaxRank < 9 && $bCacheTable)
1248 // Try and get a polygon to search in instead
1249 $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";
1250 if (CONST_Debug) var_dump($sSQL);
1251 $sPlaceGeom = $this->oDB->getOne($sSQL);
1260 $this->iMaxRank += 5;
1261 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1262 if (CONST_Debug) var_dump($sSQL);
1263 $aPlaceIDs = $this->oDB->getCol($sSQL);
1264 $sPlaceIDs = join(',',$aPlaceIDs);
1267 if ($sPlaceIDs || $sPlaceGeom)
1273 // More efficient - can make the range bigger
1277 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1278 else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1279 else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1281 $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1282 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1285 $sSQL .= ",placex as f where ";
1286 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1291 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1293 if (sizeof($this->aExcludePlaceIDs))
1295 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1297 if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1298 if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1299 if ($iOffset) $sSQL .= " offset $iOffset";
1300 $sSQL .= " limit $this->iLimit";
1301 if (CONST_Debug) var_dump($sSQL);
1302 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1306 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1309 if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1310 else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1312 $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1313 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1314 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1315 if (sizeof($this->aExcludePlaceIDs))
1317 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1319 if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1320 if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1321 if ($iOffset) $sSQL .= " offset $iOffset";
1322 $sSQL .= " limit $this->iLimit";
1323 if (CONST_Debug) var_dump($sSQL);
1324 $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1329 $aPlaceIDs = $aClassPlaceIDs;
1335 if (PEAR::IsError($aPlaceIDs))
1337 failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1340 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1342 foreach($aPlaceIDs as $iPlaceID)
1344 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1346 if ($iQueryLoop > 20) break;
1349 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1351 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1352 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1353 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1354 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1355 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1356 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1357 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1358 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1360 if (CONST_Debug) var_dump($sSQL);
1361 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1365 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1366 if ($iGroupLoop > 4) break;
1367 if ($iQueryLoop > 30) break;
1370 // Did we find anything?
1371 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1373 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1379 // Just interpret as a reverse geocode
1380 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1382 $aSearchResults = $this->getDetails(array($iPlaceID));
1384 $aSearchResults = array();
1388 if (!sizeof($aSearchResults))
1393 $aClassType = getClassTypesWithImportance();
1394 $aRecheckWords = preg_split('/\b/u',$sQuery);
1395 foreach($aRecheckWords as $i => $sWord)
1397 if (!$sWord) unset($aRecheckWords[$i]);
1400 foreach($aSearchResults as $iResNum => $aResult)
1402 if (CONST_Search_AreaPolygons)
1404 // Get the bounding box and outline polygon
1405 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1406 $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1407 $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1408 $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1409 if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1410 if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1411 if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1412 if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1413 $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1414 $aPointPolygon = $this->oDB->getRow($sSQL);
1415 if (PEAR::IsError($aPointPolygon))
1417 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1420 if ($aPointPolygon['place_id'])
1422 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1423 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1424 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1425 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1427 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1429 $aResult['lat'] = $aPointPolygon['centrelat'];
1430 $aResult['lon'] = $aPointPolygon['centrelon'];
1433 if ($this->bIncludePolygonAsPoints)
1435 // Translate geometary string to point array
1436 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1438 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1441 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1443 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1446 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1449 $iSteps = ($fRadius * 40000)^2;
1450 $fStepSize = (2*pi())/$iSteps;
1451 $aPolyPoints = array();
1452 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1454 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1456 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1457 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1458 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1459 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1463 // Output data suitable for display (points and a bounding box)
1464 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1466 $aResult['aPolyPoints'] = array();
1467 foreach($aPolyPoints as $aPoint)
1469 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1472 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1476 if ($aResult['extra_place'] == 'city')
1478 $aResult['class'] = 'place';
1479 $aResult['type'] = 'city';
1480 $aResult['rank_search'] = 16;
1483 if (!isset($aResult['aBoundingBox']))
1486 $fDiameter = 0.0001;
1488 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1489 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1491 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1493 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1494 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1496 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1498 $fRadius = $fDiameter / 2;
1500 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1501 $fStepSize = (2*pi())/$iSteps;
1502 $aPolyPoints = array();
1503 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1505 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1507 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1508 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1509 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1510 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1512 // Output data suitable for display (points and a bounding box)
1513 if ($this->bIncludePolygonAsPoints)
1515 $aResult['aPolyPoints'] = array();
1516 foreach($aPolyPoints as $aPoint)
1518 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1521 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1524 // Is there an icon set for this type of result?
1525 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1526 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1528 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1531 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1532 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1534 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1536 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1537 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1539 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1542 if ($this->bIncludeAddressDetails)
1544 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1545 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1547 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1551 // Adjust importance for the number of exact string matches in the result
1552 $aResult['importance'] = max(0.001,$aResult['importance']);
1554 $sAddress = $aResult['langaddress'];
1555 foreach($aRecheckWords as $i => $sWord)
1557 if (stripos($sAddress, $sWord)!==false) $iCountWords++;
1560 $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
1562 $aResult['name'] = $aResult['langaddress'];
1563 $aResult['foundorder'] = -$aResult['addressimportance'];
1564 $aSearchResults[$iResNum] = $aResult;
1566 uasort($aSearchResults, 'byImportance');
1568 $aOSMIDDone = array();
1569 $aClassTypeNameDone = array();
1570 $aToFilter = $aSearchResults;
1571 $aSearchResults = array();
1574 foreach($aToFilter as $iResNum => $aResult)
1576 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1577 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1580 $fLat = $aResult['lat'];
1581 $fLon = $aResult['lon'];
1582 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1585 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1586 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1588 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1589 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1590 $aSearchResults[] = $aResult;
1593 // Absolute limit on number of results
1594 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1597 return $aSearchResults;
1606 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
1608 $aPoints = explode(',',$_GET['route']);
1609 if (sizeof($aPoints) % 2 != 0)
1611 userError("Uneven number of points");
1614 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
1615 $fPrevCoord = false;