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