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