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