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