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