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