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