]> git.openstreetmap.org Git - nominatim.git/blob - lib/Geocode.php
check if query is valid unicode string
[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 = true;
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         if (!preg_match('//u', $sQuery)) {
777             userError("Query string is not UTF-8 encoded.");
778         }
779
780         // Conflicts between US state abreviations and various words for 'the' in different languages
781         if (isset($this->aLangPrefOrder['name:en'])) {
782             $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/', '\1illinois\2', $sQuery);
783             $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/', '\1alabama\2', $sQuery);
784             $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/', '\1louisiana\2', $sQuery);
785         }
786
787         $bBoundingBoxSearch = $this->bBoundedSearch && $this->sViewboxSmallSQL;
788         if ($this->sViewboxCentreSQL) {
789             // For complex viewboxes (routes) precompute the bounding geometry
790             $sGeom = chksql(
791                 $this->oDB->getOne("select ".$this->sViewboxSmallSQL),
792                 "Could not get small viewbox"
793             );
794             $this->sViewboxSmallSQL = "'".$sGeom."'::geometry";
795
796             $sGeom = chksql(
797                 $this->oDB->getOne("select ".$this->sViewboxLargeSQL),
798                 "Could not get large viewbox"
799             );
800             $this->sViewboxLargeSQL = "'".$sGeom."'::geometry";
801         }
802
803         // Do we have anything that looks like a lat/lon pair?
804         if ($aLooksLike = looksLikeLatLonPair($sQuery)) {
805             $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
806             $sQuery = $aLooksLike['query'];
807         }
808
809         $aSearchResults = array();
810         if ($sQuery || $this->aStructuredQuery) {
811             // Start with a blank search
812             $aSearches = array(
813                           array(
814                            'iSearchRank' => 0,
815                            'iNamePhrase' => -1,
816                            'sCountryCode' => false,
817                            'aName' => array(),
818                            'aAddress' => array(),
819                            'aFullNameAddress' => array(),
820                            'aNameNonSearch' => array(),
821                            'aAddressNonSearch' => array(),
822                            'sOperator' => '',
823                            'aFeatureName' => array(),
824                            'sClass' => '',
825                            'sType' => '',
826                            'sHouseNumber' => '',
827                            'fLat' => '',
828                            'fLon' => '',
829                            'fRadius' => ''
830                           )
831                          );
832
833             // Do we have a radius search?
834             $sNearPointSQL = false;
835             if ($this->aNearPoint) {
836                 $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
837                 $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
838                 $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
839                 $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
840             }
841
842             // Any 'special' terms in the search?
843             $bSpecialTerms = false;
844             preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
845             $aSpecialTerms = array();
846             foreach ($aSpecialTermsRaw as $aSpecialTerm) {
847                 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
848                 $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
849             }
850
851             preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
852             $aSpecialTerms = array();
853             if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity']) {
854                 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
855                 unset($this->aStructuredQuery['amenity']);
856             }
857
858             foreach ($aSpecialTermsRaw as $aSpecialTerm) {
859                 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
860                 $sToken = chksql($this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string"));
861                 $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
862                 $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';
863                 if (CONST_Debug) var_Dump($sSQL);
864                 $aSearchWords = chksql($this->oDB->getAll($sSQL));
865                 $aNewSearches = array();
866                 foreach ($aSearches as $aSearch) {
867                     foreach ($aSearchWords as $aSearchTerm) {
868                         $aNewSearch = $aSearch;
869                         if ($aSearchTerm['country_code']) {
870                             $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
871                             $aNewSearches[] = $aNewSearch;
872                             $bSpecialTerms = true;
873                         }
874                         if ($aSearchTerm['class']) {
875                             $aNewSearch['sClass'] = $aSearchTerm['class'];
876                             $aNewSearch['sType'] = $aSearchTerm['type'];
877                             $aNewSearches[] = $aNewSearch;
878                             $bSpecialTerms = true;
879                         }
880                     }
881                 }
882                 $aSearches = $aNewSearches;
883             }
884
885             // Split query into phrases
886             // Commas are used to reduce the search space by indicating where phrases split
887             if ($this->aStructuredQuery) {
888                 $aPhrases = $this->aStructuredQuery;
889                 $bStructuredPhrases = true;
890             } else {
891                 $aPhrases = explode(',', $sQuery);
892                 $bStructuredPhrases = false;
893             }
894
895             // Convert each phrase to standard form
896             // Create a list of standard words
897             // Get all 'sets' of words
898             // Generate a complete list of all
899             $aTokens = array();
900             foreach ($aPhrases as $iPhrase => $sPhrase) {
901                 $aPhrase = chksql(
902                     $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string"),
903                     "Cannot normalize query string (is it a UTF-8 string?)"
904                 );
905                 if (trim($aPhrase['string'])) {
906                     $aPhrases[$iPhrase] = $aPhrase;
907                     $aPhrases[$iPhrase]['words'] = explode(' ', $aPhrases[$iPhrase]['string']);
908                     $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
909                     $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
910                 } else {
911                     unset($aPhrases[$iPhrase]);
912                 }
913             }
914
915             // Reindex phrases - we make assumptions later on that they are numerically keyed in order
916             $aPhraseTypes = array_keys($aPhrases);
917             $aPhrases = array_values($aPhrases);
918
919             if (sizeof($aTokens)) {
920                 // Check which tokens we have, get the ID numbers
921                 $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
922                 $sSQL .= ' from word where word_token in ('.join(',', array_map("getDBQuoted", $aTokens)).')';
923
924                 if (CONST_Debug) var_Dump($sSQL);
925
926                 $aValidTokens = array();
927                 if (sizeof($aTokens)) {
928                     $aDatabaseWords = chksql(
929                         $this->oDB->getAll($sSQL),
930                         "Could not get word tokens."
931                     );
932                 } else {
933                     $aDatabaseWords = array();
934                 }
935                 $aPossibleMainWordIDs = array();
936                 $aWordFrequencyScores = array();
937                 foreach ($aDatabaseWords as $aToken) {
938                     // Very special case - require 2 letter country param to match the country code found
939                     if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
940                         && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code']
941                     ) {
942                         continue;
943                     }
944
945                     if (isset($aValidTokens[$aToken['word_token']])) {
946                         $aValidTokens[$aToken['word_token']][] = $aToken;
947                     } else {
948                         $aValidTokens[$aToken['word_token']] = array($aToken);
949                     }
950                     if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
951                     $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
952                 }
953                 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
954
955                 // Try and calculate GB postcodes we might be missing
956                 foreach ($aTokens as $sToken) {
957                     // Source of gb postcodes is now definitive - always use
958                     if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData)) {
959                         if (substr($aData[1], -2, 1) != ' ') {
960                             $aData[0] = substr($aData[0], 0, strlen($aData[1])-1).' '.substr($aData[0], strlen($aData[1])-1);
961                             $aData[1] = substr($aData[1], 0, -1).' '.substr($aData[1], -1, 1);
962                         }
963                         $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
964                         if ($aGBPostcodeLocation) {
965                             $aValidTokens[$sToken] = $aGBPostcodeLocation;
966                         }
967                     } elseif (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
968                         // US ZIP+4 codes - if there is no token,
969                         // merge in the 5-digit ZIP code
970                         if (isset($aValidTokens[$aData[1]])) {
971                             foreach ($aValidTokens[$aData[1]] as $aToken) {
972                                 if (!$aToken['class']) {
973                                     if (isset($aValidTokens[$sToken])) {
974                                         $aValidTokens[$sToken][] = $aToken;
975                                     } else {
976                                         $aValidTokens[$sToken] = array($aToken);
977                                     }
978                                 }
979                             }
980                         }
981                     }
982                 }
983
984                 foreach ($aTokens as $sToken) {
985                     // Unknown single word token with a number - assume it is a house number
986                     if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken, ' ') === false && preg_match('/[0-9]/', $sToken)) {
987                         $aValidTokens[' '.$sToken] = array(array('class' => 'place', 'type' => 'house'));
988                     }
989                 }
990
991                 // Any words that have failed completely?
992                 // TODO: suggestions
993
994                 // Start the search process
995                 // array with: placeid => -1 | tiger-housenumber
996                 $aResultPlaceIDs = array();
997
998                 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
999
1000                 if ($this->bReverseInPlan) {
1001                     // Reverse phrase array and also reverse the order of the wordsets in
1002                     // the first and final phrase. Don't bother about phrases in the middle
1003                     // because order in the address doesn't matter.
1004                     $aPhrases = array_reverse($aPhrases);
1005                     $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1006                     if (sizeof($aPhrases) > 1) {
1007                         $aFinalPhrase = end($aPhrases);
1008                         $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1009                     }
1010                     $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1011
1012                     foreach ($aGroupedSearches as $aSearches) {
1013                         foreach ($aSearches as $aSearch) {
1014                             if ($aSearch['iSearchRank'] < $this->iMaxRank) {
1015                                 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1016                                 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1017                             }
1018                         }
1019                     }
1020
1021                     $aGroupedSearches = $aReverseGroupedSearches;
1022                     ksort($aGroupedSearches);
1023                 }
1024             } else {
1025                 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1026                 $aGroupedSearches = array();
1027                 foreach ($aSearches as $aSearch) {
1028                     if ($aSearch['iSearchRank'] < $this->iMaxRank) {
1029                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1030                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1031                     }
1032                 }
1033                 ksort($aGroupedSearches);
1034             }
1035
1036             if (CONST_Debug) var_Dump($aGroupedSearches);
1037             if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0) {
1038                 $aCopyGroupedSearches = $aGroupedSearches;
1039                 foreach ($aCopyGroupedSearches as $iGroup => $aSearches) {
1040                     foreach ($aSearches as $iSearch => $aSearch) {
1041                         $aReductionsList = array($aSearch['aAddress']);
1042                         $iSearchRank = $aSearch['iSearchRank'];
1043                         while (sizeof($aReductionsList) > 0) {
1044                             $iSearchRank += 5;
1045                             if ($iSearchRank > iMaxRank) break 3;
1046                             $aNewReductionsList = array();
1047                             foreach ($aReductionsList as $aReductionsWordList) {
1048                                 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++) {
1049                                     $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1050                                     $aReverseSearch = $aSearch;
1051                                     $aSearch['aAddress'] = $aReductionsWordListResult;
1052                                     $aSearch['iSearchRank'] = $iSearchRank;
1053                                     $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1054                                     if (sizeof($aReductionsWordListResult) > 0) {
1055                                         $aNewReductionsList[] = $aReductionsWordListResult;
1056                                     }
1057                                 }
1058                             }
1059                             $aReductionsList = $aNewReductionsList;
1060                         }
1061                     }
1062                 }
1063                 ksort($aGroupedSearches);
1064             }
1065
1066             // Filter out duplicate searches
1067             $aSearchHash = array();
1068             foreach ($aGroupedSearches as $iGroup => $aSearches) {
1069                 foreach ($aSearches as $iSearch => $aSearch) {
1070                     $sHash = serialize($aSearch);
1071                     if (isset($aSearchHash[$sHash])) {
1072                         unset($aGroupedSearches[$iGroup][$iSearch]);
1073                         if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1074                     } else {
1075                         $aSearchHash[$sHash] = 1;
1076                     }
1077                 }
1078             }
1079
1080             if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1081
1082             $iGroupLoop = 0;
1083             $iQueryLoop = 0;
1084             foreach ($aGroupedSearches as $iGroupedRank => $aSearches) {
1085                 $iGroupLoop++;
1086                 foreach ($aSearches as $aSearch) {
1087                     $iQueryLoop++;
1088                     $searchedHousenumber = -1;
1089
1090                     if (CONST_Debug) echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>";
1091                     if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1092
1093                     // No location term?
1094                     if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon']) {
1095                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber']) {
1096                             // Just looking for a country by code - look it up
1097                             if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank) {
1098                                 $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1099                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1100                                 if ($bBoundingBoxSearch)
1101                                     $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1102                                 $sSQL .= " order by st_area(geometry) desc limit 1";
1103                                 if (CONST_Debug) var_dump($sSQL);
1104                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1105                             } else {
1106                                 $aPlaceIDs = array();
1107                             }
1108                         } else {
1109                             if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1110                             if (!$aSearch['sClass']) continue;
1111
1112                             $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1113                             if (chksql($this->oDB->getOne($sSQL))) {
1114                                 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1115                                 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1116                                 $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1117                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1118                                 if (sizeof($this->aExcludePlaceIDs)) {
1119                                     $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1120                                 }
1121                                 if ($this->sViewboxCentreSQL) $sSQL .= " order by st_distance($this->sViewboxCentreSQL, ct.centroid) asc";
1122                                 $sSQL .= " limit $this->iLimit";
1123                                 if (CONST_Debug) var_dump($sSQL);
1124                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1125
1126                                 // If excluded place IDs are given, it is fair to assume that
1127                                 // there have been results in the small box, so no further
1128                                 // expansion in that case.
1129                                 // Also don't expand if bounded results were requested.
1130                                 if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch) {
1131                                     $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1132                                     if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1133                                     $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1134                                     if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1135                                     if ($this->sViewboxCentreSQL) $sSQL .= " order by st_distance($this->sViewboxCentreSQL, ct.centroid) asc";
1136                                     $sSQL .= " limit $this->iLimit";
1137                                     if (CONST_Debug) var_dump($sSQL);
1138                                     $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1139                                 }
1140                             } else {
1141                                 $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1142                                 $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1143                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1144                                 if ($this->sViewboxCentreSQL)   $sSQL .= " order by st_distance($this->sViewboxCentreSQL, centroid) asc";
1145                                 $sSQL .= " limit $this->iLimit";
1146                                 if (CONST_Debug) var_dump($sSQL);
1147                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1148                             }
1149                         }
1150                     } elseif ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
1151                         // If a coordinate is given, the search must either
1152                         // be for a name or a special search. Ignore everythin else.
1153                         $aPlaceIDs = array();
1154                     } else {
1155                         $aPlaceIDs = array();
1156
1157                         // First we need a position, either aName or fLat or both
1158                         $aTerms = array();
1159                         $aOrder = array();
1160
1161                         if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress'])) {
1162                             $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1163                             $aOrder[] = "";
1164                             $aOrder[0] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
1165                             $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
1166                             // also housenumbers from interpolation lines table are needed
1167                             $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
1168                             $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
1169                             $aOrder[0] .= " desc";
1170                         }
1171
1172                         // TODO: filter out the pointless search terms (2 letter name tokens and less)
1173                         // they might be right - but they are just too darned expensive to run
1174                         if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'], ",")."]";
1175                         //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'], ",")."]";
1176                         if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) {
1177                             // For infrequent name terms disable index usage for address
1178                             if (CONST_Search_NameOnlySearchFrequencyThreshold
1179                                 && sizeof($aSearch['aName']) == 1
1180                                 && $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold
1181                             ) {
1182                                 //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'], $aSearch['aAddressNonSearch']), ",")."]";
1183                                 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1184                             } else {
1185                                 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'], ",")."]";
1186                                 /*if (sizeof($aSearch['aAddressNonSearch'])) {
1187                                     $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'], ",")."]";
1188                                 }*/
1189                             }
1190                         }
1191                         if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1192                         if ($aSearch['sHouseNumber']) {
1193                             $aTerms[] = "address_rank between 16 and 27";
1194                         } else {
1195                             if ($this->iMinAddressRank > 0) {
1196                                 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1197                             }
1198                             if ($this->iMaxAddressRank < 30) {
1199                                 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1200                             }
1201                         }
1202                         if ($aSearch['fLon'] && $aSearch['fLat']) {
1203                             $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1204                             $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1205                         }
1206                         if (sizeof($this->aExcludePlaceIDs)) {
1207                             $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1208                         }
1209                         if ($sCountryCodesSQL) {
1210                             $aTerms[] = "country_code in ($sCountryCodesSQL)";
1211                         }
1212
1213                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1214                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1215
1216                         if ($aSearch['sHouseNumber']) {
1217                             $sImportanceSQL = '- abs(26 - address_rank) + 3';
1218                         } else {
1219                             $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1220                         }
1221                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1222                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1223
1224                         $aOrder[] = "$sImportanceSQL DESC";
1225                         if (sizeof($aSearch['aFullNameAddress'])) {
1226                             $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'], ",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1227                             $aOrder[] = 'exactmatch DESC';
1228                         } else {
1229                             $sExactMatchSQL = '0::int as exactmatch';
1230                         }
1231
1232                         if (sizeof($aTerms)) {
1233                             $sSQL = "select place_id, ";
1234                             $sSQL .= $sExactMatchSQL;
1235                             $sSQL .= " from search_name";
1236                             $sSQL .= " where ".join(' and ', $aTerms);
1237                             $sSQL .= " order by ".join(', ', $aOrder);
1238                             if ($aSearch['sHouseNumber'] || $aSearch['sClass']) {
1239                                 $sSQL .= " limit 20";
1240                             } elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass']) {
1241                                 $sSQL .= " limit 1";
1242                             } else {
1243                                 $sSQL .= " limit ".$this->iLimit;
1244                             }
1245
1246                             if (CONST_Debug) var_dump($sSQL);
1247                             $aViewBoxPlaceIDs = chksql(
1248                                 $this->oDB->getAll($sSQL),
1249                                 "Could not get places for search terms."
1250                             );
1251                             //var_dump($aViewBoxPlaceIDs);
1252                             // Did we have an viewbox matches?
1253                             $aPlaceIDs = array();
1254                             $bViewBoxMatch = false;
1255                             foreach ($aViewBoxPlaceIDs as $aViewBoxRow) {
1256                                 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1257                                 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1258                                 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1259                                 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1260                                 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1261                                 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1262                             }
1263                         }
1264                         //var_Dump($aPlaceIDs);
1265                         //exit;
1266
1267                         //now search for housenumber, if housenumber provided
1268                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs)) {
1269                             $searchedHousenumber = intval($aSearch['sHouseNumber']);
1270                             $aRoadPlaceIDs = $aPlaceIDs;
1271                             $sPlaceIDs = join(',', $aPlaceIDs);
1272
1273                             // Now they are indexed, look for a house attached to a street we found
1274                             $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1275                             $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1276                             if (sizeof($this->aExcludePlaceIDs)) {
1277                                 $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1278                             }
1279                             $sSQL .= " limit $this->iLimit";
1280                             if (CONST_Debug) var_dump($sSQL);
1281                             $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1282                             
1283                             // if nothing found, search in the interpolation line table
1284                             if (!sizeof($aPlaceIDs)) {
1285                                 // do we need to use transliteration and the regex for housenumbers???
1286                                 //new query for lines, not housenumbers anymore
1287                                 if ($searchedHousenumber%2 == 0) {
1288                                     //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1289                                     $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";
1290                                 } else {
1291                                     //look for housenumber in streets with interpolationtype odd or all
1292                                     $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";
1293                                 }
1294
1295                                 if (sizeof($this->aExcludePlaceIDs)) {
1296                                     $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1297                                 }
1298                                 //$sSQL .= " limit $this->iLimit";
1299                                 if (CONST_Debug) var_dump($sSQL);
1300                                 //get place IDs
1301                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1302                             }
1303                                 
1304                             // If nothing found try the aux fallback table
1305                             if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs)) {
1306                                 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1307                                 if (sizeof($this->aExcludePlaceIDs)) {
1308                                     $sSQL .= " and parent_place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1309                                 }
1310                                 //$sSQL .= " limit $this->iLimit";
1311                                 if (CONST_Debug) var_dump($sSQL);
1312                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1313                             }
1314
1315                             //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1316                             if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs)) {
1317                                 //new query for lines, not housenumbers anymore
1318                                 if ($searchedHousenumber%2 == 0) {
1319                                     //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1320                                     $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";
1321                                 } else {
1322                                     //look for housenumber in streets with interpolationtype odd or all
1323                                     $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";
1324                                 }
1325
1326                                 if (sizeof($this->aExcludePlaceIDs)) {
1327                                     $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1328                                 }
1329                                 //$sSQL .= " limit $this->iLimit";
1330                                 if (CONST_Debug) var_dump($sSQL);
1331                                 //get place IDs
1332                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1333                             }
1334
1335                             // Fallback to the road (if no housenumber was found)
1336                             if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber'])) {
1337                                 $aPlaceIDs = $aRoadPlaceIDs;
1338                                 //set to -1, if no housenumbers were found
1339                                 $searchedHousenumber = -1;
1340                             }
1341                             //else: housenumber was found, remains saved in searchedHousenumber
1342                         }
1343
1344
1345                         if ($aSearch['sClass'] && sizeof($aPlaceIDs)) {
1346                             $sPlaceIDs = join(',', $aPlaceIDs);
1347                             $aClassPlaceIDs = array();
1348
1349                             if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name') {
1350                                 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1351                                 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1352                                 $sSQL .= " and linked_place_id is null";
1353                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1354                                 $sSQL .= " order by rank_search asc limit $this->iLimit";
1355                                 if (CONST_Debug) var_dump($sSQL);
1356                                 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
1357                             }
1358
1359                             if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') { // & in
1360                                 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1361                                 $bCacheTable = chksql($this->oDB->getOne($sSQL));
1362
1363                                 $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1364
1365                                 if (CONST_Debug) var_dump($sSQL);
1366                                 $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
1367
1368                                 // For state / country level searches the normal radius search doesn't work very well
1369                                 $sPlaceGeom = false;
1370                                 if ($this->iMaxRank < 9 && $bCacheTable) {
1371                                     // Try and get a polygon to search in instead
1372                                     $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";
1373                                     if (CONST_Debug) var_dump($sSQL);
1374                                     $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
1375                                 }
1376
1377                                 if ($sPlaceGeom) {
1378                                     $sPlaceIDs = false;
1379                                 } else {
1380                                     $this->iMaxRank += 5;
1381                                     $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1382                                     if (CONST_Debug) var_dump($sSQL);
1383                                     $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1384                                     $sPlaceIDs = join(',', $aPlaceIDs);
1385                                 }
1386
1387                                 if ($sPlaceIDs || $sPlaceGeom) {
1388                                     $fRange = 0.01;
1389                                     if ($bCacheTable) {
1390                                         // More efficient - can make the range bigger
1391                                         $fRange = 0.05;
1392
1393                                         $sOrderBySQL = '';
1394                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1395                                         elseif ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1396                                         elseif ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1397
1398                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1399                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1400                                         if ($sPlaceIDs) {
1401                                             $sSQL .= ",placex as f where ";
1402                                             $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1403                                         }
1404                                         if ($sPlaceGeom) {
1405                                             $sSQL .= " where ";
1406                                             $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1407                                         }
1408                                         if (sizeof($this->aExcludePlaceIDs)) {
1409                                             $sSQL .= " and l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1410                                         }
1411                                         if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1412                                         if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1413                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1414                                         $sSQL .= " limit $this->iLimit";
1415                                         if (CONST_Debug) var_dump($sSQL);
1416                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1417                                     } else {
1418                                         if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1419
1420                                         $sOrderBySQL = '';
1421                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1422                                         else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1423
1424                                         $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1425                                         $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1426                                         $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1427                                         if (sizeof($this->aExcludePlaceIDs)) {
1428                                             $sSQL .= " and l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1429                                         }
1430                                         if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1431                                         if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1432                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1433                                         $sSQL .= " limit $this->iLimit";
1434                                         if (CONST_Debug) var_dump($sSQL);
1435                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1436                                     }
1437                                 }
1438                             }
1439                             $aPlaceIDs = $aClassPlaceIDs;
1440                         }
1441                     }
1442
1443                     if (CONST_Debug) {
1444                         echo "<br><b>Place IDs:</b> ";
1445                         var_Dump($aPlaceIDs);
1446                     }
1447
1448                     foreach ($aPlaceIDs as $iPlaceID) {
1449                         // array for placeID => -1 | Tiger housenumber
1450                         $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1451                     }
1452                     if ($iQueryLoop > 20) break;
1453                 }
1454
1455                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
1456                     // Need to verify passes rank limits before dropping out of the loop (yuk!)
1457                     // reduces the number of place ids, like a filter
1458                     // rank_address is 30 for interpolated housenumbers
1459                     $sSQL = "select place_id from placex where place_id in (".join(',', array_keys($aResultPlaceIDs)).") ";
1460                     $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1461                     if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1462                     if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
1463                     if (CONST_Use_US_Tiger_Data) {
1464                         $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',', array_keys($aResultPlaceIDs)).") ";
1465                         $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1466                         if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',', $this->aAddressRankList).")";
1467                     }
1468                     $sSQL .= ") UNION select place_id from location_property_osmline where place_id in (".join(',', array_keys($aResultPlaceIDs)).")";
1469                     $sSQL .= " and (30 between $this->iMinAddressRank and $this->iMaxAddressRank)";
1470                     if (CONST_Debug) var_dump($sSQL);
1471                     $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1472                     $tempIDs = array();
1473                     foreach ($aFilteredPlaceIDs as $placeID) {
1474                         $tempIDs[$placeID] = $aResultPlaceIDs[$placeID];  //assign housenumber to placeID
1475                     }
1476                     $aResultPlaceIDs = $tempIDs;
1477                 }
1478
1479                 //exit;
1480                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1481                 if ($iGroupLoop > 4) break;
1482                 if ($iQueryLoop > 30) break;
1483             }
1484
1485             // Did we find anything?
1486             if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) {
1487                 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1488             }
1489         } else {
1490             // Just interpret as a reverse geocode
1491             $oReverse = new ReverseGeocode($this->oDB);
1492             $oReverse->setZoom(18);
1493
1494             $aLookup = $oReverse->lookup(
1495                 (float)$this->aNearPoint[0],
1496                 (float)$this->aNearPoint[1],
1497                 false
1498             );
1499
1500             if (CONST_Debug) var_dump("Reverse search", $aLookup);
1501
1502             if ($aLookup['place_id']) {
1503                 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
1504                 $aResultPlaceIDs[$aLookup['place_id']] = -1;
1505             } else {
1506                 $aSearchResults = array();
1507             }
1508         }
1509
1510         // No results? Done
1511         if (!sizeof($aSearchResults)) {
1512             if ($this->bFallback) {
1513                 if ($this->fallbackStructuredQuery()) {
1514                     return $this->lookup();
1515                 }
1516             }
1517
1518             return array();
1519         }
1520
1521         $aClassType = getClassTypesWithImportance();
1522         $aRecheckWords = preg_split('/\b[\s,\\-]*/u', $sQuery);
1523         foreach ($aRecheckWords as $i => $sWord) {
1524             if (!preg_match('/\pL/', $sWord)) unset($aRecheckWords[$i]);
1525         }
1526
1527         if (CONST_Debug) {
1528             echo '<i>Recheck words:<\i>';
1529             var_dump($aRecheckWords);
1530         }
1531
1532         $oPlaceLookup = new PlaceLookup($this->oDB);
1533         $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints);
1534         $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText);
1535         $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON);
1536         $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML);
1537         $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG);
1538         $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold);
1539
1540         foreach ($aSearchResults as $iResNum => $aResult) {
1541             // Default
1542             $fDiameter = getResultDiameter($aResult);
1543
1544             $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2);
1545             if ($aOutlineResult) {
1546                 $aResult = array_merge($aResult, $aOutlineResult);
1547             }
1548             
1549             if ($aResult['extra_place'] == 'city') {
1550                 $aResult['class'] = 'place';
1551                 $aResult['type'] = 'city';
1552                 $aResult['rank_search'] = 16;
1553             }
1554
1555             // Is there an icon set for this type of result?
1556             if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1557                 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon']
1558             ) {
1559                 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1560             }
1561
1562             if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1563                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label']
1564             ) {
1565                 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1566             } elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1567                 && $aClassType[$aResult['class'].':'.$aResult['type']]['label']
1568             ) {
1569                 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1570             }
1571             // if tag '&addressdetails=1' is set in query
1572             if ($this->bIncludeAddressDetails) {
1573                 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1574                 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1575                 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city'])) {
1576                     $aResult['address'] = array_merge(array('city' => array_values($aResult['address'])[0]), $aResult['address']);
1577                 }
1578             }
1579
1580             if ($this->bIncludeExtraTags) {
1581                 if ($aResult['extra']) {
1582                     $aResult['sExtraTags'] = json_decode($aResult['extra']);
1583                 } else {
1584                     $aResult['sExtraTags'] = (object) array();
1585                 }
1586             }
1587
1588             if ($this->bIncludeNameDetails) {
1589                 if ($aResult['names']) {
1590                     $aResult['sNameDetails'] = json_decode($aResult['names']);
1591                 } else {
1592                     $aResult['sNameDetails'] = (object) array();
1593                 }
1594             }
1595
1596             // Adjust importance for the number of exact string matches in the result
1597             $aResult['importance'] = max(0.001, $aResult['importance']);
1598             $iCountWords = 0;
1599             $sAddress = $aResult['langaddress'];
1600             foreach ($aRecheckWords as $i => $sWord) {
1601                 if (stripos($sAddress, $sWord)!==false) {
1602                     $iCountWords++;
1603                     if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1604                 }
1605             }
1606
1607             $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
1608
1609             $aResult['name'] = $aResult['langaddress'];
1610             // secondary ordering (for results with same importance (the smaller the better):
1611             // - approximate importance of address parts
1612             $aResult['foundorder'] = -$aResult['addressimportance']/10;
1613             // - number of exact matches from the query
1614             if (isset($this->exactMatchCache[$aResult['place_id']])) {
1615                 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1616             } elseif (isset($this->exactMatchCache[$aResult['parent_place_id']])) {
1617                 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1618             }
1619             // - importance of the class/type
1620             if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1621                 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance']
1622             ) {
1623                 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1624             } else {
1625                 $aResult['foundorder'] += 0.01;
1626             }
1627             if (CONST_Debug) var_dump($aResult);
1628             $aSearchResults[$iResNum] = $aResult;
1629         }
1630         uasort($aSearchResults, 'byImportance');
1631
1632         $aOSMIDDone = array();
1633         $aClassTypeNameDone = array();
1634         $aToFilter = $aSearchResults;
1635         $aSearchResults = array();
1636
1637         $bFirst = true;
1638         foreach ($aToFilter as $iResNum => $aResult) {
1639             $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1640             if ($bFirst) {
1641                 $fLat = $aResult['lat'];
1642                 $fLon = $aResult['lon'];
1643                 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1644                 $bFirst = false;
1645             }
1646             if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1647                 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']]))
1648             ) {
1649                 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1650                 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1651                 $aSearchResults[] = $aResult;
1652             }
1653
1654             // Absolute limit on number of results
1655             if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1656         }
1657
1658         return $aSearchResults;
1659     } // end lookup()
1660 } // end class