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