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