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