]> git.openstreetmap.org Git - nominatim.git/blob - lib/Geocode.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / Geocode.php
1 <?php
2         class Geocode
3         {
4                 protected $oDB;
5
6                 protected $aLangPrefOrder = array();
7
8                 protected $bIncludeAddressDetails = false;
9                 protected $bIncludeExtraTags = false;
10                 protected $bIncludeNameDetails = false;
11
12                 protected $bIncludePolygonAsPoints = false;
13                 protected $bIncludePolygonAsText = false;
14                 protected $bIncludePolygonAsGeoJSON = false;
15                 protected $bIncludePolygonAsKML = false;
16                 protected $bIncludePolygonAsSVG = false;
17                 protected $fPolygonSimplificationThreshold = 0.0;
18
19                 protected $aExcludePlaceIDs = array();
20                 protected $bDeDupe = true;
21                 protected $bReverseInPlan = true;
22
23                 protected $iLimit = 20;
24                 protected $iFinalLimit = 10;
25                 protected $iOffset = 0;
26                 protected $bFallback = false;
27
28                 protected $aCountryCodes = false;
29                 protected $aNearPoint = false;
30
31                 protected $bBoundedSearch = false;
32                 protected $aViewBox = false;
33                 protected $sViewboxSmallSQL = false;
34                 protected $sViewboxLargeSQL = false;
35                 protected $aRoutePoints = false;
36
37                 protected $iMaxRank = 20;
38                 protected $iMinAddressRank = 0;
39                 protected $iMaxAddressRank = 30;
40                 protected $aAddressRankList = array();
41                 protected $exactMatchCache = array();
42
43                 protected $sAllowedTypesSQLList = false;
44
45                 protected $sQuery = false;
46                 protected $aStructuredQuery = false;
47
48                 function Geocode(&$oDB)
49                 {
50                         $this->oDB =& $oDB;
51                 }
52
53                 function setReverseInPlan($bReverse)
54                 {
55                         $this->bReverseInPlan = $bReverse;
56                 }
57
58                 function setLanguagePreference($aLangPref)
59                 {
60                         $this->aLangPrefOrder = $aLangPref;
61                 }
62
63                 function setIncludeAddressDetails($bAddressDetails = true)
64                 {
65                         $this->bIncludeAddressDetails = (bool)$bAddressDetails;
66                 }
67
68                 function getIncludeAddressDetails()
69                 {
70                         return $this->bIncludeAddressDetails;
71                 }
72
73                 function getIncludeExtraTags()
74                 {
75                         return $this->bIncludeExtraTags;
76                 }
77
78                 function getIncludeNameDetails()
79                 {
80                         return $this->bIncludeNameDetails;
81                 }
82
83                 function setIncludePolygonAsPoints($b = true)
84                 {
85                         $this->bIncludePolygonAsPoints = $b;
86                 }
87
88                 function getIncludePolygonAsPoints()
89                 {
90                         return $this->bIncludePolygonAsPoints;
91                 }
92
93                 function setIncludePolygonAsText($b = true)
94                 {
95                         $this->bIncludePolygonAsText = $b;
96                 }
97
98                 function getIncludePolygonAsText()
99                 {
100                         return $this->bIncludePolygonAsText;
101                 }
102
103                 function setIncludePolygonAsGeoJSON($b = true)
104                 {
105                         $this->bIncludePolygonAsGeoJSON = $b;
106                 }
107
108                 function setIncludePolygonAsKML($b = true)
109                 {
110                         $this->bIncludePolygonAsKML = $b;
111                 }
112
113                 function setIncludePolygonAsSVG($b = true)
114                 {
115                         $this->bIncludePolygonAsSVG = $b;
116                 }
117
118                 function setPolygonSimplificationThreshold($f)
119                 {
120                         $this->fPolygonSimplificationThreshold = $f;
121                 }
122
123                 function setDeDupe($bDeDupe = true)
124                 {
125                         $this->bDeDupe = (bool)$bDeDupe;
126                 }
127
128                 function setLimit($iLimit = 10)
129                 {
130                         if ($iLimit > 50) $iLimit = 50;
131                         if ($iLimit < 1) $iLimit = 1;
132
133                         $this->iFinalLimit = $iLimit;
134                         $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
135                 }
136
137                 function setOffset($iOffset = 0)
138                 {
139                         $this->iOffset = $iOffset;
140                 }
141
142                 function setFallback($bFallback = true)
143                 {
144                         $this->bFallback = (bool)$bFallback;
145                 }
146
147                 function setExcludedPlaceIDs($a)
148                 {
149                         // TODO: force to int
150                         $this->aExcludePlaceIDs = $a;
151                 }
152
153                 function getExcludedPlaceIDs()
154                 {
155                         return $this->aExcludePlaceIDs;
156                 }
157
158                 function setBounded($bBoundedSearch = true)
159                 {
160                         $this->bBoundedSearch = (bool)$bBoundedSearch;
161                 }
162
163                 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
164                 {
165                         $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
166                 }
167
168                 function getViewBoxString()
169                 {
170                         if (!$this->aViewBox) return null;
171                         return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
172                 }
173
174                 function setRoute($aRoutePoints)
175                 {
176                         $this->aRoutePoints = $aRoutePoints;
177                 }
178
179                 function setFeatureType($sFeatureType)
180                 {
181                         switch($sFeatureType)
182                         {
183                         case 'country':
184                                 $this->setRankRange(4, 4);
185                                 break;
186                         case 'state':
187                                 $this->setRankRange(8, 8);
188                                 break;
189                         case 'city':
190                                 $this->setRankRange(14, 16);
191                                 break;
192                         case 'settlement':
193                                 $this->setRankRange(8, 20);
194                                 break;
195                         }
196                 }
197
198                 function setRankRange($iMin, $iMax)
199                 {
200                         $this->iMinAddressRank = (int)$iMin;
201                         $this->iMaxAddressRank = (int)$iMax;
202                 }
203
204                 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
205                 {
206                         $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
207                 }
208
209                 function setCountryCodesList($aCountryCodes)
210                 {
211                         $this->aCountryCodes = $aCountryCodes;
212                 }
213
214                 function setQuery($sQueryString)
215                 {
216                         $this->sQuery = $sQueryString;
217                         $this->aStructuredQuery = false;
218                 }
219
220                 function getQueryString()
221                 {
222                         return $this->sQuery;
223                 }
224
225
226                 function loadParamArray($aParams)
227                 {
228                         if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
229                         if ((float) CONST_Postgresql_Version > 9.2)
230                         {
231                                 if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags'];
232                                 if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails'];
233                         }
234                         if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
235                         if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
236
237                         if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
238                         if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
239
240                         if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
241
242                         // List of excluded Place IDs - used for more acurate pageing
243                         if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
244                         {
245                                 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
246                                 {
247                                         $iExcludedPlaceID = (int)$iExcludedPlaceID;
248                                         if ($iExcludedPlaceID)
249                                                 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
250                                 }
251
252                                 if (isset($aExcludePlaceIDs))
253                                         $this->aExcludePlaceIDs = $aExcludePlaceIDs;
254                         }
255
256                         // Only certain ranks of feature
257                         if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
258                         if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
259
260                         // Country code list
261                         if (isset($aParams['countrycodes']))
262                         {
263                                 $aCountryCodes = array();
264                                 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
265                                 {
266                                         if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
267                                         {
268                                                 $aCountryCodes[] = strtolower($sCountryCode);
269                                         }
270                                 }
271                                 $this->aCountryCodes = $aCountryCodes;
272                         }
273
274                         if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
275                         {
276                                 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
277                                 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
278                         }
279                         else if (isset($aParams['viewbox']) && $aParams['viewbox'])
280                         {
281                                 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
282                                 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
283                         }
284
285                         if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
286                         {
287                                 $aPoints = explode(',',$aParams['route']);
288                                 if (sizeof($aPoints) % 2 != 0)
289                                 {
290                                         userError("Uneven number of points");
291                                         exit;
292                                 }
293                                 $fPrevCoord = false;
294                                 $aRoute = array();
295                                 foreach($aPoints as $i => $fPoint)
296                                 {
297                                         if ($i%2)
298                                         {
299                                                 $aRoute[] = array((float)$fPoint, $fPrevCoord);
300                                         }
301                                         else
302                                         {
303                                                 $fPrevCoord = (float)$fPoint;
304                                         }
305                                 }
306                                 $this->aRoutePoints = $aRoute;
307                         }
308                 }
309
310                 function setQueryFromParams($aParams)
311                 {
312                         // Search query
313                         $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
314                         if (!$sQuery)
315                         {
316                                 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
317                                 $this->setReverseInPlan(false);
318                         }
319                         else
320                         {
321                                 $this->setQuery($sQuery);
322                         }
323                 }
324
325                 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
326                 {
327                         $sValue = trim($sValue);
328                         if (!$sValue) return false;
329                         $this->aStructuredQuery[$sKey] = $sValue;
330                         if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
331                         {
332                                 $this->iMinAddressRank = $iNewMinAddressRank;
333                                 $this->iMaxAddressRank = $iNewMaxAddressRank;
334                         }
335                         if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
336                         return true;
337                 }
338
339                 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
340                 {
341                         $this->sQuery = false;
342
343                         // Reset
344                         $this->iMinAddressRank = 0;
345                         $this->iMaxAddressRank = 30;
346                         $this->aAddressRankList = array();
347
348                         $this->aStructuredQuery = array();
349                         $this->sAllowedTypesSQLList = '';
350
351                         $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
352                         $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
353                         $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
354                         $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
355                         $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
356                         $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
357                         $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
358
359                         if (sizeof($this->aStructuredQuery) > 0) 
360                         {
361                                 $this->sQuery = join(', ', $this->aStructuredQuery);
362                                 if ($this->iMaxAddressRank < 30)
363                                 {
364                                         $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
365                                 }
366                         }
367                 }
368
369                 function fallbackStructuredQuery()
370                 {
371                         if (!$this->aStructuredQuery) return false;
372
373                         $aParams = $this->aStructuredQuery;
374
375                         if (sizeof($aParams) == 1) return false;
376
377                         $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
378
379                         foreach($aOrderToFallback as $sType)
380                         {
381                                 if (isset($aParams[$sType]))
382                                 {
383                                         unset($aParams[$sType]);
384                                         $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
385                                         return true;
386                                 }
387                         }
388
389                         return false;
390                 }
391
392                 function getDetails($aPlaceIDs)
393                 {
394                         if (sizeof($aPlaceIDs) == 0)  return array();
395
396                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
397
398                         // Get the details for display (is this a redundant extra step?)
399                         $sPlaceIDs = join(',',$aPlaceIDs);
400
401                         $sImportanceSQL = '';
402                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
403                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
404
405                         $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as country_code,";
406                         $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
407                         $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
408                         $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
409                         if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text as extra,";
410                         if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
411                         $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
412                         $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
413                         $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
414                         $sSQL .= "(extratags->'place') as extra_place ";
415                         $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
416                         $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
417                         if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
418                         if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
419                         $sSQL .= ") ";
420                         if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
421                         $sSQL .= "and linked_place_id is null ";
422                         $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
423                         if (!$this->bDeDupe) $sSQL .= ",place_id";
424                         $sSQL .= ",langaddress ";
425                         $sSQL .= ",placename ";
426                         $sSQL .= ",ref ";
427                         if ($this->bIncludeExtraTags) $sSQL .= ",extratags";
428                         if ($this->bIncludeNameDetails) $sSQL .= ",name";
429                         $sSQL .= ",extratags->'place' ";
430
431                         if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
432                         {
433                                 $sSQL .= " union ";
434                                 $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id,'us' as country_code,";
435                                 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
436                                 $sSQL .= "null as placename,";
437                                 $sSQL .= "null as ref,";
438                                 if ($this->bIncludeExtraTags) $sSQL .= "null as extra,";
439                                 if ($this->bIncludeNameDetails) $sSQL .= "null as names,";
440                                 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
441                                 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
442                                 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
443                                 $sSQL .= "null as extra_place ";
444                                 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
445                                 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
446                                 $sSQL .= "group by place_id";
447                                 if (!$this->bDeDupe) $sSQL .= ",place_id ";
448                                 /*
449                                 $sSQL .= " union ";
450                                 $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, min(parent_place_id) as parent_place_id,'us' as country_code,";
451                                 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
452                                 $sSQL .= "null as placename,";
453                                 $sSQL .= "null as ref,";
454                                 if ($this->bIncludeExtraTags) $sSQL .= "null as extra,";
455                                 if ($this->bIncludeNameDetails) $sSQL .= "null as names,";
456                                 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
457                                 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
458                                 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
459                                 $sSQL .= "null as extra_place ";
460                                 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
461                                 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
462                                 $sSQL .= "group by place_id";
463                                 if (!$this->bDeDupe) $sSQL .= ",place_id";
464                                 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
465                                 */
466                         }
467
468                         $sSQL .= " order by importance desc";
469                         if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
470                         $aSearchResults = $this->oDB->getAll($sSQL);
471
472                         if (PEAR::IsError($aSearchResults))
473                         {
474                                 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
475                         }
476
477                         return $aSearchResults;
478                 }
479
480                 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
481                 {
482                         /*
483                            Calculate all searches using aValidTokens i.e.
484                            'Wodsworth Road, Sheffield' =>
485
486                            Phrase Wordset
487                            0      0       (wodsworth road)
488                            0      1       (wodsworth)(road)
489                            1      0       (sheffield)
490
491                            Score how good the search is so they can be ordered
492                          */
493                         foreach($aPhrases as $iPhrase => $sPhrase)
494                         {
495                                 $aNewPhraseSearches = array();
496                                 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
497                                 else $sPhraseType = '';
498
499                                 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
500                                 {
501                                         // Too many permutations - too expensive
502                                         if ($iWordSet > 120) break;
503
504                                         $aWordsetSearches = $aSearches;
505
506                                         // Add all words from this wordset
507                                         foreach($aWordset as $iToken => $sToken)
508                                         {
509                                                 //echo "<br><b>$sToken</b>";
510                                                 $aNewWordsetSearches = array();
511
512                                                 foreach($aWordsetSearches as $aCurrentSearch)
513                                                 {
514                                                         //echo "<i>";
515                                                         //var_dump($aCurrentSearch);
516                                                         //echo "</i>";
517
518                                                         // If the token is valid
519                                                         if (isset($aValidTokens[' '.$sToken]))
520                                                         {
521                                                                 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
522                                                                 {
523                                                                         $aSearch = $aCurrentSearch;
524                                                                         $aSearch['iSearchRank']++;
525                                                                         if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
526                                                                         {
527                                                                                 if ($aSearch['sCountryCode'] === false)
528                                                                                 {
529                                                                                         $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
530                                                                                         // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
531                                                                                         if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
532                                                                                         {
533                                                                                                 $aSearch['iSearchRank'] += 5;
534                                                                                         }
535                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
536                                                                                 }
537                                                                         }
538                                                                         elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
539                                                                         {
540                                                                                 if ($aSearch['fLat'] === '')
541                                                                                 {
542                                                                                         $aSearch['fLat'] = $aSearchTerm['lat'];
543                                                                                         $aSearch['fLon'] = $aSearchTerm['lon'];
544                                                                                         $aSearch['fRadius'] = $aSearchTerm['radius'];
545                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
546                                                                                 }
547                                                                         }
548                                                                         elseif ($sPhraseType == 'postalcode')
549                                                                         {
550                                                                                 // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
551                                                                                 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
552                                                                                 {
553                                                                                         // If we already have a name try putting the postcode first
554                                                                                         if (sizeof($aSearch['aName']))
555                                                                                         {
556                                                                                                 $aNewSearch = $aSearch;
557                                                                                                 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
558                                                                                                 $aNewSearch['aName'] = array();
559                                                                                                 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
560                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
561                                                                                         }
562
563                                                                                         if (sizeof($aSearch['aName']))
564                                                                                         {
565                                                                                                 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
566                                                                                                 {
567                                                                                                         $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
568                                                                                                 }
569                                                                                                 else
570                                                                                                 {
571                                                                                                         $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
572                                                                                                         $aSearch['iSearchRank'] += 1000; // skip;
573                                                                                                 }
574                                                                                         }
575                                                                                         else
576                                                                                         {
577                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
578                                                                                                 //$aSearch['iNamePhrase'] = $iPhrase;
579                                                                                         }
580                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
581                                                                                 }
582
583                                                                         }
584                                                                         elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
585                                                                         {
586                                                                                 if ($aSearch['sHouseNumber'] === '')
587                                                                                 {
588                                                                                         $aSearch['sHouseNumber'] = $sToken;
589                                                                                         // sanity check: if the housenumber is not mainly made
590                                                                                         // up of numbers, add a penalty
591                                                                                         if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
592                                                                                         // also housenumbers should appear in the first or second phrase
593                                                                                         if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
594                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
595                                                                                         /*
596                                                                                         // Fall back to not searching for this item (better than nothing)
597                                                                                         $aSearch = $aCurrentSearch;
598                                                                                         $aSearch['iSearchRank'] += 1;
599                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
600                                                                                          */
601                                                                                 }
602                                                                         }
603                                                                         elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
604                                                                         {
605                                                                                 if ($aSearch['sClass'] === '')
606                                                                                 {
607                                                                                         $aSearch['sOperator'] = $aSearchTerm['operator'];
608                                                                                         $aSearch['sClass'] = $aSearchTerm['class'];
609                                                                                         $aSearch['sType'] = $aSearchTerm['type'];
610                                                                                         if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
611                                                                                         else $aSearch['sOperator'] = 'near'; // near = in for the moment
612                                                                                         if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
613
614                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
615                                                                                 }
616                                                                         }
617                                                                         elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
618                                                                         {
619                                                                                 if (sizeof($aSearch['aName']))
620                                                                                 {
621                                                                                         if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
622                                                                                         {
623                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
624                                                                                         }
625                                                                                         else
626                                                                                         {
627                                                                                                 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
628                                                                                                 $aSearch['iSearchRank'] += 1000; // skip;
629                                                                                         }
630                                                                                 }
631                                                                                 else
632                                                                                 {
633                                                                                         $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
634                                                                                         //$aSearch['iNamePhrase'] = $iPhrase;
635                                                                                 }
636                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
637                                                                         }
638                                                                 }
639                                                         }
640                                                         // Look for partial matches.
641                                                         // Note that there is no point in adding country terms here
642                                                         // because country are omitted in the address.
643                                                         if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
644                                                         {
645                                                                 // Allow searching for a word - but at extra cost
646                                                                 foreach($aValidTokens[$sToken] as $aSearchTerm)
647                                                                 {
648                                                                         if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
649                                                                         {
650                                                                                 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
651                                                                                 {
652                                                                                         $aSearch = $aCurrentSearch;
653                                                                                         $aSearch['iSearchRank'] += 1;
654                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
655                                                                                         {
656                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
657                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
658                                                                                         }
659                                                                                         elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
660                                                                                         {
661                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
662                                                                                                 $aSearch['iSearchRank'] += 1;
663                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
664                                                                                                 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
665                                                                                                 {
666                                                                                                         if (empty($aSearchTermToken['country_code'])
667                                                                                                                         && empty($aSearchTermToken['lat'])
668                                                                                                                         && empty($aSearchTermToken['class']))
669                                                                                                         {
670                                                                                                                 $aSearch = $aCurrentSearch;
671                                                                                                                 $aSearch['iSearchRank'] += 1;
672                                                                                                                 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
673                                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
674                                                                                                         }
675                                                                                                 }
676                                                                                         }
677                                                                                         else
678                                                                                         {
679                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
680                                                                                                 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
681                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
682                                                                                         }
683                                                                                 }
684
685                                                                                 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
686                                                                                 {
687                                                                                         $aSearch = $aCurrentSearch;
688                                                                                         $aSearch['iSearchRank'] += 1;
689                                                                                         if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
690                                                                                         if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
691                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
692                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
693                                                                                         else
694                                                                                                 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
695                                                                                         $aSearch['iNamePhrase'] = $iPhrase;
696                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
697                                                                                 }
698                                                                         }
699                                                                 }
700                                                         }
701                                                         else
702                                                         {
703                                                                 // Allow skipping a word - but at EXTREAM cost
704                                                                 //$aSearch = $aCurrentSearch;
705                                                                 //$aSearch['iSearchRank']+=100;
706                                                                 //$aNewWordsetSearches[] = $aSearch;
707                                                         }
708                                                 }
709                                                 // Sort and cut
710                                                 usort($aNewWordsetSearches, 'bySearchRank');
711                                                 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
712                                         }
713                                         //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
714
715                                         $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
716                                         usort($aNewPhraseSearches, 'bySearchRank');
717
718                                         $aSearchHash = array();
719                                         foreach($aNewPhraseSearches as $iSearch => $aSearch)
720                                         {
721                                                 $sHash = serialize($aSearch);
722                                                 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
723                                                 else $aSearchHash[$sHash] = 1;
724                                         }
725
726                                         $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
727                                 }
728
729                                 // Re-group the searches by their score, junk anything over 20 as just not worth trying
730                                 $aGroupedSearches = array();
731                                 foreach($aNewPhraseSearches as $aSearch)
732                                 {
733                                         if ($aSearch['iSearchRank'] < $this->iMaxRank)
734                                         {
735                                                 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
736                                                 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
737                                         }
738                                 }
739                                 ksort($aGroupedSearches);
740
741                                 $iSearchCount = 0;
742                                 $aSearches = array();
743                                 foreach($aGroupedSearches as $iScore => $aNewSearches)
744                                 {
745                                         $iSearchCount += sizeof($aNewSearches);
746                                         $aSearches = array_merge($aSearches, $aNewSearches);
747                                         if ($iSearchCount > 50) break;
748                                 }
749
750                                 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
751
752                         }
753                         return $aGroupedSearches;
754
755                 }
756
757                 /* Perform the actual query lookup.
758
759                         Returns an ordered list of results, each with the following fields:
760                           osm_type: type of corresponding OSM object
761                                                         N - node
762                                                         W - way
763                                                         R - relation
764                                                         P - postcode (internally computed)
765                           osm_id: id of corresponding OSM object
766                           class: general object class (corresponds to tag key of primary OSM tag)
767                           type: subclass of object (corresponds to tag value of primary OSM tag)
768                           admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
769                           rank_search: rank in search hierarchy
770                                                         (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
771                           rank_address: rank in address hierarchy (determines orer in address)
772                           place_id: internal key (may differ between different instances)
773                           country_code: ISO country code
774                           langaddress: localized full address
775                           placename: localized name of object
776                           ref: content of ref tag (if available)
777                           lon: longitude
778                           lat: latitude
779                           importance: importance of place based on Wikipedia link count
780                           addressimportance: cumulated importance of address elements
781                           extra_place: type of place (for admin boundaries, if there is a place tag)
782                           aBoundingBox: bounding Box
783                           label: short description of the object class/type (English only) 
784                           name: full name (currently the same as langaddress)
785                           foundorder: secondary ordering for places with same importance
786                 */
787                 function lookup()
788                 {
789                         if (!$this->sQuery && !$this->aStructuredQuery) return false;
790
791                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
792                         $sCountryCodesSQL = false;
793                         if ($this->aCountryCodes && sizeof($this->aCountryCodes))
794                         {
795                                 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
796                         }
797
798                         $sQuery = $this->sQuery;
799
800                         // Conflicts between US state abreviations and various words for 'the' in different languages
801                         if (isset($this->aLangPrefOrder['name:en']))
802                         {
803                                 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
804                                 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
805                                 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
806                         }
807
808                         // View Box SQL
809                         $sViewboxCentreSQL = false;
810                         $bBoundingBoxSearch = false;
811                         if ($this->aViewBox)
812                         {
813                                 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
814                                 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
815                                 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
816                                 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
817                                 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
818                                 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
819
820                                 $this->sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$this->aViewBox[0].",".(float)$this->aViewBox[1]."),ST_Point(".(float)$this->aViewBox[2].",".(float)$this->aViewBox[3].")),4326)";
821                                 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
822                                 $bBoundingBoxSearch = $this->bBoundedSearch;
823                         }
824
825                         // Route SQL
826                         if ($this->aRoutePoints)
827                         {
828                                 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
829                                 $bFirst = true;
830                                 foreach($this->aRoutePoints as $aPoint)
831                                 {
832                                         if (!$bFirst) $sViewboxCentreSQL .= ",";
833                                         $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
834                                         $bFirst = false;
835                                 }
836                                 $sViewboxCentreSQL .= ")'::geometry,4326)";
837
838                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
839                                 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
840                                 if (PEAR::isError($this->sViewboxSmallSQL))
841                                 {
842                                         failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
843                                 }
844                                 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
845
846                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
847                                 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
848                                 if (PEAR::isError($this->sViewboxLargeSQL))
849                                 {
850                                         failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
851                                 }
852                                 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
853                                 $bBoundingBoxSearch = $this->bBoundedSearch;
854                         }
855
856                         // Do we have anything that looks like a lat/lon pair?
857                         if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
858                                 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
859                                 $sQuery = $aLooksLike['query'];                 
860                         }
861
862                         $aSearchResults = array();
863                         if ($sQuery || $this->aStructuredQuery)
864                         {
865                                 // Start with a blank search
866                                 $aSearches = array(
867                                         array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
868                                               'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
869                                               'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
870                                 );
871
872                                 // Do we have a radius search?
873                                 $sNearPointSQL = false;
874                                 if ($this->aNearPoint)
875                                 {
876                                         $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
877                                         $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
878                                         $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
879                                         $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
880                                 }
881
882                                 // Any 'special' terms in the search?
883                                 $bSpecialTerms = false;
884                                 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
885                                 $aSpecialTerms = array();
886                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
887                                 {
888                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
889                                         $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
890                                 }
891
892                                 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
893                                 $aSpecialTerms = array();
894                                 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity'])
895                                 {
896                                         $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
897                                         unset($this->aStructuredQuery['amenity']);
898                                 }
899                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
900                                 {
901                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
902                                         $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
903                                         $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
904                                         $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class not in (\'place\')) or country_code is not null';
905                                         if (CONST_Debug) var_Dump($sSQL);
906                                         $aSearchWords = $this->oDB->getAll($sSQL);
907                                         $aNewSearches = array();
908                                         foreach($aSearches as $aSearch)
909                                         {
910                                                 foreach($aSearchWords as $aSearchTerm)
911                                                 {
912                                                         $aNewSearch = $aSearch;
913                                                         if ($aSearchTerm['country_code'])
914                                                         {
915                                                                 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
916                                                                 $aNewSearches[] = $aNewSearch;
917                                                                 $bSpecialTerms = true;
918                                                         }
919                                                         if ($aSearchTerm['class'])
920                                                         {
921                                                                 $aNewSearch['sClass'] = $aSearchTerm['class'];
922                                                                 $aNewSearch['sType'] = $aSearchTerm['type'];
923                                                                 $aNewSearches[] = $aNewSearch;
924                                                                 $bSpecialTerms = true;
925                                                         }
926                                                 }
927                                         }
928                                         $aSearches = $aNewSearches;
929                                 }
930
931                                 // Split query into phrases
932                                 // Commas are used to reduce the search space by indicating where phrases split
933                                 if ($this->aStructuredQuery)
934                                 {
935                                         $aPhrases = $this->aStructuredQuery;
936                                         $bStructuredPhrases = true;
937                                 }
938                                 else
939                                 {
940                                         $aPhrases = explode(',',$sQuery);
941                                         $bStructuredPhrases = false;
942                                 }
943
944                                 // Convert each phrase to standard form
945                                 // Create a list of standard words
946                                 // Get all 'sets' of words
947                                 // Generate a complete list of all
948                                 $aTokens = array();
949                                 foreach($aPhrases as $iPhrase => $sPhrase)
950                                 {
951                                         $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
952                                         if (PEAR::isError($aPhrase))
953                                         {
954                                                 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
955                                                 if (CONST_Debug) var_dump($aPhrase);
956                                                 exit;
957                                         }
958                                         if (trim($aPhrase['string']))
959                                         {
960                                                 $aPhrases[$iPhrase] = $aPhrase;
961                                                 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
962                                                 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
963                                                 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
964                                         }
965                                         else
966                                         {
967                                                 unset($aPhrases[$iPhrase]);
968                                         }
969                                 }
970
971                                 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
972                                 $aPhraseTypes = array_keys($aPhrases);
973                                 $aPhrases = array_values($aPhrases);
974
975                                 if (sizeof($aTokens))
976                                 {
977                                         // Check which tokens we have, get the ID numbers
978                                         $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
979                                         $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
980
981                                         if (CONST_Debug) var_Dump($sSQL);
982
983                                         $aValidTokens = array();
984                                         if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
985                                         else $aDatabaseWords = array();
986                                         if (PEAR::IsError($aDatabaseWords))
987                                         {
988                                                 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
989                                         }
990                                         $aPossibleMainWordIDs = array();
991                                         $aWordFrequencyScores = array();
992                                         foreach($aDatabaseWords as $aToken)
993                                         {
994                                                 // Very special case - require 2 letter country param to match the country code found
995                                                 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
996                                                                 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code'])
997                                                 {
998                                                         continue;
999                                                 }
1000
1001                                                 if (isset($aValidTokens[$aToken['word_token']]))
1002                                                 {
1003                                                         $aValidTokens[$aToken['word_token']][] = $aToken;
1004                                                 }
1005                                                 else
1006                                                 {
1007                                                         $aValidTokens[$aToken['word_token']] = array($aToken);
1008                                                 }
1009                                                 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1010                                                 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1011                                         }
1012                                         if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1013
1014                                         // Try and calculate GB postcodes we might be missing
1015                                         foreach($aTokens as $sToken)
1016                                         {
1017                                                 // Source of gb postcodes is now definitive - always use
1018                                                 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
1019                                                 {
1020                                                         if (substr($aData[1],-2,1) != ' ')
1021                                                         {
1022                                                                 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1023                                                                 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1024                                                         }
1025                                                         $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1026                                                         if ($aGBPostcodeLocation)
1027                                                         {
1028                                                                 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1029                                                         }
1030                                                 }
1031                                                 // US ZIP+4 codes - if there is no token,
1032                                                 //      merge in the 5-digit ZIP code
1033                                                 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1034                                                 {
1035                                                         if (isset($aValidTokens[$aData[1]]))
1036                                                         {
1037                                                                 foreach($aValidTokens[$aData[1]] as $aToken)
1038                                                                 {
1039                                                                         if (!$aToken['class'])
1040                                                                         {
1041                                                                                 if (isset($aValidTokens[$sToken]))
1042                                                                                 {
1043                                                                                         $aValidTokens[$sToken][] = $aToken;
1044                                                                                 }
1045                                                                                 else
1046                                                                                 {
1047                                                                                         $aValidTokens[$sToken] = array($aToken);
1048                                                                                 }
1049                                                                         }
1050                                                                 }
1051                                                         }
1052                                                 }
1053                                         }
1054
1055                                         foreach($aTokens as $sToken)
1056                                         {
1057                                                 // Unknown single word token with a number - assume it is a house number
1058                                                 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1059                                                 {
1060                                                         $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1061                                                 }
1062                                         }
1063
1064                                         // Any words that have failed completely?
1065                                         // TODO: suggestions
1066
1067                                         // Start the search process
1068                                         $aResultPlaceIDs = array();
1069
1070                                         $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1071
1072                                         if ($this->bReverseInPlan)
1073                                         {
1074                                                 // Reverse phrase array and also reverse the order of the wordsets in
1075                                                 // the first and final phrase. Don't bother about phrases in the middle
1076                                                 // because order in the address doesn't matter.
1077                                                 $aPhrases = array_reverse($aPhrases);
1078                                                 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1079                                                 if (sizeof($aPhrases) > 1)
1080                                                 {
1081                                                         $aFinalPhrase = end($aPhrases);
1082                                                         $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1083                                                 }
1084                                                 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1085
1086                                                 foreach($aGroupedSearches as $aSearches)
1087                                                 {
1088                                                         foreach($aSearches as $aSearch)
1089                                                         {
1090                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1091                                                                 {
1092                                                                         if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1093                                                                         $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1094                                                                 }
1095
1096                                                         }
1097                                                 }
1098
1099                                                 $aGroupedSearches = $aReverseGroupedSearches;
1100                                                 ksort($aGroupedSearches);
1101                                         }
1102                                 }
1103                                 else
1104                                 {
1105                                         // Re-group the searches by their score, junk anything over 20 as just not worth trying
1106                                         $aGroupedSearches = array();
1107                                         foreach($aSearches as $aSearch)
1108                                         {
1109                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1110                                                 {
1111                                                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1112                                                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1113                                                 }
1114                                         }
1115                                         ksort($aGroupedSearches);
1116                                 }
1117
1118                                 if (CONST_Debug) var_Dump($aGroupedSearches);
1119
1120                                 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0)
1121                                 {
1122                                         $aCopyGroupedSearches = $aGroupedSearches;
1123                                         foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1124                                         {
1125                                                 foreach($aSearches as $iSearch => $aSearch)
1126                                                 {
1127                                                         $aReductionsList = array($aSearch['aAddress']);
1128                                                         $iSearchRank = $aSearch['iSearchRank'];
1129                                                         while(sizeof($aReductionsList) > 0)
1130                                                         {
1131                                                                 $iSearchRank += 5;
1132                                                                 if ($iSearchRank > iMaxRank) break 3;
1133                                                                 $aNewReductionsList = array();
1134                                                                 foreach($aReductionsList as $aReductionsWordList)
1135                                                                 {
1136                                                                         for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1137                                                                         {
1138                                                                                 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1139                                                                                 $aReverseSearch = $aSearch;
1140                                                                                 $aSearch['aAddress'] = $aReductionsWordListResult;
1141                                                                                 $aSearch['iSearchRank'] = $iSearchRank;
1142                                                                                 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1143                                                                                 if (sizeof($aReductionsWordListResult) > 0)
1144                                                                                 {
1145                                                                                         $aNewReductionsList[] = $aReductionsWordListResult;
1146                                                                                 }
1147                                                                         }
1148                                                                 }
1149                                                                 $aReductionsList = $aNewReductionsList;
1150                                                         }
1151                                                 }
1152                                         }
1153                                         ksort($aGroupedSearches);
1154                                 }
1155
1156                                 // Filter out duplicate searches
1157                                 $aSearchHash = array();
1158                                 foreach($aGroupedSearches as $iGroup => $aSearches)
1159                                 {
1160                                         foreach($aSearches as $iSearch => $aSearch)
1161                                         {
1162                                                 $sHash = serialize($aSearch);
1163                                                 if (isset($aSearchHash[$sHash]))
1164                                                 {
1165                                                         unset($aGroupedSearches[$iGroup][$iSearch]);
1166                                                         if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1167                                                 }
1168                                                 else
1169                                                 {
1170                                                         $aSearchHash[$sHash] = 1;
1171                                                 }
1172                                         }
1173                                 }
1174
1175                                 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1176
1177                                 $iGroupLoop = 0;
1178                                 $iQueryLoop = 0;
1179                                 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1180                                 {
1181                                         $iGroupLoop++;
1182                                         foreach($aSearches as $aSearch)
1183                                         {
1184                                                 $iQueryLoop++;
1185
1186                                                 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1187                                                 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1188
1189                                                 // No location term?
1190                                                 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1191                                                 {
1192                                                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1193                                                         {
1194                                                                 // Just looking for a country by code - look it up
1195                                                                 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1196                                                                 {
1197                                                                         $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1198                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1199                                     if ($bBoundingBoxSearch)
1200                                         $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1201                                                                         $sSQL .= " order by st_area(geometry) desc limit 1";
1202                                                                         if (CONST_Debug) var_dump($sSQL);
1203                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1204                                                                 }
1205                                                                 else
1206                                                                 {
1207                                                                         $aPlaceIDs = array();
1208                                                                 }
1209                                                         }
1210                                                         else
1211                                                         {
1212                                                                 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1213                                                                 if (!$aSearch['sClass']) continue;
1214                                                                 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1215                                                                 if ($this->oDB->getOne($sSQL))
1216                                                                 {
1217                                                                         $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1218                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1219                                                                         $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1220                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1221                                                                         if (sizeof($this->aExcludePlaceIDs))
1222                                                                         {
1223                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1224                                                                         }
1225                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1226                                                                         $sSQL .= " limit $this->iLimit";
1227                                                                         if (CONST_Debug) var_dump($sSQL);
1228                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1229
1230                                                                         // If excluded place IDs are given, it is fair to assume that
1231                                                                         // there have been results in the small box, so no further
1232                                                                         // expansion in that case.
1233                                                                         // Also don't expand if bounded results were requested.
1234                                                                         if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1235                                                                         {
1236                                                                                 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1237                                                                                 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1238                                                                                 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1239                                                                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1240                                                                                 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1241                                                                                 $sSQL .= " limit $this->iLimit";
1242                                                                                 if (CONST_Debug) var_dump($sSQL);
1243                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1244                                                                         }
1245                                                                 }
1246                                                                 else
1247                                                                 {
1248                                                                         $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1249                                                                         $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1250                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1251                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1252                                                                         $sSQL .= " limit $this->iLimit";
1253                                                                         if (CONST_Debug) var_dump($sSQL);
1254                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1255                                                                 }
1256                                                         }
1257                                                 }
1258                                                 else
1259                                                 {
1260                                                         $aPlaceIDs = array();
1261
1262                                                         // First we need a position, either aName or fLat or both
1263                                                         $aTerms = array();
1264                                                         $aOrder = array();
1265
1266                                                         if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1267                                                         {
1268                                                                 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1269                                                                 $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
1270                                                         }
1271
1272                                                         // TODO: filter out the pointless search terms (2 letter name tokens and less)
1273                                                         // they might be right - but they are just too darned expensive to run
1274                                                         if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1275                                                         //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1276                                                         if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1277                                                         {
1278                                                                 // For infrequent name terms disable index usage for address
1279                                                                 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1280                                                                                 sizeof($aSearch['aName']) == 1 &&
1281                                                                                 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1282                                                                 {
1283                                                                         //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1284                                                                         $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1285                                                                 }
1286                                                                 else
1287                                                                 {
1288                                                                         $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1289                                                                         //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1290                                                                 }
1291                                                         }
1292                                                         if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1293                                                         if ($aSearch['sHouseNumber'])
1294                                                         {
1295                                                                 $aTerms[] = "address_rank between 16 and 27";
1296                                                         }
1297                                                         else
1298                                                         {
1299                                                                 if ($this->iMinAddressRank > 0)
1300                                                                 {
1301                                                                         $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1302                                                                 }
1303                                                                 if ($this->iMaxAddressRank < 30)
1304                                                                 {
1305                                                                         $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1306                                                                 }
1307                                                         }
1308                                                         if ($aSearch['fLon'] && $aSearch['fLat'])
1309                                                         {
1310                                                                 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1311                                                                 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1312                                                         }
1313                                                         if (sizeof($this->aExcludePlaceIDs))
1314                                                         {
1315                                                                 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1316                                                         }
1317                                                         if ($sCountryCodesSQL)
1318                                                         {
1319                                                                 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1320                                                         }
1321
1322                                                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1323                                                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1324
1325                                                         if ($aSearch['sHouseNumber'])
1326                                                         {
1327                                                                 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1328                                                         }
1329                                                         else
1330                                                         {
1331                                                                 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1332                                                         }
1333                                                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1334                                                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1335
1336                                                         $aOrder[] = "$sImportanceSQL DESC";
1337                                                         if (sizeof($aSearch['aFullNameAddress']))
1338                                                         {
1339                                                                 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1340                                                                 $aOrder[] = 'exactmatch DESC';
1341                                                         } else {
1342                                                                 $sExactMatchSQL = '0::int as exactmatch';
1343                                                         }
1344
1345                                                         if (sizeof($aTerms))
1346                                                         {
1347                                                                 $sSQL = "select place_id, ";
1348                                                                 $sSQL .= $sExactMatchSQL;
1349                                                                 $sSQL .= " from search_name";
1350                                                                 $sSQL .= " where ".join(' and ',$aTerms);
1351                                                                 $sSQL .= " order by ".join(', ',$aOrder);
1352                                                                 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1353                                                                         $sSQL .= " limit 20";
1354                                                                 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1355                                                                         $sSQL .= " limit 1";
1356                                                                 else
1357                                                                         $sSQL .= " limit ".$this->iLimit;
1358
1359                                                                 if (CONST_Debug) { var_dump($sSQL); }
1360                                                                 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1361                                                                 if (PEAR::IsError($aViewBoxPlaceIDs))
1362                                                                 {
1363                                                                         failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1364                                                                 }
1365                                                                 //var_dump($aViewBoxPlaceIDs);
1366                                                                 // Did we have an viewbox matches?
1367                                                                 $aPlaceIDs = array();
1368                                                                 $bViewBoxMatch = false;
1369                                                                 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1370                                                                 {
1371                                                                         //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1372                                                                         //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1373                                                                         //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1374                                                                         //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1375                                                                         $aPlaceIDs[] = $aViewBoxRow['place_id'];
1376                                                                         $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1377                                                                 }
1378                                                         }
1379                                                         //var_Dump($aPlaceIDs);
1380                                                         //exit;
1381
1382                                                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1383                                                         {
1384                                                                 $aRoadPlaceIDs = $aPlaceIDs;
1385                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1386
1387                                                                 // Now they are indexed look for a house attached to a street we found
1388                                                                 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1389                                                                 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1390                                                                 if (sizeof($this->aExcludePlaceIDs))
1391                                                                 {
1392                                                                         $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1393                                                                 }
1394                                                                 $sSQL .= " limit $this->iLimit";
1395                                                                 if (CONST_Debug) var_dump($sSQL);
1396                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1397
1398                                                                 // If not try the aux fallback table
1399                                                                 /*
1400                                                                 if (!sizeof($aPlaceIDs))
1401                                                                 {
1402                                                                         $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1403                                                                         if (sizeof($this->aExcludePlaceIDs))
1404                                                                         {
1405                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1406                                                                         }
1407                                                                         //$sSQL .= " limit $this->iLimit";
1408                                                                         if (CONST_Debug) var_dump($sSQL);
1409                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1410                                                                 }
1411                                                                 */
1412
1413                                                                 if (!sizeof($aPlaceIDs))
1414                                                                 {
1415                                                                         $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1416                                                                         if (sizeof($this->aExcludePlaceIDs))
1417                                                                         {
1418                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1419                                                                         }
1420                                                                         //$sSQL .= " limit $this->iLimit";
1421                                                                         if (CONST_Debug) var_dump($sSQL);
1422                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1423                                                                 }
1424
1425                                                                 // Fallback to the road
1426                                                                 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1427                                                                 {
1428                                                                         $aPlaceIDs = $aRoadPlaceIDs;
1429                                                                 }
1430
1431                                                         }
1432
1433                                                         if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1434                                                         {
1435                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1436                                                                 $aClassPlaceIDs = array();
1437
1438                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1439                                                                 {
1440                                                                         // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1441                                                                         $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1442                                                                         $sSQL .= " and linked_place_id is null";
1443                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1444                                                                         $sSQL .= " order by rank_search asc limit $this->iLimit";
1445                                                                         if (CONST_Debug) var_dump($sSQL);
1446                                                                         $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1447                                                                 }
1448
1449                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1450                                                                 {
1451                                                                         $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1452                                                                         $bCacheTable = $this->oDB->getOne($sSQL);
1453
1454                                                                         $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1455
1456                                                                         if (CONST_Debug) var_dump($sSQL);
1457                                                                         $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1458
1459                                                                         // For state / country level searches the normal radius search doesn't work very well
1460                                                                         $sPlaceGeom = false;
1461                                                                         if ($this->iMaxRank < 9 && $bCacheTable)
1462                                                                         {
1463                                                                                 // Try and get a polygon to search in instead
1464                                                                                 $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
1465                                                                                 if (CONST_Debug) var_dump($sSQL);
1466                                                                                 $sPlaceGeom = $this->oDB->getOne($sSQL);
1467                                                                         }
1468
1469                                                                         if ($sPlaceGeom)
1470                                                                         {
1471                                                                                 $sPlaceIDs = false;
1472                                                                         }
1473                                                                         else
1474                                                                         {
1475                                                                                 $this->iMaxRank += 5;
1476                                                                                 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1477                                                                                 if (CONST_Debug) var_dump($sSQL);
1478                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1479                                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1480                                                                         }
1481
1482                                                                         if ($sPlaceIDs || $sPlaceGeom)
1483                                                                         {
1484
1485                                                                                 $fRange = 0.01;
1486                                                                                 if ($bCacheTable)
1487                                                                                 {
1488                                                                                         // More efficient - can make the range bigger
1489                                                                                         $fRange = 0.05;
1490
1491                                                                                         $sOrderBySQL = '';
1492                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1493                                                                                         else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1494                                                                                         else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1495
1496                                                                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1497                                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1498                                                                                         if ($sPlaceIDs)
1499                                                                                         {
1500                                                                                                 $sSQL .= ",placex as f where ";
1501                                                                                                 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1502                                                                                         }
1503                                                                                         if ($sPlaceGeom)
1504                                                                                         {
1505                                                                                                 $sSQL .= " where ";
1506                                                                                                 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1507                                                                                         }
1508                                                                                         if (sizeof($this->aExcludePlaceIDs))
1509                                                                                         {
1510                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1511                                                                                         }
1512                                                                                         if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1513                                                                                         if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1514                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1515                                                                                         $sSQL .= " limit $this->iLimit";
1516                                                                                         if (CONST_Debug) var_dump($sSQL);
1517                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1518                                                                                 }
1519                                                                                 else
1520                                                                                 {
1521                                                                                         if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1522
1523                                                                                         $sOrderBySQL = '';
1524                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1525                                                                                         else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1526
1527                                                                                         $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1528                                                                                         $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1529                                                                                         $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1530                                                                                         if (sizeof($this->aExcludePlaceIDs))
1531                                                                                         {
1532                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1533                                                                                         }
1534                                                                                         if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1535                                                                                         if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1536                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1537                                                                                         $sSQL .= " limit $this->iLimit";
1538                                                                                         if (CONST_Debug) var_dump($sSQL);
1539                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1540                                                                                 }
1541                                                                         }
1542                                                                 }
1543
1544                                                                 $aPlaceIDs = $aClassPlaceIDs;
1545
1546                                                         }
1547
1548                                                 }
1549
1550                                                 if (PEAR::IsError($aPlaceIDs))
1551                                                 {
1552                                                         failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1553                                                 }
1554
1555                                                 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1556
1557                                                 foreach($aPlaceIDs as $iPlaceID)
1558                                                 {
1559                                                         $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1560                                                 }
1561                                                 if ($iQueryLoop > 20) break;
1562                                         }
1563
1564                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1565                                         {
1566                                                 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1567                                                 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1568                                                 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1569                                                 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1570                                                 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1571                                                 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1572                                                 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1573                                                 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1574                                                 $sSQL .= ")";
1575                                                 if (CONST_Debug) var_dump($sSQL);
1576                                                 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1577                                         }
1578
1579                                         //exit;
1580                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1581                                         if ($iGroupLoop > 4) break;
1582                                         if ($iQueryLoop > 30) break;
1583                                 }
1584
1585                                 // Did we find anything?
1586                                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1587                                 {
1588                                         $aSearchResults = $this->getDetails($aResultPlaceIDs);
1589                                 }
1590
1591                         }
1592                         else
1593                         {
1594                                 // Just interpret as a reverse geocode
1595                                 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1596                                 if ($iPlaceID)
1597                                         $aSearchResults = $this->getDetails(array($iPlaceID));
1598                                 else
1599                                         $aSearchResults = array();
1600                         }
1601
1602                         // No results? Done
1603                         if (!sizeof($aSearchResults))
1604                         {
1605                                 if ($this->bFallback)
1606                                 {
1607                                         if ($this->fallbackStructuredQuery())
1608                                         {
1609                                                 return $this->lookup();
1610                                         }
1611                                 }
1612
1613                                 return array();
1614                         }
1615
1616                         $aClassType = getClassTypesWithImportance();
1617                         $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1618                         foreach($aRecheckWords as $i => $sWord)
1619                         {
1620                                 if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1621                         }
1622
1623             if (CONST_Debug) { echo '<i>Recheck words:<\i>'; var_dump($aRecheckWords); }
1624
1625                         foreach($aSearchResults as $iResNum => $aResult)
1626                         {
1627                                 // Default
1628                                 $fDiameter = 0.0001;
1629
1630                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1631                                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1632                                 {
1633                                         $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'];
1634                                 }
1635                                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1636                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1637                                 {
1638                                         $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1639                                 }
1640                                 $fRadius = $fDiameter / 2;
1641
1642                                 if (CONST_Search_AreaPolygons)
1643                                 {
1644                                         // Get the bounding box and outline polygon
1645                                         $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1646                                         $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1647                                         $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
1648                                         $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
1649                                         if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1650                                         if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1651                                         if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1652                                         if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1653                                         $sFrom = " from placex where place_id = ".$aResult['place_id'];
1654                                         if ($this->fPolygonSimplificationThreshold > 0)
1655                                         {
1656                                                 $sSQL .= " from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,".$this->fPolygonSimplificationThreshold.") as geometry".$sFrom.") as plx";
1657                                         }
1658                                         else
1659                                         {
1660                                                 $sSQL .= $sFrom;
1661                                         }
1662
1663                                         $aPointPolygon = $this->oDB->getRow($sSQL);
1664                                         if (PEAR::IsError($aPointPolygon))
1665                                         {
1666                                                 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1667                                         }
1668
1669                                         if ($aPointPolygon['place_id'])
1670                                         {
1671                                                 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1672                                                 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1673                                                 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1674                                                 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1675
1676                                                 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1677                                                 {
1678                                                         $aResult['lat'] = $aPointPolygon['centrelat'];
1679                                                         $aResult['lon'] = $aPointPolygon['centrelon'];
1680                                                 }
1681
1682                                                 if ($this->bIncludePolygonAsPoints)
1683                                                 {
1684                                                         // Translate geometry string to point array
1685                                                         if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1686                                                         {
1687                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1688                                                         }
1689                             /*
1690                                                         elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1691                                                         {
1692                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1693                                                         }
1694                             */
1695                                                         elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1696                                                         {
1697                                                                 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
1698                                                                 $fStepSize = (2*pi())/$iSteps;
1699                                                                 $aPolyPoints = array();
1700                                                                 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1701                                                                 {
1702                                                                         $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1703                                                                 }
1704                                                         }
1705                                                 }
1706
1707                                                 // Output data suitable for display (points and a bounding box)
1708                                                 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1709                                                 {
1710                                                         $aResult['aPolyPoints'] = array();
1711                                                         foreach($aPolyPoints as $aPoint)
1712                                                         {
1713                                                                 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1714                                                         }
1715                                                 }
1716
1717                                                 if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001)
1718                                                 {
1719                                                         $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1720                                                         $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1721                                                 }
1722                                                 if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001)
1723                                                 {
1724                                                         $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1725                                                         $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1726                                                 }
1727                                                 $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1728                                         }
1729                                 }
1730
1731                                 if ($aResult['extra_place'] == 'city')
1732                                 {
1733                                         $aResult['class'] = 'place';
1734                                         $aResult['type'] = 'city';
1735                                         $aResult['rank_search'] = 16;
1736                                 }
1737
1738                                 if (!isset($aResult['aBoundingBox']))
1739                                 {
1740                                         $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1741                                         $fStepSize = (2*pi())/$iSteps;
1742                                         $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1743                                         $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1744                                         $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1745                                         $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1746
1747                                         // Output data suitable for display (points and a bounding box)
1748                                         if ($this->bIncludePolygonAsPoints)
1749                                         {
1750                                                 $aPolyPoints = array();
1751                                                 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1752                                                 {
1753                                                         $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1754                                                 }
1755                                                 $aResult['aPolyPoints'] = array();
1756                                                 foreach($aPolyPoints as $aPoint)
1757                                                 {
1758                                                         $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1759                                                 }
1760                                         }
1761                                         $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1762                                 }
1763
1764                                 // Is there an icon set for this type of result?
1765                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1766                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1767                                 {
1768                                         $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1769                                 }
1770
1771                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1772                                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1773                                 {
1774                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1775                                 }
1776                                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1777                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1778                                 {
1779                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1780                                 }
1781
1782                                 if ($this->bIncludeAddressDetails)
1783                                 {
1784                                         $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1785                                         if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1786                                         {
1787                                                 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1788                                         }
1789                                 }
1790
1791                                 if ($this->bIncludeExtraTags)
1792                                 {
1793                                         if ($aResult['extra'])
1794                                         {
1795                                                 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1796                                         }
1797                                         else
1798                                         {
1799                                                 $aResult['sExtraTags'] = array();
1800                                         }
1801                                 }
1802
1803                                 if ($this->bIncludeNameDetails)
1804                                 {
1805                                         if ($aResult['names'])
1806                                         {
1807                                                 $aResult['sNameDetails'] = json_decode($aResult['names']);
1808                                         }
1809                                         else
1810                                         {
1811                                                 $aResult['sNameDetails'] = array();
1812                                         }
1813                                 }
1814
1815                                 // Adjust importance for the number of exact string matches in the result
1816                                 $aResult['importance'] = max(0.001,$aResult['importance']);
1817                                 $iCountWords = 0;
1818                                 $sAddress = $aResult['langaddress'];
1819                                 foreach($aRecheckWords as $i => $sWord)
1820                                 {
1821                                         if (stripos($sAddress, $sWord)!==false)
1822                                         {
1823                                                 $iCountWords++;
1824                                                 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1825                                         }
1826                                 }
1827
1828                                 $aResult['importance'] = $aResult['importance'] + ($iCountWords*0.1); // 0.1 is a completely arbitrary number but something in the range 0.1 to 0.5 would seem right
1829
1830                                 $aResult['name'] = $aResult['langaddress'];
1831                                 // secondary ordering (for results with same importance (the smaller the better):
1832                                 //   - approximate importance of address parts
1833                                 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1834                                 //   - number of exact matches from the query
1835                                 if (isset($this->exactMatchCache[$aResult['place_id']]))
1836                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1837                                 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1838                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1839                                 //  - importance of the class/type
1840                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1841                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1842                                 {
1843                                         $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1844                                 }
1845                                 else
1846                                 {
1847                                         $aResult['foundorder'] += 0.01;
1848                                 }
1849                                 if (CONST_Debug) { var_dump($aResult); }
1850                                 $aSearchResults[$iResNum] = $aResult;
1851                         }
1852                         uasort($aSearchResults, 'byImportance');
1853
1854                         $aOSMIDDone = array();
1855                         $aClassTypeNameDone = array();
1856                         $aToFilter = $aSearchResults;
1857                         $aSearchResults = array();
1858
1859                         $bFirst = true;
1860                         foreach($aToFilter as $iResNum => $aResult)
1861                         {
1862                                 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1863                                 if ($bFirst)
1864                                 {
1865                                         $fLat = $aResult['lat'];
1866                                         $fLon = $aResult['lon'];
1867                                         if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1868                                         $bFirst = false;
1869                                 }
1870                                 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1871                                                         && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1872                                 {
1873                                         $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1874                                         $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1875                                         $aSearchResults[] = $aResult;
1876                                 }
1877
1878                                 // Absolute limit on number of results
1879                                 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1880                         }
1881
1882                         return $aSearchResults;
1883
1884                 } // end lookup()
1885
1886
1887         } // end class
1888