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