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