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