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