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