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