]> 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                                                                         $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1097                                                                 }
1098                                                                 else
1099                                                                 {
1100                                                                         $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1101                                                                         //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1102                                                                 }
1103                                                         }
1104                                                         if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1105                                                         if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank between 16 and 27";
1106                                                         if ($aSearch['fLon'] && $aSearch['fLat'])
1107                                                         {
1108                                                                 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1109                                                                 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1110                                                         }
1111                                                         if (sizeof($this->aExcludePlaceIDs))
1112                                                         {
1113                                                                 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1114                                                         }
1115                                                         if ($sCountryCodesSQL)
1116                                                         {
1117                                                                 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1118                                                         }
1119
1120                                                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1121                                                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1122
1123                                                         $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1124                                                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1125                                                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1126                                                         $aOrder[] = "$sImportanceSQL DESC";
1127                                                         if (sizeof($aSearch['aFullNameAddress']))
1128                                                         {
1129                                                                 $aOrder[] = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) DESC';
1130                                                         }
1131
1132                                                         if (sizeof($aTerms))
1133                                                         {
1134                                                                 $sSQL = "select place_id";
1135                                                                 $sSQL .= " from search_name";
1136                                                                 $sSQL .= " where ".join(' and ',$aTerms);
1137                                                                 $sSQL .= " order by ".join(', ',$aOrder);
1138                                                                 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1139                                                                         $sSQL .= " limit 50";
1140                                                                 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1141                                                                         $sSQL .= " limit 1";
1142                                                                 else
1143                                                                         $sSQL .= " limit ".$this->iLimit;
1144
1145                                                                 if (CONST_Debug) { var_dump($sSQL); }
1146                                                                 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1147                                                                 if (PEAR::IsError($aViewBoxPlaceIDs))
1148                                                                 {
1149                                                                         failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1150                                                                 }
1151                                                                 //var_dump($aViewBoxPlaceIDs);
1152                                                                 // Did we have an viewbox matches?
1153                                                                 $aPlaceIDs = array();
1154                                                                 $bViewBoxMatch = false;
1155                                                                 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1156                                                                 {
1157                                                                         //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1158                                                                         //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1159                                                                         //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1160                                                                         //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1161                                                                         $aPlaceIDs[] = $aViewBoxRow['place_id'];
1162                                                                 }
1163                                                         }
1164                                                         //var_Dump($aPlaceIDs);
1165                                                         //exit;
1166
1167                                                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1168                                                         {
1169                                                                 $aRoadPlaceIDs = $aPlaceIDs;
1170                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1171
1172                                                                 // Now they are indexed look for a house attached to a street we found
1173                                                                 $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-,/ ]',$aSearch['sHouseNumber']).'\\\\M';
1174                                                                 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
1175                                                                 if (sizeof($this->aExcludePlaceIDs))
1176                                                                 {
1177                                                                         $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1178                                                                 }
1179                                                                 $sSQL .= " limit $this->iLimit";
1180                                                                 if (CONST_Debug) var_dump($sSQL);
1181                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1182
1183                                                                 // If not try the aux fallback table
1184                                                                 /*
1185                                                                 if (!sizeof($aPlaceIDs))
1186                                                                 {
1187                                                                         $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1188                                                                         if (sizeof($this->aExcludePlaceIDs))
1189                                                                         {
1190                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1191                                                                         }
1192                                                                         //$sSQL .= " limit $this->iLimit";
1193                                                                         if (CONST_Debug) var_dump($sSQL);
1194                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1195                                                                 }
1196                                                                 */
1197
1198                                                                 if (!sizeof($aPlaceIDs))
1199                                                                 {
1200                                                                         $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1201                                                                         if (sizeof($this->aExcludePlaceIDs))
1202                                                                         {
1203                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1204                                                                         }
1205                                                                         //$sSQL .= " limit $this->iLimit";
1206                                                                         if (CONST_Debug) var_dump($sSQL);
1207                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1208                                                                 }
1209
1210                                                                 // Fallback to the road
1211                                                                 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1212                                                                 {
1213                                                                         $aPlaceIDs = $aRoadPlaceIDs;
1214                                                                 }
1215
1216                                                         }
1217
1218                                                         if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1219                                                         {
1220                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1221                                                                 $aClassPlaceIDs = array();
1222
1223                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1224                                                                 {
1225                                                                         // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1226                                                                         $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1227                                                                         $sSQL .= " and linked_place_id is null";
1228                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1229                                                                         $sSQL .= " order by rank_search asc limit $this->iLimit";
1230                                                                         if (CONST_Debug) var_dump($sSQL);
1231                                                                         $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1232                                                                 }
1233
1234                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1235                                                                 {
1236                                                                         $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1237                                                                         $bCacheTable = $this->oDB->getOne($sSQL);
1238
1239                                                                         $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1240
1241                                                                         if (CONST_Debug) var_dump($sSQL);
1242                                                                         $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1243
1244                                                                         // For state / country level searches the normal radius search doesn't work very well
1245                                                                         $sPlaceGeom = false;
1246                                                                         if ($this->iMaxRank < 9 && $bCacheTable)
1247                                                                         {
1248                                                                                 // Try and get a polygon to search in instead
1249                                                                                 $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";
1250                                                                                 if (CONST_Debug) var_dump($sSQL);
1251                                                                                 $sPlaceGeom = $this->oDB->getOne($sSQL);
1252                                                                         }
1253
1254                                                                         if ($sPlaceGeom)
1255                                                                         {
1256                                                                                 $sPlaceIDs = false;
1257                                                                         }
1258                                                                         else
1259                                                                         {
1260                                                                                 $this->iMaxRank += 5;
1261                                                                                 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1262                                                                                 if (CONST_Debug) var_dump($sSQL);
1263                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1264                                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1265                                                                         }
1266
1267                                                                         if ($sPlaceIDs || $sPlaceGeom)
1268                                                                         {
1269
1270                                                                                 $fRange = 0.01;
1271                                                                                 if ($bCacheTable)
1272                                                                                 {
1273                                                                                         // More efficient - can make the range bigger
1274                                                                                         $fRange = 0.05;
1275
1276                                                                                         $sOrderBySQL = '';
1277                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1278                                                                                         else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1279                                                                                         else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1280
1281                                                                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1282                                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1283                                                                                         if ($sPlaceIDs)
1284                                                                                         {
1285                                                                                                 $sSQL .= ",placex as f where ";
1286                                                                                                 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1287                                                                                         }
1288                                                                                         if ($sPlaceGeom)
1289                                                                                         {
1290                                                                                                 $sSQL .= " where ";
1291                                                                                                 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1292                                                                                         }
1293                                                                                         if (sizeof($this->aExcludePlaceIDs))
1294                                                                                         {
1295                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1296                                                                                         }
1297                                                                                         if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1298                                                                                         if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1299                                                                                         if ($iOffset) $sSQL .= " offset $iOffset";
1300                                                                                         $sSQL .= " limit $this->iLimit";
1301                                                                                         if (CONST_Debug) var_dump($sSQL);
1302                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1303                                                                                 }
1304                                                                                 else
1305                                                                                 {
1306                                                                                         if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1307
1308                                                                                         $sOrderBySQL = '';
1309                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1310                                                                                         else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1311
1312                                                                                         $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1313                                                                                         $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1314                                                                                         $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1315                                                                                         if (sizeof($this->aExcludePlaceIDs))
1316                                                                                         {
1317                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1318                                                                                         }
1319                                                                                         if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1320                                                                                         if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1321                                                                                         if ($iOffset) $sSQL .= " offset $iOffset";
1322                                                                                         $sSQL .= " limit $this->iLimit";
1323                                                                                         if (CONST_Debug) var_dump($sSQL);
1324                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1325                                                                                 }
1326                                                                         }
1327                                                                 }
1328
1329                                                                 $aPlaceIDs = $aClassPlaceIDs;
1330
1331                                                         }
1332
1333                                                 }
1334
1335                                                 if (PEAR::IsError($aPlaceIDs))
1336                                                 {
1337                                                         failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1338                                                 }
1339
1340                                                 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1341
1342                                                 foreach($aPlaceIDs as $iPlaceID)
1343                                                 {
1344                                                         $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1345                                                 }
1346                                                 if ($iQueryLoop > 20) break;
1347                                         }
1348
1349                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1350                                         {
1351                                                 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1352                                                 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1353                                                 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1354                                                 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1355                                                 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1356                                                 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1357                                                 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1358                                                 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1359                                                 $sSQL .= ")";
1360                                                 if (CONST_Debug) var_dump($sSQL);
1361                                                 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1362                                         }
1363
1364                                         //exit;
1365                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1366                                         if ($iGroupLoop > 4) break;
1367                                         if ($iQueryLoop > 30) break;
1368                                 }
1369
1370                                 // Did we find anything?
1371                                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1372                                 {
1373                                         $aSearchResults = $this->getDetails($aResultPlaceIDs);
1374                                 }
1375
1376                         }
1377                         else
1378                         {
1379                                 // Just interpret as a reverse geocode
1380                                 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1381                                 if ($iPlaceID)
1382                                         $aSearchResults = $this->getDetails(array($iPlaceID));
1383                                 else
1384                                         $aSearchResults = array();
1385                         }
1386
1387                         // No results? Done
1388                         if (!sizeof($aSearchResults))
1389                         {
1390                                 return array();
1391                         }
1392
1393                         $aClassType = getClassTypesWithImportance();
1394                         $aRecheckWords = preg_split('/\b/u',$sQuery);
1395                         foreach($aRecheckWords as $i => $sWord)
1396                         {
1397                                 if (!$sWord) unset($aRecheckWords[$i]);
1398                         }
1399
1400                         foreach($aSearchResults as $iResNum => $aResult)
1401                         {
1402                                 if (CONST_Search_AreaPolygons)
1403                                 {
1404                                         // Get the bounding box and outline polygon
1405                                         $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1406                                         $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1407                                         $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1408                                         $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1409                                         if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1410                                         if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1411                                         if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1412                                         if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1413                                         $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1414                                         $aPointPolygon = $this->oDB->getRow($sSQL);
1415                                         if (PEAR::IsError($aPointPolygon))
1416                                         {
1417                                                 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1418                                         }
1419
1420                                         if ($aPointPolygon['place_id'])
1421                                         {
1422                                                 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1423                                                 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1424                                                 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1425                                                 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1426
1427                                                 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1428                                                 {
1429                                                         $aResult['lat'] = $aPointPolygon['centrelat'];
1430                                                         $aResult['lon'] = $aPointPolygon['centrelon'];
1431                                                 }
1432
1433                                                 if ($this->bIncludePolygonAsPoints)
1434                                                 {
1435                                                         // Translate geometary string to point array
1436                                                         if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1437                                                         {
1438                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1439                                                         }
1440                             /*
1441                                                         elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1442                                                         {
1443                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1444                                                         }
1445                             */
1446                                                         elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1447                                                         {
1448                                                                 $fRadius = 0.01;
1449                                                                 $iSteps = ($fRadius * 40000)^2;
1450                                                                 $fStepSize = (2*pi())/$iSteps;
1451                                                                 $aPolyPoints = array();
1452                                                                 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1453                                                                 {
1454                                                                         $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1455                                                                 }
1456                                                                 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1457                                                                 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1458                                                                 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1459                                                                 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1460                                                         }
1461                                                 }
1462
1463                                                 // Output data suitable for display (points and a bounding box)
1464                                                 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1465                                                 {
1466                                                         $aResult['aPolyPoints'] = array();
1467                                                         foreach($aPolyPoints as $aPoint)
1468                                                         {
1469                                                                 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1470                                                         }
1471                                                 }
1472                                                 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1473                                         }
1474                                 }
1475
1476                                 if ($aResult['extra_place'] == 'city')
1477                                 {
1478                                         $aResult['class'] = 'place';
1479                                         $aResult['type'] = 'city';
1480                                         $aResult['rank_search'] = 16;
1481                                 }
1482
1483                                 if (!isset($aResult['aBoundingBox']))
1484                                 {
1485                                         // Default
1486                                         $fDiameter = 0.0001;
1487
1488                                         if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1489                                                         && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1490                                         {
1491                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1492                                         }
1493                                         elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1494                                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1495                                         {
1496                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1497                                         }
1498                                         $fRadius = $fDiameter / 2;
1499
1500                                         $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1501                                         $fStepSize = (2*pi())/$iSteps;
1502                                         $aPolyPoints = array();
1503                                         for($f = 0; $f < 2*pi(); $f += $fStepSize)
1504                                         {
1505                                                 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1506                                         }
1507                                         $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1508                                         $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1509                                         $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1510                                         $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1511
1512                                         // Output data suitable for display (points and a bounding box)
1513                                         if ($this->bIncludePolygonAsPoints)
1514                                         {
1515                                                 $aResult['aPolyPoints'] = array();
1516                                                 foreach($aPolyPoints as $aPoint)
1517                                                 {
1518                                                         $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1519                                                 }
1520                                         }
1521                                         $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1522                                 }
1523
1524                                 // Is there an icon set for this type of result?
1525                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1526                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1527                                 {
1528                                         $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1529                                 }
1530
1531                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1532                                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1533                                 {
1534                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1535                                 }
1536                                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1537                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1538                                 {
1539                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1540                                 }
1541
1542                                 if ($this->bIncludeAddressDetails)
1543                                 {
1544                                         $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1545                                         if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1546                                         {
1547                                                 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1548                                         }
1549                                 }
1550
1551                                 // Adjust importance for the number of exact string matches in the result
1552                                 $aResult['importance'] = max(0.001,$aResult['importance']);
1553                                 $iCountWords = 0;
1554                                 $sAddress = $aResult['langaddress'];
1555                                 foreach($aRecheckWords as $i => $sWord)
1556                                 {
1557                                         if (stripos($sAddress, $sWord)!==false) $iCountWords++;
1558                                 }
1559
1560                                 $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
1561
1562                                 $aResult['name'] = $aResult['langaddress'];
1563                                 $aResult['foundorder'] = -$aResult['addressimportance'];
1564                                 $aSearchResults[$iResNum] = $aResult;
1565                         }
1566                         uasort($aSearchResults, 'byImportance');
1567
1568                         $aOSMIDDone = array();
1569                         $aClassTypeNameDone = array();
1570                         $aToFilter = $aSearchResults;
1571                         $aSearchResults = array();
1572
1573                         $bFirst = true;
1574                         foreach($aToFilter as $iResNum => $aResult)
1575                         {
1576                                 if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
1577                                 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1578                                 if ($bFirst)
1579                                 {
1580                                         $fLat = $aResult['lat'];
1581                                         $fLon = $aResult['lon'];
1582                                         if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1583                                         $bFirst = false;
1584                                 }
1585                                 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1586                                                         && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1587                                 {
1588                                         $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1589                                         $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1590                                         $aSearchResults[] = $aResult;
1591                                 }
1592
1593                                 // Absolute limit on number of results
1594                                 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1595                         }
1596
1597                         return $aSearchResults;
1598
1599                 } // end lookup()
1600
1601
1602         } // end class
1603
1604
1605 /*
1606                 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
1607                 {
1608                         $aPoints = explode(',',$_GET['route']);
1609                         if (sizeof($aPoints) % 2 != 0)
1610                         {
1611                                 userError("Uneven number of points");
1612                                 exit;
1613                         }
1614                         $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
1615                         $fPrevCoord = false;
1616                 }
1617 */