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