2 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
3 require_once(CONST_BasePath.'/lib/log.php');
5 ini_set('memory_limit', '200M');
9 $fLat = CONST_Default_Lat;
10 $fLon = CONST_Default_Lon;
11 $iZoom = CONST_Default_Zoom;
12 $bBoundingBoxSearch = isset($_GET['bounded'])?(bool)$_GET['bounded']:false;
13 $sOutputFormat = 'html';
14 $aSearchResults = array();
15 $aExcludePlaceIDs = array();
16 $sSuggestion = $sSuggestionURL = false;
17 $bDeDupe = isset($_GET['dedupe'])?(bool)$_GET['dedupe']:true;
18 $bReverseInPlan = false;
19 $iLimit = isset($_GET['limit'])?(int)$_GET['limit']:10;
20 $iOffset = isset($_GET['offset'])?(int)$_GET['offset']:0;
22 if ($iLimit > 100) $iLimit = 100;
24 $iMaxAddressRank = 30;
27 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
29 $sOutputFormat = $_GET['format'];
32 // Show / use polygons
33 $bShowPolygons = isset($_GET['polygon']) && $_GET['polygon'];
35 // Show address breakdown
36 $bShowAddressDetails = isset($_GET['addressdetails']) && $_GET['addressdetails'];
39 $aLangPrefOrder = getPrefferedLangauges();
40 if (isset($aLangPrefOrder['name:de'])) $bReverseInPlan = true;
41 if (isset($aLangPrefOrder['name:ru'])) $bReverseInPlan = true;
42 if (isset($aLangPrefOrder['name:ja'])) $bReverseInPlan = true;
44 $bReverseInPlan = true;
46 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
48 if (isset($_GET['exclude_place_ids']) && $_GET['exclude_place_ids'])
50 foreach(explode(',',$_GET['exclude_place_ids']) as $iExcludedPlaceID)
52 $iExcludedPlaceID = (int)$iExcludedPlaceID;
53 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
57 // Only certain ranks of feature
58 if (isset($_GET['featuretype']))
60 switch($_GET['featuretype'])
63 $iMinAddressRank = $iMaxAddressRank = 4;
66 $iMinAddressRank = $iMaxAddressRank = 8;
69 $iMinAddressRank = 14;
70 $iMaxAddressRank = 16;
74 $iMaxAddressRank = 20;
80 $sQuery = (isset($_GET['q'])?trim($_GET['q']):'');
81 if (!$sQuery && $_SERVER['PATH_INFO'] && $_SERVER['PATH_INFO'][0] == '/')
83 $sQuery = substr($_SERVER['PATH_INFO'], 1);
85 // reverse order of '/' seperated string
86 $aPhrases = explode('/', $sQuery);
87 $aPhrases = array_reverse($aPhrases);
88 $sQuery = join(', ',$aPhrases);
93 $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
95 // Hack to make it handle "new york, ny" (and variants) correctly
96 $sQuery = str_ireplace(array('New York, ny','new york, new york', 'New York ny','new york new york'), 'new york city, ny', $sQuery);
98 // If we have a view box create the SQL
99 // Small is the actual view box, Large is double (on each axis) that
100 $sViewboxCentreSQL = $sViewboxSmallSQL = $sViewboxLargeSQL = false;
101 if (isset($_GET['viewboxlbrt']) && $_GET['viewboxlbrt'])
103 $aCoOrdinatesLBRT = explode(',',$_GET['viewboxlbrt']);
104 $_GET['viewbox'] = $aCoOrdinatesLBRT[0].','.$aCoOrdinatesLBRT[3].','.$aCoOrdinatesLBRT[2].','.$aCoOrdinatesLBRT[1];
106 if (isset($_GET['viewbox']) && $_GET['viewbox'])
108 $aCoOrdinates = explode(',',$_GET['viewbox']);
109 $sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aCoOrdinates[0].",".(float)$aCoOrdinates[1]."),ST_Point(".(float)$aCoOrdinates[2].",".(float)$aCoOrdinates[3].")),4326)";
110 $fHeight = $aCoOrdinates[0]-$aCoOrdinates[2];
111 $fWidth = $aCoOrdinates[1]-$aCoOrdinates[3];
112 $aCoOrdinates[0] += $fHeight;
113 $aCoOrdinates[2] -= $fHeight;
114 $aCoOrdinates[1] += $fWidth;
115 $aCoOrdinates[3] -= $fWidth;
116 $sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aCoOrdinates[0].",".(float)$aCoOrdinates[1]."),ST_Point(".(float)$aCoOrdinates[2].",".(float)$aCoOrdinates[3].")),4326)";
118 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
120 $aPoints = explode(',',$_GET['route']);
121 if (sizeof($aPoints) % 2 != 0)
123 echo "Uneven number of points";
126 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
128 foreach($aPoints as $i => $fPoint)
132 if ($i != 1) $sViewboxCentreSQL .= ",";
133 $sViewboxCentreSQL .= ((float)$fPoint).' '.$fPrevCoord;
137 $fPrevCoord = (float)$fPoint;
140 $sViewboxCentreSQL .= ")'::geometry,4326)";
142 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
143 $sViewboxSmallSQL = $oDB->getOne($sSQL);
144 if (PEAR::isError($sViewboxSmallSQL))
146 var_dump($sViewboxSmallSQL);
149 $sViewboxSmallSQL = "'".$sViewboxSmallSQL."'::geometry";
151 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
152 $sViewboxLargeSQL = $oDB->getOne($sSQL);
153 if (PEAR::isError($sViewboxLargeSQL))
155 var_dump($sViewboxLargeSQL);
158 $sViewboxLargeSQL = "'".$sViewboxLargeSQL."'::geometry";
161 // Do we have anything that looks like a lat/lon pair?
162 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
164 $_GET['nearlat'] = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
165 $_GET['nearlon'] = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
166 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
168 elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
170 $_GET['nearlat'] = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
171 $_GET['nearlon'] = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
172 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
174 elseif (preg_match('/(\\[|\\b)(-?[0-9]+[0-9.]*)[, ]+(-?[0-9]+[0-9.]*)(\\]|\\b])/', $sQuery, $aData))
176 $_GET['nearlat'] = $aData[2];
177 $_GET['nearlon'] = $aData[3];
178 $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
184 // Start with a blank search
186 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(),
187 'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
190 $sNearPointSQL = false;
191 if (isset($_GET['nearlat']) && isset($_GET['nearlon']))
193 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$_GET['nearlon'].",".$_GET['nearlat']."),4326)";
194 $aSearches[0]['fLat'] = (float)$_GET['nearlat'];
195 $aSearches[0]['fLon'] = (float)$_GET['nearlon'];
196 $aSearches[0]['fRadius'] = 0.1;
199 $bSpecialTerms = false;
200 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
201 $aSpecialTerms = array();
202 foreach($aSpecialTermsRaw as $aSpecialTerm)
204 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
205 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
208 preg_match_all('/\\[([a-zA-Z]*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
209 $aSpecialTerms = array();
210 foreach($aSpecialTermsRaw as $aSpecialTerm)
212 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
213 $sToken = $oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
214 $sSQL = 'select * from (select word_id,word_token, word, class, type, location, country_code, operator';
215 $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class not in (\'place\',\'highway\')) or country_code is not null';
216 $aSearchWords = $oDB->getAll($sSQL);
217 $aNewSearches = array();
218 foreach($aSearches as $aSearch)
220 foreach($aSearchWords as $aSearchTerm)
222 $aNewSearch = $aSearch;
223 if ($aSearchTerm['country_code'])
225 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
226 $aNewSearches[] = $aNewSearch;
227 $bSpecialTerms = true;
229 if ($aSearchTerm['class'])
231 $aNewSearch['sClass'] = $aSearchTerm['class'];
232 $aNewSearch['sType'] = $aSearchTerm['type'];
233 $aNewSearches[] = $aNewSearch;
234 $bSpecialTerms = true;
238 $aSearches = $aNewSearches;
241 // Split query into phrases
242 // Commas are used to reduce the search space by indicating where phrases split
243 $aPhrases = explode(',',$sQuery);
245 // Convert each phrase to standard form
246 // Create a list of standard words
247 // Get all 'sets' of words
248 // Generate a complete list of all
250 foreach($aPhrases as $iPhrase => $sPhrase)
252 $aPhrase = $oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
253 if (PEAR::isError($aPhrase))
258 if (trim($aPhrase['string']))
260 $aPhrases[$iPhrase] = $aPhrase;
261 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
262 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words']);
263 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
267 unset($aPhrases[$iPhrase]);
271 // reindex phrases - we make assumptions later on
272 $aPhrases = array_values($aPhrases);
274 if (sizeof($aTokens))
277 // Check which tokens we have, get the ID numbers
278 $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator';
279 $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
280 $sSQL .= ' and (class is null or class not in (\'highway\'))';
281 // $sSQL .= ' group by word_token, word, class, type, location, country_code';
283 if (CONST_Debug) var_Dump($sSQL);
285 $aValidTokens = array();
286 if (sizeof($aTokens))
287 $aDatabaseWords = $oDB->getAll($sSQL);
289 $aDatabaseWords = array();
290 if (PEAR::IsError($aDatabaseWords))
292 var_dump($sSQL, $aDatabaseWords);
295 $aPossibleMainWordIDs = array();
296 foreach($aDatabaseWords as $aToken)
298 if (isset($aValidTokens[$aToken['word_token']]))
300 $aValidTokens[$aToken['word_token']][] = $aToken;
304 $aValidTokens[$aToken['word_token']] = array($aToken);
306 if ($aToken['word_token'][0]==' ' && !$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
308 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
310 $aSuggestion = array();
311 $bSuggestion = false;
312 if (CONST_Suggestions_Enabled)
314 foreach($aPhrases as $iPhrase => $aPhrase)
316 if (!isset($aValidTokens[' '.$aPhrase['wordsets'][0][0]]))
318 $sQuotedPhrase = getDBQuoted(' '.$aPhrase['wordsets'][0][0]);
319 $aSuggestionWords = getWordSuggestions($oDB, $aPhrase['wordsets'][0][0]);
320 $aRow = $aSuggestionWords[0];
321 if ($aRow && $aRow['word'])
323 $aSuggestion[] = $aRow['word'];
328 $aSuggestion[] = $aPhrase['string'];
333 $aSuggestion[] = $aPhrase['string'];
337 if ($bSuggestion) $sSuggestion = join(', ',$aSuggestion);
339 // Try and calculate GB postcodes we might be missing
340 foreach($aTokens as $sToken)
342 if (!isset($aValidTokens[$sToken]) && !isset($aValidTokens[' '.$sToken]) && preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
344 if (substr($aData[1],-2,1) != ' ')
346 $aData[0] = substr($aData[0],0,strlen($aData[1]-1)).' '.substr($aData[0],strlen($aData[1]-1));
347 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
349 $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $oDB);
350 if ($aGBPostcodeLocation)
352 $aValidTokens[$sToken] = $aGBPostcodeLocation;
357 // Any words that have failed completely?
360 // Start the search process
361 $aResultPlaceIDs = array();
364 Calculate all searches using aValidTokens i.e.
366 'Wodsworth Road, Sheffield' =>
370 0 1 (wodsworth)(road)
373 Score how good the search is so they can be ordered
376 foreach($aPhrases as $iPhrase => $sPhrase)
378 $aNewPhraseSearches = array();
380 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordset => $aWordset)
382 $aWordsetSearches = $aSearches;
384 // Add all words from this wordset
385 foreach($aWordset as $sToken)
387 //echo "<br><b>$sToken</b>";
388 $aNewWordsetSearches = array();
390 foreach($aWordsetSearches as $aCurrentSearch)
393 //var_dump($aCurrentSearch);
396 // If the token is valid
397 if (isset($aValidTokens[' '.$sToken]))
399 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
401 $aSearch = $aCurrentSearch;
402 $aSearch['iSearchRank']++;
403 if ($aSearchTerm['country_code'] !== null && $aSearchTerm['country_code'] != '0')
405 if ($aSearch['sCountryCode'] === false)
407 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
408 // Country is almost always at the end of the string - increase score for finding it anywhere else (opimisation)
409 if ($iWordset+1 != sizeof($aPhrases[$iPhrase]['wordsets']) || $iPhrase+1 != sizeof($aPhrases)) $aSearch['iSearchRank'] += 5;
410 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
413 elseif ($aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
415 if ($aSearch['fLat'] === '')
417 $aSearch['fLat'] = $aSearchTerm['lat'];
418 $aSearch['fLon'] = $aSearchTerm['lon'];
419 $aSearch['fRadius'] = $aSearchTerm['radius'];
420 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
423 elseif ($aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
425 if ($aSearch['sHouseNumber'] === '')
427 $aSearch['sHouseNumber'] = $sToken;
428 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
430 // Fall back to not searching for this item (better than nothing)
431 $aSearch = $aCurrentSearch;
432 $aSearch['iSearchRank'] += 1;
433 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
437 elseif ($aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
439 if ($aSearch['sClass'] === '')
441 $aSearch['sOperator'] = $aSearchTerm['operator'];
442 $aSearch['sClass'] = $aSearchTerm['class'];
443 $aSearch['sType'] = $aSearchTerm['type'];
444 if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
445 else $aSearch['sOperator'] = 'near'; // near = in for the moment
447 // Do we have a shortcut id?
448 if ($aSearch['sOperator'] == 'name')
450 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
451 if ($iAmenityID = $oDB->getOne($sSQL))
453 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
454 $aSearch['aName'][$iAmenityID] = $iAmenityID;
455 $aSearch['sClass'] = '';
456 $aSearch['sType'] = '';
459 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
464 if (sizeof($aSearch['aName']))
466 if (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false)
468 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
472 $aSearch['iSearchRank'] += 1000; // skip;
477 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
478 // $aSearch['iNamePhrase'] = $iPhrase;
480 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
484 if (isset($aValidTokens[$sToken]))
486 // Allow searching for a word - but at extra cost
487 foreach($aValidTokens[$sToken] as $aSearchTerm)
489 //var_Dump('<hr>',$aSearch['aName']);
491 if (sizeof($aCurrentSearch['aName']) && strlen($sToken) >= 4)
493 $aSearch = $aCurrentSearch;
494 $aSearch['iSearchRank'] += 1;
495 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
496 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
499 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
501 $aSearch = $aCurrentSearch;
502 $aSearch['iSearchRank'] += 2;
503 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
504 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
505 $aSearch['iNamePhrase'] = $iPhrase;
506 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
512 // Allow skipping a word - but at EXTREAM cost
513 //$aSearch = $aCurrentSearch;
514 //$aSearch['iSearchRank']+=100;
515 //$aNewWordsetSearches[] = $aSearch;
519 usort($aNewWordsetSearches, 'bySearchRank');
520 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
522 // var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
524 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
525 usort($aNewPhraseSearches, 'bySearchRank');
527 $aSearchHash = array();
528 foreach($aNewPhraseSearches as $iSearch => $aSearch)
530 $sHash = serialize($aSearch);
531 if (isset($aSearchHash[$sHash]))
533 unset($aNewPhraseSearches[$iSearch]);
537 $aSearchHash[$sHash] = 1;
541 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
544 // Re-group the searches by their score, junk anything over 20 as just not worth trying
545 $aGroupedSearches = array();
546 foreach($aNewPhraseSearches as $aSearch)
548 if ($aSearch['iSearchRank'] < $iMaxRank)
550 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
551 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
554 ksort($aGroupedSearches);
557 $aSearches = array();
558 foreach($aGroupedSearches as $iScore => $aNewSearches)
560 $iSearchCount += sizeof($aNewSearches);
561 $aSearches = array_merge($aSearches, $aNewSearches);
562 if ($iSearchCount > 50) break;
565 // if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
571 // Re-group the searches by their score, junk anything over 20 as just not worth trying
572 $aGroupedSearches = array();
573 foreach($aSearches as $aSearch)
575 if ($aSearch['iSearchRank'] < $iMaxRank)
577 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
578 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
581 ksort($aGroupedSearches);
584 if (CONST_Debug) var_Dump($aGroupedSearches);
588 $aCopyGroupedSearches = $aGroupedSearches;
589 foreach($aCopyGroupedSearches as $iGroup => $aSearches)
591 foreach($aSearches as $iSearch => $aSearch)
593 if (sizeof($aSearch['aAddress']))
595 $iReverseItem = array_pop($aSearch['aAddress']);
596 if (isset($aPossibleMainWordIDs[$iReverseItem]))
598 $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
599 $aSearch['aName'] = array($iReverseItem);
600 $aGroupedSearches[$iGroup][] = $aSearch;
602 // $aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
603 // $aGroupedSearches[$iGroup][] = $aReverseSearch;
609 // Filter out duplicate searches
610 $aSearchHash = array();
611 foreach($aGroupedSearches as $iGroup => $aSearches)
613 foreach($aSearches as $iSearch => $aSearch)
615 $sHash = serialize($aSearch);
616 if (isset($aSearchHash[$sHash]))
618 unset($aGroupedSearches[$iGroup][$iSearch]);
619 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
623 $aSearchHash[$sHash] = 1;
628 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
632 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
635 foreach($aSearches as $aSearch)
639 // Must have a location term
640 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
642 if ($aSearch['sCountryCode'] && !$aSearch['sClass'])
644 if (4 >= $iMinAddressRank && 4 <= $iMaxAddressRank)
646 $sSQL = "select place_id from placex where country_code='".$aSearch['sCountryCode']."' and rank_search = 4 order by st_area(geometry) desc limit 1";
647 $aPlaceIDs = $oDB->getCol($sSQL);
652 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
653 if (!$aSearch['sClass']) continue;
654 if (CONST_Debug) var_dump('<hr>',$aSearch);
655 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
657 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
658 if ($oDB->getOne($sSQL))
660 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType'];
661 $sSQL .= " where st_contains($sViewboxSmallSQL, centroid)";
662 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
663 $sSQL .= " limit $iLimit";
664 if (CONST_Debug) var_dump($sSQL);
665 $aPlaceIDs = $oDB->getCol($sSQL);
667 if (!sizeof($aPlaceIDs))
669 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType'];
670 $sSQL .= " where st_contains($sViewboxLargeSQL, centroid)";
671 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
672 $sSQL .= " limit $iLimit";
673 if (CONST_Debug) var_dump($sSQL);
674 $aPlaceIDs = $oDB->getCol($sSQL);
679 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
680 $sSQL .= " and st_contains($sViewboxSmallSQL, centroid)";
681 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
682 $sSQL .= " limit $iLimit";
683 if (CONST_Debug) var_dump($sSQL);
684 $aPlaceIDs = $oDB->getCol($sSQL);
690 if (CONST_Debug) var_dump('<hr>',$aSearch);
691 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
692 $aPlaceIDs = array();
694 // First we need a position, either aName or fLat or both
697 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
698 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
699 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
700 if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank in (26,27)";
701 if ($aSearch['fLon'] && $aSearch['fLat'])
703 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
704 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
706 if (sizeof($aExcludePlaceIDs))
708 $aTerms[] = "place_id not in (".join(',',$aExcludePlaceIDs).")";
710 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $sViewboxSmallSQL";
711 if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
713 $sImportanceSQL = 'case when importance = 0 OR importance IS NULL then 0.92-(search_rank::float/33) else importance end';
715 if ($sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
716 if ($sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
717 $aOrder[] = "$sImportanceSQL DESC";
721 $sSQL = "select place_id";
722 $sSQL .= " from search_name";
723 $sSQL .= " where ".join(' and ',$aTerms);
724 $sSQL .= " order by ".join(', ',$aOrder);
725 if ($aSearch['sHouseNumber'])
726 $sSQL .= " limit 50";
727 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
730 $sSQL .= " limit ".$iLimit;
732 if (CONST_Debug) var_dump($sSQL);
733 $aViewBoxPlaceIDs = $oDB->getAll($sSQL);
734 if (PEAR::IsError($aViewBoxPlaceIDs))
736 var_dump($sSQL, $aViewBoxPlaceIDs);
739 //var_dump($aViewBoxPlaceIDs);
740 // Did we have an viewbox matches?
741 $aPlaceIDs = array();
742 $bViewBoxMatch = false;
743 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
745 // if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
746 // if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
747 // if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
748 // else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
749 $aPlaceIDs[] = $aViewBoxRow['place_id'];
752 //var_Dump($aPlaceIDs);
755 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
757 $aRoadPlaceIDs = $aPlaceIDs;
758 $sPlaceIDs = join(',',$aPlaceIDs);
760 // Now they are indexed look for a house attached to a street we found
761 $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-, ]',$aSearch['sHouseNumber']).'\\\\M';
762 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
763 if (sizeof($aExcludePlaceIDs))
765 $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
767 $sSQL .= " limit $iLimit";
768 if (CONST_Debug) var_dump($sSQL);
769 $aPlaceIDs = $oDB->getCol($sSQL);
771 // If not try the aux fallback table
772 if (!sizeof($aPlaceIDs))
774 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
775 if (sizeof($aExcludePlaceIDs))
777 $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
779 // $sSQL .= " limit $iLimit";
780 if (CONST_Debug) var_dump($sSQL);
781 $aPlaceIDs = $oDB->getCol($sSQL);
784 if (!sizeof($aPlaceIDs))
786 $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
787 if (sizeof($aExcludePlaceIDs))
789 $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
791 // $sSQL .= " limit $iLimit";
792 if (CONST_Debug) var_dump($sSQL);
793 $aPlaceIDs = $oDB->getCol($sSQL);
796 // Fallback to the road
797 if (!sizeof($aPlaceIDs))
799 $aPlaceIDs = $aRoadPlaceIDs;
804 if ($aSearch['sClass'] && sizeof($aPlaceIDs))
806 $sPlaceIDs = join(',',$aPlaceIDs);
808 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
810 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
811 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
812 $sSQL .= " order by rank_search asc limit $iLimit";
813 if (CONST_Debug) var_dump($sSQL);
814 $aPlaceIDs = $oDB->getCol($sSQL);
817 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
819 $sSQL = "select rank_search from placex where place_id in ($sPlaceIDs) order by rank_search asc limit 1";
820 if (CONST_Debug) var_dump($sSQL);
821 $iMaxRank = ((int)$oDB->getOne($sSQL)) + 5;
823 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
824 if (CONST_Debug) var_dump($sSQL);
825 $aPlaceIDs = $oDB->getCol($sSQL);
826 $sPlaceIDs = join(',',$aPlaceIDs);
832 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
833 if ($oDB->getOne($sSQL))
835 // More efficient - can make the range bigger
837 $sSQL = "select l.place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
838 $sSQL .= ",placex as f where ";
839 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, st_centroid(f.geometry), $fRange) ";
840 if (sizeof($aExcludePlaceIDs))
842 $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
844 if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.centroid) ASC";
845 else $sSQL .= " order by ST_Distance(l.centroid, f.geometry) asc";
846 $sSQL .= " limit $iLimit";
847 if (CONST_Debug) var_dump($sSQL);
848 $aPlaceIDs = $oDB->getCol($sSQL);
852 if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
853 $sSQL = "select l.place_id from placex as l,placex as f where ";
854 $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, st_centroid(f.geometry), $fRange) ";
855 $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
856 if (sizeof($aExcludePlaceIDs))
858 $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
860 if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.geometry) ASC";
861 else $sSQL .= " order by ST_Distance(l.geometry, f.geometry) asc, l.rank_search ASC";
862 $sSQL .= " limit $iLimit";
863 if (CONST_Debug) var_dump($sSQL);
864 $aPlaceIDs = $oDB->getCol($sSQL);
872 if (PEAR::IsError($aPlaceIDs))
874 var_dump($sSQL, $aPlaceIDs);
878 if (CONST_Debug) var_Dump($aPlaceIDs);
880 foreach($aPlaceIDs as $iPlaceID)
882 $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
884 if ($iQueryLoop > 20) break;
887 if (sizeof($aResultPlaceIDs)) break;
888 if ($iGroupLoop > 4) break;
889 if ($iQueryLoop > 30) break;
892 // Did we find anything?
893 if (sizeof($aResultPlaceIDs))
895 //var_Dump($aResultPlaceIDs);exit;
896 // Get the details for display (is this a redundant extra step?)
897 $sPlaceIDs = join(',',$aResultPlaceIDs);
898 $sOrderSQL = 'CASE ';
899 foreach(array_keys($aResultPlaceIDs) as $iOrder => $iPlaceID)
901 $sOrderSQL .= 'when min(place_id) = '.$iPlaceID.' then '.$iOrder.' ';
903 $sOrderSQL .= ' ELSE 10000000 END';
904 $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id,country_code,";
905 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
906 $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
907 $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
908 $sSQL .= "avg(ST_X(ST_Centroid(geometry))) as lon,avg(ST_Y(ST_Centroid(geometry))) as lat, ";
909 $sSQL .= $sOrderSQL." as porder, ";
910 $sSQL .= "coalesce(importance,0.9-(rank_search::float/30)) as importance ";
911 $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
912 $sSQL .= "and placex.rank_address between $iMinAddressRank and $iMaxAddressRank ";
913 $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,country_code,importance";
914 if (!$bDeDupe) $sSQL .= ",place_id";
915 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
916 $sSQL .= ",get_name_by_language(name, $sLanguagePrefArraySQL) ";
917 $sSQL .= ",get_name_by_language(name, ARRAY['ref']) ";
919 $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,";
920 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
921 $sSQL .= "null as placename,";
922 $sSQL .= "null as ref,";
923 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
924 $sSQL .= $sOrderSQL." as porder, ";
925 $sSQL .= "-0.15 as importance ";
926 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
927 $sSQL .= "and 30 between $iMinAddressRank and $iMaxAddressRank ";
928 $sSQL .= "group by place_id";
929 if (!$bDeDupe) $sSQL .= ",place_id";
931 $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,";
932 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
933 $sSQL .= "null as placename,";
934 $sSQL .= "null as ref,";
935 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
936 $sSQL .= $sOrderSQL." as porder, ";
937 $sSQL .= "-0.15 as importance ";
938 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
939 $sSQL .= "and 30 between $iMinAddressRank and $iMaxAddressRank ";
940 $sSQL .= "group by place_id";
941 if (!$bDeDupe) $sSQL .= ",place_id";
942 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
943 $sSQL .= "order by porder asc";
944 // $sSQL .= "order by rank_search,rank_address,porder asc";
945 if (CONST_Debug) var_dump('<hr>',$sSQL);
946 $aSearchResults = $oDB->getAll($sSQL);
947 //var_dump($sSQL,$aSearchResults);exit;
949 if (PEAR::IsError($aSearchResults))
951 var_dump($sSQL, $aSearchResults);
959 if (!sizeof($aSearchResults) && isset($_GET['q']) && $_GET['q'])
961 $sSearchResult = 'No Results Found';
963 //var_Dump($aSearchResults);
965 $aClassType = getClassTypesWithImportance();
966 foreach($aSearchResults as $iResNum => $aResult)
968 if (CONST_Search_AreaPolygons || true)
970 // Get the bounding box and outline polygon
971 $sSQL = "select place_id,numfeatures,area,outline,";
972 $sSQL .= "ST_Y(ST_PointN(ExteriorRing(ST_Box2D(outline)),4)) as minlat,ST_Y(ST_PointN(ExteriorRing(ST_Box2D(outline)),2)) as maxlat,";
973 $sSQL .= "ST_X(ST_PointN(ExteriorRing(ST_Box2D(outline)),1)) as minlon,ST_X(ST_PointN(ExteriorRing(ST_Box2D(outline)),3)) as maxlon,";
974 $sSQL .= "ST_AsText(outline) as outlinestring from get_place_boundingbox_quick(".$aResult['place_id'].")";
976 $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
977 $sSQL .= "ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),2)) as maxlat,";
978 $sSQL .= "ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),3)) as maxlon,";
979 $sSQL .= "ST_AsText(geometry) as outlinestring from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(ST_Box2D(geometry)) = \'ST_Polygon\'';
980 $aPointPolygon = $oDB->getRow($sSQL);
981 if (PEAR::IsError($aPointPolygon))
983 var_dump($sSQL, $aPointPolygon);
986 if ($aPointPolygon['place_id'])
988 // Translate geometary string to point array
989 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
991 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
993 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['outlinestring'],$aMatch))
996 $iSteps = ($fRadius * 40000)^2;
997 $fStepSize = (2*pi())/$iSteps;
998 $aPolyPoints = array();
999 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1001 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1003 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1004 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1005 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1006 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1009 // Output data suitable for display (points and a bounding box)
1012 $aResult['aPolyPoints'] = array();
1013 foreach($aPolyPoints as $aPoint)
1015 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1018 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1022 if (!isset($aResult['aBoundingBox']))
1025 $fDiameter = 0.0001;
1027 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1028 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1030 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1032 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1033 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1035 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1037 $fRadius = $fDiameter / 2;
1039 $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1040 $fStepSize = (2*pi())/$iSteps;
1041 $aPolyPoints = array();
1042 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1044 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1046 $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1047 $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1048 $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1049 $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1051 // Output data suitable for display (points and a bounding box)
1054 $aResult['aPolyPoints'] = array();
1055 foreach($aPolyPoints as $aPoint)
1057 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1060 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1063 // Is there an icon set for this type of result?
1064 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1065 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1067 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1070 if ($bShowAddressDetails)
1072 $aResult['address'] = getAddressDetails($oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1073 //var_dump($aResult['address']);
1077 //if (CONST_Debug) var_dump($aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']);
1079 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance'])
1080 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance'])
1082 $aResult['importance'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance'];
1084 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1085 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1087 $aResult['importance'] = $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1091 $aResult['importance'] = 1000000000000000;
1094 $aResult['name'] = $aResult['langaddress'];
1095 $aResult['foundorder'] = $iResNum;
1096 $aSearchResults[$iResNum] = $aResult;
1099 uasort($aSearchResults, 'byImportance');
1101 //var_dump($aSearchResults);exit;
1103 $aOSMIDDone = array();
1104 $aClassTypeNameDone = array();
1105 $aToFilter = $aSearchResults;
1106 $aSearchResults = array();
1109 foreach($aToFilter as $iResNum => $aResult)
1111 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1112 $aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1115 $fLat = $aResult['lat'];
1116 $fLon = $aResult['lon'];
1117 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1120 if (!$bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1121 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['osm_class'].$aResult['name']])))
1123 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1124 $aClassTypeNameDone[$aResult['osm_type'].$aResult['osm_class'].$aResult['name']] = true;
1125 $aSearchResults[] = $aResult;
1129 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '1 day'::interval,'YYYY/MM/DD') from import_status limit 1");
1131 if (isset($_GET['nearlat']) && isset($_GET['nearlon']))
1133 $sQuery .= ' ['.$_GET['nearlat'].','.$_GET['nearlon'].']';
1138 logEnd($oDB, $hLog, sizeof($aToFilter));
1140 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$aExcludePlaceIDs);
1141 $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
1142 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
1143 if ($bShowAddressDetails) $sMoreURL .= '&addressdetails=1';
1144 if (isset($_GET['viewbox']) && $_GET['viewbox']) $sMoreURL .= '&viewbox='.urlencode($_GET['viewbox']);
1145 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
1148 $sSuggestionURL = $sMoreURL.'&q='.urlencode($sSuggestion);
1150 $sMoreURL .= '&q='.urlencode($sQuery);
1152 if (CONST_Debug) exit;
1154 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');