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