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