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