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