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