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