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