]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
calculate search position based to 'importance' rather than address rank
[nominatim.git] / website / search.php
1 <?php
2         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
3         require_once(CONST_BasePath.'/lib/log.php');
4
5         ini_set('memory_limit', '200M');
6         $oDB =& getDB();
7
8         // Display defaults
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;
21         $iMaxRank = 20;
22         if ($iLimit > 100) $iLimit = 100;
23         $iMinAddressRank = 0;
24         $iMaxAddressRank = 30;
25
26         // Format for output
27         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
28         {
29                 $sOutputFormat = $_GET['format'];
30         }
31
32         // Show / use polygons
33         $bShowPolygons = isset($_GET['polygon']) && $_GET['polygon'];
34
35         // Show address breakdown
36         $bShowAddressDetails = isset($_GET['addressdetails']) && $_GET['addressdetails'];
37
38         // Prefered language    
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;
43
44 $bReverseInPlan = true;
45
46         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
47
48         if (isset($_GET['exclude_place_ids']) && $_GET['exclude_place_ids'])
49         {
50                 foreach(explode(',',$_GET['exclude_place_ids']) as $iExcludedPlaceID)
51                 {
52                         $iExcludedPlaceID = (int)$iExcludedPlaceID;
53                         if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
54                 }
55         }
56
57   // Only certain ranks of feature
58         if (isset($_GET['featuretype']))
59         {
60                 switch($_GET['featuretype'])
61                 {
62                 case 'country':
63                         $iMinAddressRank = $iMaxAddressRank = 4;
64                         break;
65                 case 'state':
66                         $iMinAddressRank = $iMaxAddressRank = 8;
67                         break;
68                 case 'city':
69                         $iMinAddressRank = 14;
70                         $iMaxAddressRank = 16;
71                         break;
72                 case 'settlement':
73                         $iMinAddressRank = 8;
74                         $iMaxAddressRank = 20;
75                         break;
76                 }
77         }
78                 
79         // Search query
80         $sQuery = (isset($_GET['q'])?trim($_GET['q']):'');
81         if (!$sQuery && $_SERVER['PATH_INFO'] && $_SERVER['PATH_INFO'][0] == '/')
82         {
83                 $sQuery = substr($_SERVER['PATH_INFO'], 1);
84
85                 // reverse order of '/' seperated string
86                 $aPhrases = explode('/', $sQuery);              
87                 $aPhrases = array_reverse($aPhrases); 
88                 $sQuery = join(', ',$aPhrases);
89         }
90
91         if ($sQuery)
92         {
93                 $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
94
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);
97
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'])
102                 {
103                         $aCoOrdinatesLBRT = explode(',',$_GET['viewboxlbrt']);
104                         $_GET['viewbox'] = $aCoOrdinatesLBRT[0].','.$aCoOrdinatesLBRT[3].','.$aCoOrdinatesLBRT[2].','.$aCoOrdinatesLBRT[1];
105                 }
106                 if (isset($_GET['viewbox']) && $_GET['viewbox'])
107                 {
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)";
117                 }
118                 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
119                 {
120                         $aPoints = explode(',',$_GET['route']);
121                         if (sizeof($aPoints) % 2 != 0)
122                         {
123                                 echo "Uneven number of points";
124                                 exit;
125                         }
126                         $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
127                         $fPrevCoord = false;
128                         foreach($aPoints as $i => $fPoint)
129                         {
130                                 if ($i%2)
131                                 {
132                                         if ($i != 1) $sViewboxCentreSQL .= ",";
133                                         $sViewboxCentreSQL .= ((float)$fPoint).' '.$fPrevCoord;
134                                 }
135                                 else
136                                 {
137                                         $fPrevCoord = (float)$fPoint;
138                                 }
139                         }
140                         $sViewboxCentreSQL .= ")'::geometry,4326)";
141
142                         $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
143                         $sViewboxSmallSQL = $oDB->getOne($sSQL);
144                         if (PEAR::isError($sViewboxSmallSQL))
145                         {
146                                 var_dump($sViewboxSmallSQL);
147                                 exit;
148                         }
149                         $sViewboxSmallSQL = "'".$sViewboxSmallSQL."'::geometry";
150
151                         $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
152                         $sViewboxLargeSQL = $oDB->getOne($sSQL);
153                         if (PEAR::isError($sViewboxLargeSQL))
154                         {
155                                 var_dump($sViewboxLargeSQL);
156                                 exit;
157                         }
158                         $sViewboxLargeSQL = "'".$sViewboxLargeSQL."'::geometry";
159                 }
160
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))
163                 {
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));
167                 }
168                 elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
169                 {
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));
173                 }
174                 elseif (preg_match('/(\\[|\\b)(-?[0-9]+[0-9.]*)[, ]+(-?[0-9]+[0-9.]*)(\\]|\\b])/', $sQuery, $aData))
175                 {
176                         $_GET['nearlat'] = $aData[2];
177                         $_GET['nearlon'] = $aData[3];
178                         $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
179                 }
180
181                 if ($sQuery)
182                 {
183
184                         // Start with a blank search
185                         $aSearches = array(
186                                 array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 
187                                         'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
188                         );
189
190                         $sNearPointSQL = false;
191                         if (isset($_GET['nearlat']) && isset($_GET['nearlon']))
192                         {
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;
197                         }
198
199                         $bSpecialTerms = false;
200                         preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
201                         $aSpecialTerms = array();
202                         foreach($aSpecialTermsRaw as $aSpecialTerm)
203                         {
204                                 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
205                                 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
206                         }
207
208                         preg_match_all('/\\[([a-zA-Z]*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
209                         $aSpecialTerms = array();
210                         foreach($aSpecialTermsRaw as $aSpecialTerm)
211                         {
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)
219                                 {
220                                         foreach($aSearchWords as $aSearchTerm)
221                                         {
222                                                 $aNewSearch = $aSearch;                 
223                                                 if ($aSearchTerm['country_code'])
224                                                 {
225                                                         $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
226                                                         $aNewSearches[] = $aNewSearch;
227                                                         $bSpecialTerms = true;
228                                                 }
229                                                 if ($aSearchTerm['class'])
230                                                 {
231                                                         $aNewSearch['sClass'] = $aSearchTerm['class'];
232                                                         $aNewSearch['sType'] = $aSearchTerm['type'];
233                                                         $aNewSearches[] = $aNewSearch;
234                                                         $bSpecialTerms = true;
235                                                 }
236                                         }
237                                 }
238                                 $aSearches = $aNewSearches;
239                         }
240
241                         // Split query into phrases
242                         // Commas are used to reduce the search space by indicating where phrases split
243                         $aPhrases = explode(',',$sQuery);
244
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 
249                         $aTokens = array();
250                         foreach($aPhrases as $iPhrase => $sPhrase)
251                         {
252                                 $aPhrase = $oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
253                                 if (PEAR::isError($aPhrase))
254                                 {
255                                         var_dump($aPhrase);
256                                         exit;
257                                 }
258                                 if (trim($aPhrase['string']))
259                                 {
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']));
264                                 }
265                                 else
266                                 {
267                                         unset($aPhrases[$iPhrase]);
268                                 }
269                         }                       
270
271                         // reindex phrases - we make assumptions later on
272                         $aPhrases = array_values($aPhrases);
273
274                         if (sizeof($aTokens))
275                         {
276
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';
282
283                         if (CONST_Debug) var_Dump($sSQL);
284
285                         $aValidTokens = array();
286                         if (sizeof($aTokens))
287                                 $aDatabaseWords = $oDB->getAll($sSQL);
288                         else
289                                 $aDatabaseWords = array();
290                         if (PEAR::IsError($aDatabaseWords))
291                         {
292                                 var_dump($sSQL, $aDatabaseWords);
293                                 exit;
294                         }
295                         $aPossibleMainWordIDs = array();
296                         foreach($aDatabaseWords as $aToken)
297                         {
298                                 if (isset($aValidTokens[$aToken['word_token']]))
299                                 {
300                                         $aValidTokens[$aToken['word_token']][] = $aToken;
301                                 }
302                                 else
303                                 {
304                                         $aValidTokens[$aToken['word_token']] = array($aToken);
305                                 }
306                                 if ($aToken['word_token'][0]==' ' && !$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
307                         }
308                         if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
309
310                         $aSuggestion = array();
311                         $bSuggestion = false;
312                         if (CONST_Suggestions_Enabled)
313                         {
314                                 foreach($aPhrases as $iPhrase => $aPhrase)
315                                 {
316                                         if (!isset($aValidTokens[' '.$aPhrase['wordsets'][0][0]]))
317                                         {
318                                                 $sQuotedPhrase = getDBQuoted(' '.$aPhrase['wordsets'][0][0]);
319                                                 $aSuggestionWords = getWordSuggestions($oDB, $aPhrase['wordsets'][0][0]);
320                                                 $aRow = $aSuggestionWords[0];
321                                                 if ($aRow && $aRow['word'])
322                                                 {
323                                                         $aSuggestion[] = $aRow['word'];
324                                                         $bSuggestion = true;
325                                                 }
326                                                 else
327                                                 {
328                                                         $aSuggestion[] = $aPhrase['string'];
329                                                 }
330                                         }
331                                         else
332                                         {
333                                                 $aSuggestion[] = $aPhrase['string'];
334                                         }
335                                 }
336                         }
337                         if ($bSuggestion) $sSuggestion = join(', ',$aSuggestion);
338 /*
339                         // Try and calculate GB postcodes we might be missing
340                         foreach($aTokens as $sToken)
341                         {
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))
343                                 {
344                                         if (substr($aData[1],-2,1) != ' ')
345                                         {
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);
348                                         }
349                                         $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $oDB);
350                                         if ($aGBPostcodeLocation)
351                                         {
352                                                 $aValidTokens[$sToken] = $aGBPostcodeLocation;
353                                         }
354                                 }
355                         }
356 */
357                         // Any words that have failed completely?
358                         // TODO: suggestions
359
360                         // Start the search process
361                         $aResultPlaceIDs = array();
362
363                         /*
364                                 Calculate all searches using aValidTokens i.e.
365
366                                 'Wodsworth Road, Sheffield' =>
367                                         
368                                 Phrase Wordset
369                                 0      0       (wodsworth road)
370                                 0      1       (wodsworth)(road)
371                                 1      0       (sheffield)
372                                 
373                                 Score how good the search is so they can be ordered
374                         */
375
376                                 foreach($aPhrases as $iPhrase => $sPhrase)
377                                 {
378                                         $aNewPhraseSearches = array();
379
380                                         foreach($aPhrases[$iPhrase]['wordsets'] as $iWordset => $aWordset)
381                                         {
382                                                 $aWordsetSearches = $aSearches;
383
384                                                 // Add all words from this wordset
385                                                 foreach($aWordset as $sToken)
386                                                 {
387 //echo "<br><b>$sToken</b>";
388                                                         $aNewWordsetSearches = array();
389                                                         
390                                                         foreach($aWordsetSearches as $aCurrentSearch)
391                                                         {
392 //echo "<i>";
393 //var_dump($aCurrentSearch);
394 //echo "</i>";
395
396                                                                 // If the token is valid
397                                                                 if (isset($aValidTokens[' '.$sToken]))
398                                                                 {
399                                                                         foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
400                                                                         {
401                                                                                 $aSearch = $aCurrentSearch;
402                                                                                 $aSearch['iSearchRank']++;
403                                                                                 if ($aSearchTerm['country_code'] !== null && $aSearchTerm['country_code'] != '0')
404                                                                                 {
405                                                                                         if ($aSearch['sCountryCode'] === false)
406                                                                                         {
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;
411                                                                                         }
412                                                                                 }
413                                                                                 elseif ($aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
414                                                                                 {
415                                                                                         if ($aSearch['fLat'] === '')
416                                                                                         {
417                                                                                                 $aSearch['fLat'] = $aSearchTerm['lat'];
418                                                                                                 $aSearch['fLon'] = $aSearchTerm['lon'];
419                                                                                                 $aSearch['fRadius'] = $aSearchTerm['radius'];
420                                                                                                 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
421                                                                                         }
422                                                                                 }
423                                                                                 elseif ($aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
424                                                                                 {
425                                                                                         if ($aSearch['sHouseNumber'] === '')
426                                                                                         {
427                                                                                                 $aSearch['sHouseNumber'] = $sToken;
428                                                                                                 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
429 /*
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;
434 */
435                                                                                         }
436                                                                                 }
437                                                                                 elseif ($aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
438                                                                                 {
439                                                                                         if ($aSearch['sClass'] === '')
440                                                                                         {
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
446
447                                                                                                 // Do we have a shortcut id?
448                                                                                                 if ($aSearch['sOperator'] == 'name')
449                                                                                                 {
450                                                                                                         $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
451                                                                                                         if ($iAmenityID = $oDB->getOne($sSQL))
452                                                                                                         {
453                                                                                                                 $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
454                                                                                                                 $aSearch['aName'][$iAmenityID] = $iAmenityID;
455                                                                                                                 $aSearch['sClass'] = '';
456                                                                                                                 $aSearch['sType'] = '';
457                                                                                                         }
458                                                                                                 }
459                                                                                                 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
460                                                                                         }
461                                                                                 }
462                                                                                 else
463                                                                                 {
464                                                                                         if (sizeof($aSearch['aName']))
465                                                                                         {
466                                                                                                 if (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false)
467                                                                                                 {
468                                                                                                         $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
469                                                                                                 }
470                                                                                                 else
471                                                                                                 {
472                                                                                                         $aSearch['iSearchRank'] += 1000; // skip;
473                                                                                                 }
474                                                                                         }
475                                                                                         else
476                                                                                         {
477                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
478 //                                                                                              $aSearch['iNamePhrase'] = $iPhrase;
479                                                                                         }
480                                                                                         if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
481                                                                                 }
482                                                                         }
483                                                                 }
484                                                                 if (isset($aValidTokens[$sToken]))
485                                                                 {
486                                                                         // Allow searching for a word - but at extra cost
487                                                                         foreach($aValidTokens[$sToken] as $aSearchTerm)
488                                                                         {
489 //var_Dump('<hr>',$aSearch['aName']);
490
491                                                                                 if (sizeof($aCurrentSearch['aName'])  && strlen($sToken) >= 4)
492                                                                                 {
493                                                                                 $aSearch = $aCurrentSearch;
494                                                                                         $aSearch['iSearchRank'] += 1;
495                                                                                         $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
496                                                                                 if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
497                                                                                 }
498
499                                                                                 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
500                                                                                 {
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;
507                                                                                 }
508                                                                         }
509                                                                 }
510                                                                 else
511                                                                 {
512                                                                         // Allow skipping a word - but at EXTREAM cost
513                                                                         //$aSearch = $aCurrentSearch;
514                                                                         //$aSearch['iSearchRank']+=100;
515                                                                         //$aNewWordsetSearches[] = $aSearch;
516                                                                 }
517                                                         }
518                                                         // Sort and cut
519                                                         usort($aNewWordsetSearches, 'bySearchRank');
520                                                         $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
521                                                 }                                               
522 //                                              var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
523
524                                                 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
525                                                 usort($aNewPhraseSearches, 'bySearchRank');
526
527           $aSearchHash = array();
528           foreach($aNewPhraseSearches as $iSearch => $aSearch)
529           {
530             $sHash = serialize($aSearch);
531             if (isset($aSearchHash[$sHash]))
532             {
533               unset($aNewPhraseSearches[$iSearch]);
534             }
535             else
536             {
537               $aSearchHash[$sHash] = 1;
538             }
539           }
540
541                                                 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
542                                         }
543
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)
547                                         {
548                                                 if ($aSearch['iSearchRank'] < $iMaxRank)
549                                                 {
550                                                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
551                                                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
552                                                 }
553                                         }
554                                         ksort($aGroupedSearches);
555
556                                         $iSearchCount = 0;
557                                         $aSearches = array();
558                                         foreach($aGroupedSearches as $iScore => $aNewSearches)
559                                         {
560                                                 $iSearchCount += sizeof($aNewSearches);
561                                                 $aSearches = array_merge($aSearches, $aNewSearches);
562                                                 if ($iSearchCount > 50) break;
563                                         }
564
565 //                                      if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
566
567                                 }
568                         }
569                         else
570                         {
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)
574                                         {
575                                                 if ($aSearch['iSearchRank'] < $iMaxRank)
576                                                 {
577                                                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
578                                                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
579                                                 }
580                                         }
581                                         ksort($aGroupedSearches);
582                         }
583                                 
584                                 if (CONST_Debug) var_Dump($aGroupedSearches);
585
586                                 if ($bReverseInPlan)
587                                 {
588                                         $aCopyGroupedSearches = $aGroupedSearches;
589                                         foreach($aCopyGroupedSearches as $iGroup => $aSearches)
590                                         {
591                                                 foreach($aSearches as $iSearch => $aSearch)
592                                                 {
593                                                         if (sizeof($aSearch['aAddress']))
594                                                         {
595                                                                 $iReverseItem = array_pop($aSearch['aAddress']);
596                                                                 if (isset($aPossibleMainWordIDs[$iReverseItem]))
597                                                                 {
598                                                                         $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
599                                                                         $aSearch['aName'] = array($iReverseItem);
600                                                                         $aGroupedSearches[$iGroup][] = $aSearch;
601                                                                 }
602 //                                                              $aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
603         //                                                      $aGroupedSearches[$iGroup][] = $aReverseSearch;
604                                                         }
605                                                 }
606                                         }
607                                 }
608
609                                 // Filter out duplicate searches
610                                 $aSearchHash = array();
611                                 foreach($aGroupedSearches as $iGroup => $aSearches)
612                                 {
613                                         foreach($aSearches as $iSearch => $aSearch)
614                                         {
615                                                 $sHash = serialize($aSearch);
616                                                 if (isset($aSearchHash[$sHash]))
617                                                 {
618                                                         unset($aGroupedSearches[$iGroup][$iSearch]);
619                                                         if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
620                                                 }
621                                                 else
622                                                 {
623                                                         $aSearchHash[$sHash] = 1;
624                                                 }
625                                         }
626                                 }
627
628                                 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
629
630                                 $iGroupLoop = 0;
631                                 $iQueryLoop = 0;
632                                 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
633                                 {
634                                         $iGroupLoop++;
635                                         foreach($aSearches as $aSearch)
636                                         {
637                                                 $iQueryLoop++;
638
639                                                 // Must have a location term
640                                                 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
641                                                 {
642                                                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'])
643                                                         {
644                                                                 if (4 >= $iMinAddressRank && 4 <= $iMaxAddressRank)
645                                                                 {
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);
648                                                                 }
649                                                         }
650                                                         else
651                                                         {
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);     
656
657                                                                 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
658                                                                 if ($oDB->getOne($sSQL))
659                                                                 {
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);
666
667                                                                 if (!sizeof($aPlaceIDs))
668                                                                 {
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);
675                                                                 }
676                                                         }
677                                                         else
678                                                         {
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);
685                                                         }
686                                                         }
687                                                 }
688                                                 else
689                                                 {
690                                                         if (CONST_Debug) var_dump('<hr>',$aSearch);
691                                                         if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);     
692                                                         $aPlaceIDs = array();
693                                                 
694                                                         // First we need a position, either aName or fLat or both
695                                                         $aTerms = array();
696                                                         $aOrder = array();
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'])
702                                                         {
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";
705                                                         }
706                                                         if (sizeof($aExcludePlaceIDs))
707                                                         {
708                                                                 $aTerms[] = "place_id not in (".join(',',$aExcludePlaceIDs).")";
709                                                         }
710                                                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $sViewboxSmallSQL";
711                                                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
712
713                                                         $sImportanceSQL = 'case when importance = 0 OR importance IS NULL then 0.92-(search_rank::float/33) else importance end';
714
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";
718                                                 
719                                                         if (sizeof($aTerms))
720                                                         {
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'])
728                                                                         $sSQL .= " limit 1";
729                                                                 else
730                                                                         $sSQL .= " limit ".$iLimit;
731
732                                                                 if (CONST_Debug) var_dump($sSQL);
733                                                                 $aViewBoxPlaceIDs = $oDB->getAll($sSQL);
734                                                                 if (PEAR::IsError($aViewBoxPlaceIDs))
735                                                                 {
736                                                                         var_dump($sSQL, $aViewBoxPlaceIDs);                                     
737                                                                         exit;
738                                                                 }
739 //var_dump($aViewBoxPlaceIDs);
740                                                                 // Did we have an viewbox matches?
741                                                                 $aPlaceIDs = array();
742                                                                 $bViewBoxMatch = false;
743                                                                 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
744                                                                 {
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'];
750                                                                 }
751                                                         }
752 //var_Dump($aPlaceIDs);
753 //exit;
754
755                                                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
756                                                         {
757                                                                 $aRoadPlaceIDs = $aPlaceIDs;
758                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
759         
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))
764                                                                 {
765                                                                         $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
766                                                                 }
767                                                                 $sSQL .= " limit $iLimit";
768                                                                 if (CONST_Debug) var_dump($sSQL);
769                                                                 $aPlaceIDs = $oDB->getCol($sSQL);
770
771                                                                 // If not try the aux fallback table
772                                                                 if (!sizeof($aPlaceIDs))
773                                                                 {
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))
776                                                                         {
777                                                                                 $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
778                                                                         }
779 //                                                                      $sSQL .= " limit $iLimit";
780                                                                         if (CONST_Debug) var_dump($sSQL);
781                                                                         $aPlaceIDs = $oDB->getCol($sSQL);
782                                                                 }
783
784                                                                 if (!sizeof($aPlaceIDs))
785                                                                 {
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))
788                                                                         {
789                                                                                 $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
790                                                                         }
791 //                                                                      $sSQL .= " limit $iLimit";
792                                                                         if (CONST_Debug) var_dump($sSQL);
793                                                                         $aPlaceIDs = $oDB->getCol($sSQL);
794                                                                 }
795
796                                                                 // Fallback to the road
797                                                                 if (!sizeof($aPlaceIDs))
798                                                                 {
799                                                                         $aPlaceIDs = $aRoadPlaceIDs;
800                                                                 }
801                                                                 
802                                                         }
803                                                 
804                                                         if ($aSearch['sClass'] && sizeof($aPlaceIDs))
805                                                         {
806                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
807
808                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
809                                                                 {
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);
815                                                                 }
816                                                                 
817                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
818                                                                 {
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;
822
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);
827
828                                                                         if ($sPlaceIDs)
829                                                                         {
830
831                                                                         $fRange = 0.01;
832                                                                         $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
833                                                                         if ($oDB->getOne($sSQL))
834                                                                         {
835                                                                                 // More efficient - can make the range bigger
836                                                                         $fRange = 0.05;
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))
841                                                                                 {
842                                                                                         $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
843                                                                                 }
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);
849                                                                         }
850                                                                         else
851                                                                         {
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))
857                                                                                 {
858                                                                                         $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
859                                                                                 }
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);
865                                                                         }
866                                                                         }
867                                                                 }
868                                                         }
869                                                 
870                                                 }
871
872                                                 if (PEAR::IsError($aPlaceIDs))
873                                                 {
874                                                         var_dump($sSQL, $aPlaceIDs);                                    
875                                                         exit;
876                                                 }
877
878                                                 if (CONST_Debug) var_Dump($aPlaceIDs);
879
880                                                 foreach($aPlaceIDs as $iPlaceID)
881                                                 {
882                                                         $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
883                                                 }
884                                                 if ($iQueryLoop > 20) break;
885                                         }
886                                         //exit;
887                                         if (sizeof($aResultPlaceIDs)) break;
888                                         if ($iGroupLoop > 4) break;
889                                         if ($iQueryLoop > 30) break;
890                                 }
891 //exit;
892                                 // Did we find anything?        
893                                 if (sizeof($aResultPlaceIDs))
894                                 {
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)
900                                         {
901                                                 $sOrderSQL .= 'when min(place_id) = '.$iPlaceID.' then '.$iOrder.' ';
902                                         }
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']) ";
918                                         $sSQL .= " union ";
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";
930                                         $sSQL .= " union ";
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;
948
949                                         if (PEAR::IsError($aSearchResults))
950                                         {
951                                                 var_dump($sSQL, $aSearchResults);                                       
952                                                 exit;
953                                         }
954                                 }
955                         }
956                 }
957         
958         $sSearchResult = '';
959         if (!sizeof($aSearchResults) && isset($_GET['q']) && $_GET['q'])
960         {
961                 $sSearchResult = 'No Results Found';
962         }
963 //var_Dump($aSearchResults);
964 //exit;
965         $aClassType = getClassTypesWithImportance();
966         foreach($aSearchResults as $iResNum => $aResult)
967         {
968                 if (CONST_Search_AreaPolygons || true)
969                 {
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'].")";
975
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))
982                         {
983                                 var_dump($sSQL, $aPointPolygon);
984                                 exit;
985                         }
986                         if ($aPointPolygon['place_id'])
987                         {
988                                 // Translate geometary string to point array
989                                 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['outlinestring'],$aMatch))
990                                 {
991                                         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
992                                 }
993                                 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['outlinestring'],$aMatch))
994                                 {
995                                         $fRadius = 0.01;
996                                         $iSteps = ($fRadius * 40000)^2;
997                                         $fStepSize = (2*pi())/$iSteps;
998                                         $aPolyPoints = array();
999                                         for($f = 0; $f < 2*pi(); $f += $fStepSize)
1000                                         {
1001                                                 $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1002                                         }
1003                                         $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1004                                         $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1005                                         $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1006                                         $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1007                                 }
1008
1009                                 // Output data suitable for display (points and a bounding box)
1010                                 if ($bShowPolygons)
1011                                 {
1012                                         $aResult['aPolyPoints'] = array();
1013                                         foreach($aPolyPoints as $aPoint)
1014                                         {
1015                                                 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1016                                         }
1017                                 }
1018                                 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1019                         }
1020                 }
1021
1022                 if (!isset($aResult['aBoundingBox']))
1023                 {
1024                         // Default
1025                         $fDiameter = 0.0001;
1026
1027                         if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter']) 
1028                                         && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1029                         {
1030                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1031                         }
1032                         elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter']) 
1033                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1034                         {
1035                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1036                         }
1037                         $fRadius = $fDiameter / 2;
1038
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)
1043                         {
1044                                 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1045                         }
1046                         $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1047                         $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1048                         $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1049                         $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1050
1051                         // Output data suitable for display (points and a bounding box)
1052                         if ($bShowPolygons)
1053                         {
1054                                 $aResult['aPolyPoints'] = array();
1055                                 foreach($aPolyPoints as $aPoint)
1056                                 {
1057                                         $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1058                                 }
1059                         }
1060                         $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1061                 }
1062
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'])
1066                 {
1067                         $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1068                 }
1069
1070                 if ($bShowAddressDetails)
1071                 {
1072                         $aResult['address'] = getAddressDetails($oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1073 //var_dump($aResult['address']);
1074 //exit;
1075                 }
1076
1077 //if (CONST_Debug) var_dump($aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']);
1078 /*
1079                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance']) 
1080                         && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance'])
1081                 {
1082                         $aResult['importance'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['importance'];
1083                 }
1084                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance']) 
1085                         && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1086                 {
1087                         $aResult['importance'] = $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1088                 }
1089                 else
1090                 {
1091                         $aResult['importance'] = 1000000000000000;
1092                 }
1093 */
1094                 $aResult['name'] = $aResult['langaddress'];
1095                 $aResult['foundorder'] = $iResNum;
1096                 $aSearchResults[$iResNum] = $aResult;
1097         }
1098         
1099         uasort($aSearchResults, 'byImportance');
1100
1101 //var_dump($aSearchResults);exit;
1102         
1103         $aOSMIDDone = array();
1104         $aClassTypeNameDone = array();
1105         $aToFilter = $aSearchResults;
1106         $aSearchResults = array();
1107
1108         $bFirst = true;
1109         foreach($aToFilter as $iResNum => $aResult)
1110         {
1111                 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1112                 $aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1113                 if ($bFirst)
1114                 {
1115                         $fLat = $aResult['lat'];
1116                         $fLon = $aResult['lon'];
1117                         if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1118                         $bFirst = false;
1119                 }
1120                 if (!$bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1121                         && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['osm_class'].$aResult['name']])))
1122                 {
1123                         $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1124                         $aClassTypeNameDone[$aResult['osm_type'].$aResult['osm_class'].$aResult['name']] = true;
1125                         $aSearchResults[] = $aResult;
1126                 }
1127         }
1128
1129         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '1 day'::interval,'YYYY/MM/DD') from import_status limit 1");
1130
1131         if (isset($_GET['nearlat']) && isset($_GET['nearlon']))
1132         {
1133                 $sQuery .= ' ['.$_GET['nearlat'].','.$_GET['nearlon'].']';
1134         }
1135
1136         if ($sQuery)
1137         {
1138                 logEnd($oDB, $hLog, sizeof($aToFilter));
1139         }
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'];
1146         if ($sSuggestion)
1147         {
1148                 $sSuggestionURL = $sMoreURL.'&q='.urlencode($sSuggestion);
1149         }
1150         $sMoreURL .= '&q='.urlencode($sQuery);
1151
1152         if (CONST_Debug) exit;
1153
1154         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');