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