5 require_once(CONST_BasePath.'/lib/NearPoint.php');
6 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
7 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
13 protected $aLangPrefOrder = array();
15 protected $bIncludeAddressDetails = false;
16 protected $bIncludeExtraTags = false;
17 protected $bIncludeNameDetails = false;
19 protected $bIncludePolygonAsPoints = false;
20 protected $bIncludePolygonAsText = false;
21 protected $bIncludePolygonAsGeoJSON = false;
22 protected $bIncludePolygonAsKML = false;
23 protected $bIncludePolygonAsSVG = false;
24 protected $fPolygonSimplificationThreshold = 0.0;
26 protected $aExcludePlaceIDs = array();
27 protected $bDeDupe = true;
28 protected $bReverseInPlan = false;
30 protected $iLimit = 20;
31 protected $iFinalLimit = 10;
32 protected $iOffset = 0;
33 protected $bFallback = false;
35 protected $aCountryCodes = false;
37 protected $bBoundedSearch = false;
38 protected $aViewBox = false;
39 protected $sViewboxCentreSQL = false;
40 protected $sViewboxSmallSQL = false;
41 protected $sViewboxLargeSQL = false;
43 protected $iMaxRank = 20;
44 protected $iMinAddressRank = 0;
45 protected $iMaxAddressRank = 30;
46 protected $aAddressRankList = array();
47 protected $exactMatchCache = array();
49 protected $sAllowedTypesSQLList = false;
51 protected $sQuery = false;
52 protected $aStructuredQuery = false;
54 protected $oNormalizer = null;
57 public function __construct(&$oDB)
60 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
63 private function normTerm($sTerm)
65 if ($this->oNormalizer === null) {
69 return $this->oNormalizer->transliterate($sTerm);
72 public function setReverseInPlan($bReverse)
74 $this->bReverseInPlan = $bReverse;
77 public function setLanguagePreference($aLangPref)
79 $this->aLangPrefOrder = $aLangPref;
82 public function getMoreUrlParams()
84 if ($this->aStructuredQuery) {
85 $aParams = $this->aStructuredQuery;
87 $aParams = array('q' => $this->sQuery);
90 if ($this->aExcludePlaceIDs) {
91 $aParams['exclude_place_ids'] = implode(',', $this->aExcludePlaceIDs);
94 if ($this->bIncludeAddressDetails) $aParams['addressdetails'] = '1';
95 if ($this->bIncludeExtraTags) $aParams['extratags'] = '1';
96 if ($this->bIncludeNameDetails) $aParams['namedetails'] = '1';
98 if ($this->bIncludePolygonAsPoints) $aParams['polygon'] = '1';
99 if ($this->bIncludePolygonAsText) $aParams['polygon_text'] = '1';
100 if ($this->bIncludePolygonAsGeoJSON) $aParams['polygon_geojson'] = '1';
101 if ($this->bIncludePolygonAsKML) $aParams['polygon_kml'] = '1';
102 if ($this->bIncludePolygonAsSVG) $aParams['polygon_svg'] = '1';
104 if ($this->fPolygonSimplificationThreshold > 0.0) {
105 $aParams['polygon_threshold'] = $this->fPolygonSimplificationThreshold;
108 if ($this->bBoundedSearch) $aParams['bounded'] = '1';
109 if (!$this->bDeDupe) $aParams['dedupe'] = '0';
111 if ($this->aCountryCodes) {
112 $aParams['countrycodes'] = implode(',', $this->aCountryCodes);
115 if ($this->aViewBox) {
116 $aParams['viewbox'] = $this->aViewBox[0].','.$this->aViewBox[3]
117 .','.$this->aViewBox[2].','.$this->aViewBox[1];
123 public function setIncludePolygonAsPoints($b = true)
125 $this->bIncludePolygonAsPoints = $b;
128 public function setIncludePolygonAsText($b = true)
130 $this->bIncludePolygonAsText = $b;
133 public function setIncludePolygonAsGeoJSON($b = true)
135 $this->bIncludePolygonAsGeoJSON = $b;
138 public function setIncludePolygonAsKML($b = true)
140 $this->bIncludePolygonAsKML = $b;
143 public function setIncludePolygonAsSVG($b = true)
145 $this->bIncludePolygonAsSVG = $b;
148 public function setPolygonSimplificationThreshold($f)
150 $this->fPolygonSimplificationThreshold = $f;
153 public function setLimit($iLimit = 10)
155 if ($iLimit > 50) $iLimit = 50;
156 if ($iLimit < 1) $iLimit = 1;
158 $this->iFinalLimit = $iLimit;
159 $this->iLimit = $iLimit + min($iLimit, 10);
162 public function setFeatureType($sFeatureType)
164 switch ($sFeatureType) {
166 $this->setRankRange(4, 4);
169 $this->setRankRange(8, 8);
172 $this->setRankRange(14, 16);
175 $this->setRankRange(8, 20);
180 public function setRankRange($iMin, $iMax)
182 $this->iMinAddressRank = $iMin;
183 $this->iMaxAddressRank = $iMax;
186 public function setRoute($aRoutePoints, $fRouteWidth)
188 $this->aViewBox = false;
190 $this->sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
192 foreach ($aRoutePoints as $aPoint) {
193 $fPoint = (float)$aPoint;
194 $this->sViewboxCentreSQL .= $sSep.$fPoint;
195 $sSep = ($sSep == ' ') ? ',' : ' ';
197 $this->sViewboxCentreSQL .= ")'::geometry,4326)";
199 $this->sViewboxSmallSQL = 'ST_BUFFER('.$this->sViewboxCentreSQL;
200 $this->sViewboxSmallSQL .= ','.($fRouteWidth/69).')';
202 $this->sViewboxLargeSQL = 'ST_BUFFER('.$this->sViewboxCentreSQL;
203 $this->sViewboxLargeSQL .= ','.($fRouteWidth/30).')';
206 public function setViewbox($aViewbox)
208 $this->aViewBox = array_map('floatval', $aViewbox);
210 $this->aViewBox[0] = max(-180.0, min(180, $this->aViewBox[0]));
211 $this->aViewBox[1] = max(-90.0, min(90, $this->aViewBox[1]));
212 $this->aViewBox[2] = max(-180.0, min(180, $this->aViewBox[2]));
213 $this->aViewBox[3] = max(-90.0, min(90, $this->aViewBox[3]));
215 if (abs($this->aViewBox[0] - $this->aViewBox[2]) < 0.000000001
216 || abs($this->aViewBox[1] - $this->aViewBox[3]) < 0.000000001
218 userError("Bad parameter 'viewbox'. Not a box.");
221 $fHeight = $this->aViewBox[0] - $this->aViewBox[2];
222 $fWidth = $this->aViewBox[1] - $this->aViewBox[3];
223 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
224 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
225 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
226 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
228 $this->sViewboxCentreSQL = false;
229 $this->sViewboxSmallSQL = sprintf(
230 'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
236 $this->sViewboxLargeSQL = sprintf(
237 'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
245 public function setQuery($sQueryString)
247 $this->sQuery = $sQueryString;
248 $this->aStructuredQuery = false;
251 public function getQueryString()
253 return $this->sQuery;
257 public function loadParamArray($oParams)
259 $this->bIncludeAddressDetails
260 = $oParams->getBool('addressdetails', $this->bIncludeAddressDetails);
261 $this->bIncludeExtraTags
262 = $oParams->getBool('extratags', $this->bIncludeExtraTags);
263 $this->bIncludeNameDetails
264 = $oParams->getBool('namedetails', $this->bIncludeNameDetails);
266 $this->bBoundedSearch = $oParams->getBool('bounded', $this->bBoundedSearch);
267 $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
269 $this->setLimit($oParams->getInt('limit', $this->iFinalLimit));
270 $this->iOffset = $oParams->getInt('offset', $this->iOffset);
272 $this->bFallback = $oParams->getBool('fallback', $this->bFallback);
274 // List of excluded Place IDs - used for more acurate pageing
275 $sExcluded = $oParams->getStringList('exclude_place_ids');
277 foreach ($sExcluded as $iExcludedPlaceID) {
278 $iExcludedPlaceID = (int)$iExcludedPlaceID;
279 if ($iExcludedPlaceID)
280 $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
283 if (isset($aExcludePlaceIDs))
284 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
287 // Only certain ranks of feature
288 $sFeatureType = $oParams->getString('featureType');
289 if (!$sFeatureType) $sFeatureType = $oParams->getString('featuretype');
290 if ($sFeatureType) $this->setFeatureType($sFeatureType);
293 $sCountries = $oParams->getStringList('countrycodes');
295 foreach ($sCountries as $sCountryCode) {
296 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode)) {
297 $aCountries[] = strtolower($sCountryCode);
300 if (isset($aCountries))
301 $this->aCountryCodes = $aCountries;
304 $aViewbox = $oParams->getStringList('viewboxlbrt');
306 if (count($aViewbox) != 4) {
307 userError("Bad parmater 'viewboxlbrt'. Expected 4 coordinates.");
309 $this->setViewbox($aViewbox);
311 $aViewbox = $oParams->getStringList('viewbox');
313 if (count($aViewbox) != 4) {
314 userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
316 $this->setViewBox($aViewbox);
318 $aRoute = $oParams->getStringList('route');
319 $fRouteWidth = $oParams->getFloat('routewidth');
320 if ($aRoute && $fRouteWidth) {
321 $this->setRoute($aRoute, $fRouteWidth);
327 public function setQueryFromParams($oParams)
330 $sQuery = $oParams->getString('q');
332 $this->setStructuredQuery(
333 $oParams->getString('amenity'),
334 $oParams->getString('street'),
335 $oParams->getString('city'),
336 $oParams->getString('county'),
337 $oParams->getString('state'),
338 $oParams->getString('country'),
339 $oParams->getString('postalcode')
341 $this->setReverseInPlan(false);
343 $this->setQuery($sQuery);
347 public function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
349 $sValue = trim($sValue);
350 if (!$sValue) return false;
351 $this->aStructuredQuery[$sKey] = $sValue;
352 if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30) {
353 $this->iMinAddressRank = $iNewMinAddressRank;
354 $this->iMaxAddressRank = $iNewMaxAddressRank;
356 if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
360 public function setStructuredQuery($sAmenity = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
362 $this->sQuery = false;
365 $this->iMinAddressRank = 0;
366 $this->iMaxAddressRank = 30;
367 $this->aAddressRankList = array();
369 $this->aStructuredQuery = array();
370 $this->sAllowedTypesSQLList = False;
372 $this->loadStructuredAddressElement($sAmenity, 'amenity', 26, 30, false);
373 $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
374 $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
375 $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
376 $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
377 $this->loadStructuredAddressElement($sPostalCode, 'postalcode', 5, 11, array(5, 11));
378 $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
380 if (sizeof($this->aStructuredQuery) > 0) {
381 $this->sQuery = join(', ', $this->aStructuredQuery);
382 if ($this->iMaxAddressRank < 30) {
383 $this->sAllowedTypesSQLList = '(\'place\',\'boundary\')';
388 public function fallbackStructuredQuery()
390 if (!$this->aStructuredQuery) return false;
392 $aParams = $this->aStructuredQuery;
394 if (sizeof($aParams) == 1) return false;
396 $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
398 foreach ($aOrderToFallback as $sType) {
399 if (isset($aParams[$sType])) {
400 unset($aParams[$sType]);
401 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
409 public function getDetails($aPlaceIDs)
411 //$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
412 if (sizeof($aPlaceIDs) == 0) return array();
414 $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
416 // Get the details for display (is this a redundant extra step?)
417 $sPlaceIDs = join(',', array_keys($aPlaceIDs));
419 $sImportanceSQL = '';
420 $sImportanceSQLGeom = '';
421 if ($this->sViewboxSmallSQL) {
422 $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
423 $sImportanceSQLGeom .= " CASE WHEN ST_Contains($this->sViewboxSmallSQL, geometry) THEN 1 ELSE 0.75 END * ";
425 if ($this->sViewboxLargeSQL) {
426 $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
427 $sImportanceSQLGeom .= " CASE WHEN ST_Contains($this->sViewboxLargeSQL, geometry) THEN 1 ELSE 0.75 END * ";
431 $sSQL .= " osm_type,";
435 $sSQL .= " admin_level,";
436 $sSQL .= " rank_search,";
437 $sSQL .= " rank_address,";
438 $sSQL .= " min(place_id) AS place_id, ";
439 $sSQL .= " min(parent_place_id) AS parent_place_id, ";
440 $sSQL .= " country_code, ";
441 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) AS langaddress,";
442 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) AS placename,";
443 $sSQL .= " get_name_by_language(name, ARRAY['ref']) AS ref,";
444 if ($this->bIncludeExtraTags) $sSQL .= "hstore_to_json(extratags)::text AS extra,";
445 if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text AS names,";
446 $sSQL .= " avg(ST_X(centroid)) AS lon, ";
447 $sSQL .= " avg(ST_Y(centroid)) AS lat, ";
448 $sSQL .= " ".$sImportanceSQL."COALESCE(importance,0.75-(rank_search::float/40)) AS importance, ";
450 $sSQL .= " SELECT max(p.importance*(p.rank_address+2))";
452 $sSQL .= " place_addressline s, ";
453 $sSQL .= " placex p";
454 $sSQL .= " WHERE s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END)";
455 $sSQL .= " AND p.place_id = s.address_place_id ";
456 $sSQL .= " AND s.isaddress ";
457 $sSQL .= " AND p.importance is not null ";
458 $sSQL .= " ) AS addressimportance, ";
459 $sSQL .= " (extratags->'place') AS extra_place ";
460 $sSQL .= " FROM placex";
461 $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
463 $sSQL .= " placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
464 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) {
465 $sSQL .= " OR (extratags->'place') = 'city'";
467 if ($this->aAddressRankList) {
468 $sSQL .= " OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
471 if ($this->sAllowedTypesSQLList) {
472 $sSQL .= "AND placex.class in $this->sAllowedTypesSQLList ";
474 $sSQL .= " AND linked_place_id is null ";
475 $sSQL .= " GROUP BY ";
476 $sSQL .= " osm_type, ";
477 $sSQL .= " osm_id, ";
480 $sSQL .= " admin_level, ";
481 $sSQL .= " rank_search, ";
482 $sSQL .= " rank_address, ";
483 $sSQL .= " country_code, ";
484 $sSQL .= " importance, ";
485 if (!$this->bDeDupe) $sSQL .= "place_id,";
486 $sSQL .= " langaddress, ";
487 $sSQL .= " placename, ";
489 if ($this->bIncludeExtraTags) $sSQL .= "extratags, ";
490 if ($this->bIncludeNameDetails) $sSQL .= "name, ";
491 $sSQL .= " extratags->'place' ";
496 $sSQL .= " 'P' as osm_type,";
497 $sSQL .= " (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,";
498 $sSQL .= " 'place' as class, 'postcode' as type,";
499 $sSQL .= " null as admin_level, rank_search, rank_address,";
500 $sSQL .= " place_id, parent_place_id, country_code,";
501 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) AS langaddress,";
502 $sSQL .= " postcode as placename,";
503 $sSQL .= " postcode as ref,";
504 if ($this->bIncludeExtraTags) $sSQL .= "null AS extra,";
505 if ($this->bIncludeNameDetails) $sSQL .= "null AS names,";
506 $sSQL .= " ST_x(st_centroid(geometry)) AS lon, ST_y(st_centroid(geometry)) AS lat,";
507 $sSQL .= $sImportanceSQLGeom."(0.75-(rank_search::float/40)) AS importance, ";
509 $sSQL .= " SELECT max(p.importance*(p.rank_address+2))";
511 $sSQL .= " place_addressline s, ";
512 $sSQL .= " placex p";
513 $sSQL .= " WHERE s.place_id = lp.parent_place_id";
514 $sSQL .= " AND p.place_id = s.address_place_id ";
515 $sSQL .= " AND s.isaddress";
516 $sSQL .= " AND p.importance is not null";
517 $sSQL .= " ) AS addressimportance, ";
518 $sSQL .= " null AS extra_place ";
519 $sSQL .= "FROM location_postcode lp";
520 $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
522 if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank) {
523 // only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines
524 // with start- and endnumber, the common osm housenumbers are usually saved as points
527 $length = count($aPlaceIDs);
528 foreach ($aPlaceIDs as $placeID => $housenumber) {
530 $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
531 if ($i<$length) $sHousenumbers .= ", ";
534 if (CONST_Use_US_Tiger_Data) {
535 // Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
538 $sSQL .= " 'T' AS osm_type, ";
539 $sSQL .= " (SELECT osm_id from placex p WHERE p.place_id=min(blub.parent_place_id)) as osm_id, ";
540 $sSQL .= " 'place' AS class, ";
541 $sSQL .= " 'house' AS type, ";
542 $sSQL .= " null AS admin_level, ";
543 $sSQL .= " 30 AS rank_search, ";
544 $sSQL .= " 30 AS rank_address, ";
545 $sSQL .= " min(place_id) AS place_id, ";
546 $sSQL .= " min(parent_place_id) AS parent_place_id, ";
547 $sSQL .= " 'us' AS country_code, ";
548 $sSQL .= " get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) AS langaddress,";
549 $sSQL .= " null AS placename, ";
550 $sSQL .= " null AS ref, ";
551 if ($this->bIncludeExtraTags) $sSQL .= "null AS extra,";
552 if ($this->bIncludeNameDetails) $sSQL .= "null AS names,";
553 $sSQL .= " avg(st_x(centroid)) AS lon, ";
554 $sSQL .= " avg(st_y(centroid)) AS lat,";
555 $sSQL .= " ".$sImportanceSQL."-1.15 AS importance, ";
557 $sSQL .= " SELECT max(p.importance*(p.rank_address+2))";
559 $sSQL .= " place_addressline s, ";
560 $sSQL .= " placex p";
561 $sSQL .= " WHERE s.place_id = min(blub.parent_place_id)";
562 $sSQL .= " AND p.place_id = s.address_place_id ";
563 $sSQL .= " AND s.isaddress";
564 $sSQL .= " AND p.importance is not null";
565 $sSQL .= " ) AS addressimportance, ";
566 $sSQL .= " null AS extra_place ";
568 $sSQL .= " SELECT place_id, "; // interpolate the Tiger housenumbers here
569 $sSQL .= " ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) AS centroid, ";
570 $sSQL .= " parent_place_id, ";
571 $sSQL .= " housenumber_for_place";
573 $sSQL .= " location_property_tiger ";
574 $sSQL .= " JOIN (values ".$sHousenumbers.") AS housenumbers(place_id, housenumber_for_place) USING(place_id)) ";
576 $sSQL .= " housenumber_for_place>=0";
577 $sSQL .= " AND 30 between $this->iMinAddressRank AND $this->iMaxAddressRank";
578 $sSQL .= " ) AS blub"; //postgres wants an alias here
579 $sSQL .= " GROUP BY";
580 $sSQL .= " place_id, ";
581 $sSQL .= " housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
582 if (!$this->bDeDupe) $sSQL .= ", place_id ";
585 // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
588 $sSQL .= " 'W' AS osm_type, ";
589 $sSQL .= " osm_id, ";
590 $sSQL .= " 'place' AS class, ";
591 $sSQL .= " 'house' AS type, ";
592 $sSQL .= " null AS admin_level, ";
593 $sSQL .= " 30 AS rank_search, ";
594 $sSQL .= " 30 AS rank_address, ";
595 $sSQL .= " min(place_id) as place_id, ";
596 $sSQL .= " min(parent_place_id) AS parent_place_id, ";
597 $sSQL .= " country_code, ";
598 $sSQL .= " get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) AS langaddress, ";
599 $sSQL .= " null AS placename, ";
600 $sSQL .= " null AS ref, ";
601 if ($this->bIncludeExtraTags) $sSQL .= "null AS extra, ";
602 if ($this->bIncludeNameDetails) $sSQL .= "null AS names, ";
603 $sSQL .= " AVG(st_x(centroid)) AS lon, ";
604 $sSQL .= " AVG(st_y(centroid)) AS lat, ";
605 $sSQL .= " ".$sImportanceSQL."-0.1 AS importance, "; // slightly smaller than the importance for normal houses with rank 30, which is 0
608 $sSQL .= " MAX(p.importance*(p.rank_address+2)) ";
610 $sSQL .= " place_addressline s, ";
611 $sSQL .= " placex p";
612 $sSQL .= " WHERE s.place_id = min(blub.parent_place_id) ";
613 $sSQL .= " AND p.place_id = s.address_place_id ";
614 $sSQL .= " AND s.isaddress ";
615 $sSQL .= " AND p.importance is not null";
616 $sSQL .= " ) AS addressimportance,";
617 $sSQL .= " null AS extra_place ";
620 $sSQL .= " osm_id, ";
621 $sSQL .= " place_id, ";
622 $sSQL .= " country_code, ";
623 $sSQL .= " CASE "; // interpolate the housenumbers here
624 $sSQL .= " WHEN startnumber != endnumber ";
625 $sSQL .= " THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
626 $sSQL .= " ELSE ST_LineInterpolatePoint(linegeo, 0.5) ";
627 $sSQL .= " END as centroid, ";
628 $sSQL .= " parent_place_id, ";
629 $sSQL .= " housenumber_for_place ";
631 $sSQL .= " location_property_osmline ";
632 $sSQL .= " JOIN (values ".$sHousenumbers.") AS housenumbers(place_id, housenumber_for_place) USING(place_id)";
634 $sSQL .= " WHERE housenumber_for_place>=0 ";
635 $sSQL .= " AND 30 between $this->iMinAddressRank AND $this->iMaxAddressRank";
636 $sSQL .= " ) as blub"; //postgres wants an alias here
637 $sSQL .= " GROUP BY ";
638 $sSQL .= " osm_id, ";
639 $sSQL .= " place_id, ";
640 $sSQL .= " housenumber_for_place, ";
641 $sSQL .= " country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
642 if (!$this->bDeDupe) $sSQL .= ", place_id ";
644 if (CONST_Use_Aux_Location_data) {
647 $sSQL .= " 'L' AS osm_type, ";
648 $sSQL .= " place_id AS osm_id, ";
649 $sSQL .= " 'place' AS class,";
650 $sSQL .= " 'house' AS type, ";
651 $sSQL .= " null AS admin_level, ";
652 $sSQL .= " 0 AS rank_search,";
653 $sSQL .= " 0 AS rank_address, ";
654 $sSQL .= " min(place_id) AS place_id,";
655 $sSQL .= " min(parent_place_id) AS parent_place_id, ";
656 $sSQL .= " 'us' AS country_code, ";
657 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) AS langaddress, ";
658 $sSQL .= " null AS placename, ";
659 $sSQL .= " null AS ref, ";
660 if ($this->bIncludeExtraTags) $sSQL .= "null AS extra, ";
661 if ($this->bIncludeNameDetails) $sSQL .= "null AS names, ";
662 $sSQL .= " avg(ST_X(centroid)) AS lon, ";
663 $sSQL .= " avg(ST_Y(centroid)) AS lat, ";
664 $sSQL .= " ".$sImportanceSQL."-1.10 AS importance, ";
666 $sSQL .= " SELECT max(p.importance*(p.rank_address+2))";
668 $sSQL .= " place_addressline s, ";
669 $sSQL .= " placex p";
670 $sSQL .= " WHERE s.place_id = min(location_property_aux.parent_place_id)";
671 $sSQL .= " AND p.place_id = s.address_place_id ";
672 $sSQL .= " AND s.isaddress";
673 $sSQL .= " AND p.importance is not null";
674 $sSQL .= " ) AS addressimportance, ";
675 $sSQL .= " null AS extra_place ";
676 $sSQL .= " FROM location_property_aux ";
677 $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
678 $sSQL .= " AND 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
679 $sSQL .= " GROUP BY ";
680 $sSQL .= " place_id, ";
681 if (!$this->bDeDupe) $sSQL .= "place_id, ";
682 $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) ";
686 $sSQL .= " order by importance desc";
691 $aSearchResults = chksql(
692 $this->oDB->getAll($sSQL),
693 "Could not get details for place."
696 return $aSearchResults;
699 public function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases, $sNormQuery)
702 Calculate all searches using aValidTokens i.e.
703 'Wodsworth Road, Sheffield' =>
707 0 1 (wodsworth)(road)
710 Score how good the search is so they can be ordered
712 foreach ($aPhrases as $iPhrase => $aPhrase) {
713 $aNewPhraseSearches = array();
714 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
715 else $sPhraseType = '';
717 foreach ($aPhrase['wordsets'] as $iWordSet => $aWordset) {
718 // Too many permutations - too expensive
719 if ($iWordSet > 120) break;
721 $aWordsetSearches = $aSearches;
723 // Add all words from this wordset
724 foreach ($aWordset as $iToken => $sToken) {
725 //echo "<br><b>$sToken</b>";
726 $aNewWordsetSearches = array();
728 foreach ($aWordsetSearches as $aCurrentSearch) {
730 //var_dump($aCurrentSearch);
733 // If the token is valid
734 if (isset($aValidTokens[' '.$sToken])) {
735 // TODO variable should go into aCurrentSearch
736 $bHavePostcode = false;
737 foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
738 $aSearch = $aCurrentSearch;
739 $aSearch['iSearchRank']++;
740 if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0') {
741 if ($aSearch['sCountryCode'] === false) {
742 $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
743 // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
744 if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases))) {
745 $aSearch['iSearchRank'] += 5;
747 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
749 } elseif (($sPhraseType == '' || $sPhraseType == 'postalcode') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'postcode') {
750 // 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
751 if (!$bHavePostcode && $aSearch['sPostcode'] === '' && $aSearch['sHouseNumber'] === '' &&
752 isset($aSearchTerm['word']) && $aSearchTerm['word'] && strpos($sNormQuery, $this->normTerm($aSearchTerm['word'])) !== false) {
753 // If we have structured search or this is the first term,
754 // make the postcode the primary search element.
755 if ($aSearch['sOperator'] === '' && ($sPhraseType == 'postalcode' || ($iToken == 0 && $iPhrase == 0))) {
756 $aNewSearch = $aSearch;
757 $aNewSearch['sOperator'] = 'postcode';
758 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
759 $aNewSearch['aName'] = array($aSearchTerm['word_id'] => $aSearchTerm['word']);
760 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
761 $bHavePostcode = true;
764 // If we have a structured search or this is not the first term,
765 // add the postcode as an addendum.
766 if ($aSearch['sOperator'] !== 'postcode' && ($sPhraseType == 'postalcode' || sizeof($aSearch['aName']))) {
767 $aSearch['sPostcode'] = $aSearchTerm['word'];
768 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
771 } elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house') {
772 if ($aSearch['sHouseNumber'] === '' && $aSearch['sOperator'] !== 'postcode') {
773 $aSearch['sHouseNumber'] = $sToken;
774 // sanity check: if the housenumber is not mainly made
775 // up of numbers, add a penalty
776 if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
777 // also housenumbers should appear in the first or second phrase
778 if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
779 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
781 // Fall back to not searching for this item (better than nothing)
782 $aSearch = $aCurrentSearch;
783 $aSearch['iSearchRank'] += 1;
784 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
787 } elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null) {
788 // require a normalized exact match of the term
789 // if we have the normalizer version of the query
791 if ($aSearch['sOperator'] === ''
792 && ($sNormQuery === null || !($aSearchTerm['word'] && strpos($sNormQuery, $aSearchTerm['word']) === false))) {
793 $aSearch['sClass'] = $aSearchTerm['class'];
794 $aSearch['sType'] = $aSearchTerm['type'];
795 if ($aSearchTerm['operator'] == '') {
796 $aSearch['sOperator'] = sizeof($aSearch['aName']) ? 'name' : 'near';
797 $aSearch['iSearchRank'] += 2;
799 $aSearch['sOperator'] = 'near'; // near = in for the moment
802 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
804 } elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
805 if (sizeof($aSearch['aName'])) {
806 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
807 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
809 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
810 $aSearch['iSearchRank'] += 1000; // skip;
813 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
814 //$aSearch['iNamePhrase'] = $iPhrase;
816 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
820 // Look for partial matches.
821 // Note that there is no point in adding country terms here
822 // because country are omitted in the address.
823 if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country') {
824 // Allow searching for a word - but at extra cost
825 foreach ($aValidTokens[$sToken] as $aSearchTerm) {
826 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
827 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false) {
828 $aSearch = $aCurrentSearch;
829 $aSearch['iSearchRank'] += 1;
830 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
831 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
832 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
833 } elseif (isset($aValidTokens[' '.$sToken])) { // revert to the token version?
834 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
835 $aSearch['iSearchRank'] += 1;
836 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
837 foreach ($aValidTokens[' '.$sToken] as $aSearchTermToken) {
838 if (empty($aSearchTermToken['country_code'])
839 && empty($aSearchTermToken['lat'])
840 && empty($aSearchTermToken['class'])
842 $aSearch = $aCurrentSearch;
843 $aSearch['iSearchRank'] += 1;
844 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
845 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
849 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
850 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
851 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
855 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase) {
856 $aSearch = $aCurrentSearch;
857 $aSearch['iSearchRank'] += 1;
858 if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
859 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
860 if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
861 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
863 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
865 $aSearch['iNamePhrase'] = $iPhrase;
866 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
871 // Allow skipping a word - but at EXTREAM cost
872 //$aSearch = $aCurrentSearch;
873 //$aSearch['iSearchRank']+=100;
874 //$aNewWordsetSearches[] = $aSearch;
878 usort($aNewWordsetSearches, 'bySearchRank');
879 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
881 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
883 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
884 usort($aNewPhraseSearches, 'bySearchRank');
886 $aSearchHash = array();
887 foreach ($aNewPhraseSearches as $iSearch => $aSearch) {
888 $sHash = serialize($aSearch);
889 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
890 else $aSearchHash[$sHash] = 1;
893 $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
896 // Re-group the searches by their score, junk anything over 20 as just not worth trying
897 $aGroupedSearches = array();
898 foreach ($aNewPhraseSearches as $aSearch) {
899 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
900 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
901 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
904 ksort($aGroupedSearches);
907 $aSearches = array();
908 foreach ($aGroupedSearches as $iScore => $aNewSearches) {
909 $iSearchCount += sizeof($aNewSearches);
910 $aSearches = array_merge($aSearches, $aNewSearches);
911 if ($iSearchCount > 50) break;
914 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
917 // Revisit searches, giving penalty to unlikely combinations
918 $aGroupedSearches = array();
919 foreach ($aSearches as $aSearch) {
920 if (!$aSearch['aName']) {
921 if ($aSearch['sHouseNumber']) {
925 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
927 ksort($aGroupedSearches);
929 return $aGroupedSearches;
932 /* Perform the actual query lookup.
934 Returns an ordered list of results, each with the following fields:
935 osm_type: type of corresponding OSM object
939 P - postcode (internally computed)
940 osm_id: id of corresponding OSM object
941 class: general object class (corresponds to tag key of primary OSM tag)
942 type: subclass of object (corresponds to tag value of primary OSM tag)
943 admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
944 rank_search: rank in search hierarchy
945 (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
946 rank_address: rank in address hierarchy (determines orer in address)
947 place_id: internal key (may differ between different instances)
948 country_code: ISO country code
949 langaddress: localized full address
950 placename: localized name of object
951 ref: content of ref tag (if available)
954 importance: importance of place based on Wikipedia link count
955 addressimportance: cumulated importance of address elements
956 extra_place: type of place (for admin boundaries, if there is a place tag)
957 aBoundingBox: bounding Box
958 label: short description of the object class/type (English only)
959 name: full name (currently the same as langaddress)
960 foundorder: secondary ordering for places with same importance
964 public function lookup()
966 if (!$this->sQuery && !$this->aStructuredQuery) return array();
968 $sNormQuery = $this->normTerm($this->sQuery);
969 $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
970 $sCountryCodesSQL = false;
971 if ($this->aCountryCodes) {
972 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
975 $sQuery = $this->sQuery;
976 if (!preg_match('//u', $sQuery)) {
977 userError("Query string is not UTF-8 encoded.");
980 // Conflicts between US state abreviations and various words for 'the' in different languages
981 if (isset($this->aLangPrefOrder['name:en'])) {
982 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/', '\1illinois\2', $sQuery);
983 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/', '\1alabama\2', $sQuery);
984 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/', '\1louisiana\2', $sQuery);
987 $bBoundingBoxSearch = $this->bBoundedSearch && $this->sViewboxSmallSQL;
988 if ($this->sViewboxCentreSQL) {
989 // For complex viewboxes (routes) precompute the bounding geometry
991 $this->oDB->getOne("select ".$this->sViewboxSmallSQL),
992 "Could not get small viewbox"
994 $this->sViewboxSmallSQL = "'".$sGeom."'::geometry";
997 $this->oDB->getOne("select ".$this->sViewboxLargeSQL),
998 "Could not get large viewbox"
1000 $this->sViewboxLargeSQL = "'".$sGeom."'::geometry";
1003 // Do we have anything that looks like a lat/lon pair?
1004 $oNearPoint = false;
1005 if ($aLooksLike = NearPoint::extractFromQuery($sQuery)) {
1006 $oNearPoint = $aLooksLike['pt'];
1007 $sQuery = $aLooksLike['query'];
1010 $aSearchResults = array();
1011 if ($sQuery || $this->aStructuredQuery) {
1012 // Start with a blank search
1016 'iNamePhrase' => -1,
1017 'sCountryCode' => false,
1019 'aAddress' => array(),
1020 'aFullNameAddress' => array(),
1021 'aNameNonSearch' => array(),
1022 'aAddressNonSearch' => array(),
1024 'aFeatureName' => array(),
1027 'sHouseNumber' => '',
1029 'oNear' => $oNearPoint
1033 // Any 'special' terms in the search?
1034 $bSpecialTerms = false;
1035 preg_match_all('/\\[([\\w_]*)=([\\w_]*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
1036 foreach ($aSpecialTermsRaw as $aSpecialTerm) {
1037 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
1038 if (!$bSpecialTerms) {
1039 $aNewSearches = array();
1040 foreach ($aSearches as $aSearch) {
1041 $aNewSearch = $aSearch;
1042 $aNewSearch['sClass'] = $aSpecialTerm[1];
1043 $aNewSearch['sType'] = $aSpecialTerm[2];
1044 $aNewSearches[] = $aNewSearch;
1047 $aSearches = $aNewSearches;
1048 $bSpecialTerms = true;
1052 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
1053 if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity']) {
1054 $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
1055 unset($this->aStructuredQuery['amenity']);
1058 foreach ($aSpecialTermsRaw as $aSpecialTerm) {
1059 $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
1060 if ($bSpecialTerms) {
1064 $sToken = chksql($this->oDB->getOne("SELECT make_standard_name('".pg_escape_string($aSpecialTerm[1])."') AS string"));
1065 $sSQL = 'SELECT * ';
1067 $sSQL .= ' SELECT word_id, word_token, word, class, type, country_code, operator';
1068 $sSQL .= ' FROM word ';
1069 $sSQL .= ' WHERE word_token in (\' '.$sToken.'\')';
1071 $sSQL .= ' WHERE (class is not null AND class not in (\'place\'))';
1072 if (CONST_Debug) var_Dump($sSQL);
1073 $aSearchWords = chksql($this->oDB->getAll($sSQL));
1074 $aNewSearches = array();
1075 foreach ($aSearches as $aSearch) {
1076 foreach ($aSearchWords as $aSearchTerm) {
1077 $aNewSearch = $aSearch;
1078 $aNewSearch['sClass'] = $aSearchTerm['class'];
1079 $aNewSearch['sType'] = $aSearchTerm['type'];
1080 $aNewSearches[] = $aNewSearch;
1081 $bSpecialTerms = true;
1084 $aSearches = $aNewSearches;
1087 // Split query into phrases
1088 // Commas are used to reduce the search space by indicating where phrases split
1089 if ($this->aStructuredQuery) {
1090 $aPhrases = $this->aStructuredQuery;
1091 $bStructuredPhrases = true;
1093 $aPhrases = explode(',', $sQuery);
1094 $bStructuredPhrases = false;
1097 // Convert each phrase to standard form
1098 // Create a list of standard words
1099 // Get all 'sets' of words
1100 // Generate a complete list of all
1102 foreach ($aPhrases as $iPhrase => $sPhrase) {
1104 $this->oDB->getRow("SELECT make_standard_name('".pg_escape_string($sPhrase)."') as string"),
1105 "Cannot normalize query string (is it a UTF-8 string?)"
1107 if (trim($aPhrase['string'])) {
1108 $aPhrases[$iPhrase] = $aPhrase;
1109 $aPhrases[$iPhrase]['words'] = explode(' ', $aPhrases[$iPhrase]['string']);
1110 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
1111 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
1113 unset($aPhrases[$iPhrase]);
1117 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
1118 $aPhraseTypes = array_keys($aPhrases);
1119 $aPhrases = array_values($aPhrases);
1121 if (sizeof($aTokens)) {
1122 // Check which tokens we have, get the ID numbers
1123 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code, operator, search_name_count';
1124 $sSQL .= ' FROM word ';
1125 $sSQL .= ' WHERE word_token in ('.join(',', array_map("getDBQuoted", $aTokens)).')';
1127 if (CONST_Debug) var_Dump($sSQL);
1129 $aValidTokens = array();
1130 $aDatabaseWords = chksql(
1131 $this->oDB->getAll($sSQL),
1132 "Could not get word tokens."
1134 $aPossibleMainWordIDs = array();
1135 $aWordFrequencyScores = array();
1136 foreach ($aDatabaseWords as $aToken) {
1137 // Very special case - require 2 letter country param to match the country code found
1138 if ($bStructuredPhrases && $aToken['country_code'] && !empty($this->aStructuredQuery['country'])
1139 && strlen($this->aStructuredQuery['country']) == 2 && strtolower($this->aStructuredQuery['country']) != $aToken['country_code']
1144 if (isset($aValidTokens[$aToken['word_token']])) {
1145 $aValidTokens[$aToken['word_token']][] = $aToken;
1147 $aValidTokens[$aToken['word_token']] = array($aToken);
1149 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
1150 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
1152 if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
1154 // US ZIP+4 codes - if there is no token, merge in the 5-digit ZIP code
1155 foreach ($aTokens as $sToken) {
1156 if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
1157 if (isset($aValidTokens[$aData[1]])) {
1158 foreach ($aValidTokens[$aData[1]] as $aToken) {
1159 if (!$aToken['class']) {
1160 if (isset($aValidTokens[$sToken])) {
1161 $aValidTokens[$sToken][] = $aToken;
1163 $aValidTokens[$sToken] = array($aToken);
1171 foreach ($aTokens as $sToken) {
1172 // Unknown single word token with a number - assume it is a house number
1173 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken, ' ') === false && preg_match('/[0-9]/', $sToken)) {
1174 $aValidTokens[' '.$sToken] = array(array('class' => 'place', 'type' => 'house'));
1178 // Any words that have failed completely?
1179 // TODO: suggestions
1181 // Start the search process
1182 // array with: placeid => -1 | tiger-housenumber
1183 $aResultPlaceIDs = array();
1185 $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases, $sNormQuery);
1187 if ($this->bReverseInPlan) {
1188 // Reverse phrase array and also reverse the order of the wordsets in
1189 // the first and final phrase. Don't bother about phrases in the middle
1190 // because order in the address doesn't matter.
1191 $aPhrases = array_reverse($aPhrases);
1192 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1193 if (sizeof($aPhrases) > 1) {
1194 $aFinalPhrase = end($aPhrases);
1195 $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1197 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false, $sNormQuery);
1199 foreach ($aGroupedSearches as $aSearches) {
1200 foreach ($aSearches as $aSearch) {
1201 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
1202 if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1203 $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1208 $aGroupedSearches = $aReverseGroupedSearches;
1209 ksort($aGroupedSearches);
1212 // Re-group the searches by their score, junk anything over 20 as just not worth trying
1213 $aGroupedSearches = array();
1214 foreach ($aSearches as $aSearch) {
1215 if ($aSearch['iSearchRank'] < $this->iMaxRank) {
1216 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1217 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1220 ksort($aGroupedSearches);
1223 if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0) {
1224 $aCopyGroupedSearches = $aGroupedSearches;
1225 foreach ($aCopyGroupedSearches as $iGroup => $aSearches) {
1226 foreach ($aSearches as $iSearch => $aSearch) {
1227 $aReductionsList = array($aSearch['aAddress']);
1228 $iSearchRank = $aSearch['iSearchRank'];
1229 while (sizeof($aReductionsList) > 0) {
1231 if ($iSearchRank > iMaxRank) break 3;
1232 $aNewReductionsList = array();
1233 foreach ($aReductionsList as $aReductionsWordList) {
1234 for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++) {
1235 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1236 $aReverseSearch = $aSearch;
1237 $aSearch['aAddress'] = $aReductionsWordListResult;
1238 $aSearch['iSearchRank'] = $iSearchRank;
1239 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1240 if (sizeof($aReductionsWordListResult) > 0) {
1241 $aNewReductionsList[] = $aReductionsWordListResult;
1245 $aReductionsList = $aNewReductionsList;
1249 ksort($aGroupedSearches);
1252 // Filter out duplicate searches
1253 $aSearchHash = array();
1254 foreach ($aGroupedSearches as $iGroup => $aSearches) {
1255 foreach ($aSearches as $iSearch => $aSearch) {
1256 $sHash = serialize($aSearch);
1257 if (isset($aSearchHash[$sHash])) {
1258 unset($aGroupedSearches[$iGroup][$iSearch]);
1259 if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1261 $aSearchHash[$sHash] = 1;
1266 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1270 foreach ($aGroupedSearches as $iGroupedRank => $aSearches) {
1272 foreach ($aSearches as $aSearch) {
1274 $searchedHousenumber = -1;
1276 if (CONST_Debug) echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>";
1277 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1279 if ($sCountryCodesSQL && $aSearch['sCountryCode'] && !in_array($aSearch['sCountryCode'], $this->aCountryCodes)) {
1283 // No location term?
1284 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress'])) {
1285 if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'] && !$aSearch['oNear']) {
1286 // Just looking for a country by code - look it up
1287 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank) {
1288 $sSQL = "SELECT place_id FROM placex WHERE country_code='".$aSearch['sCountryCode']."' AND rank_search = 4";
1289 if ($bBoundingBoxSearch)
1290 $sSQL .= " AND _st_intersects($this->sViewboxSmallSQL, geometry)";
1291 $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
1292 if (CONST_Debug) var_dump($sSQL);
1293 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1295 $aPlaceIDs = array();
1298 if (!$bBoundingBoxSearch && !$aSearch['oNear']) continue;
1299 if (!$aSearch['sClass']) continue;
1301 $sSQL = "SELECT COUNT(*) FROM pg_tables WHERE tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1302 if (chksql($this->oDB->getOne($sSQL))) {
1303 $sSQL = "SELECT place_id FROM place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1304 if ($sCountryCodesSQL) $sSQL .= " JOIN placex USING (place_id)";
1305 if ($aSearch['oNear']) {
1306 $sSQL .= " WHERE ".$aSearch['oNear']->withinSQL('ct.centroid');
1308 $sSQL .= " WHERE st_contains($this->sViewboxSmallSQL, ct.centroid)";
1310 if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
1311 if (sizeof($this->aExcludePlaceIDs)) {
1312 $sSQL .= " AND place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1314 if ($this->sViewboxCentreSQL) {
1315 $sSQL .= " ORDER BY ST_Distance($this->sViewboxCentreSQL, ct.centroid) ASC";
1316 } elseif ($aSearch['oNear']) {
1317 $sSQL .= " ORDER BY ".$aSearch['oNear']->distanceSQL('ct.centroid').' ASC';
1319 $sSQL .= " limit $this->iLimit";
1320 if (CONST_Debug) var_dump($sSQL);
1321 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1322 } else if ($aSearch['oNear']) {
1323 $sSQL = "SELECT place_id ";
1324 $sSQL .= "FROM placex ";
1325 $sSQL .= "WHERE class='".$aSearch['sClass']."' ";
1326 $sSQL .= " AND type='".$aSearch['sType']."'";
1327 $sSQL .= " AND ".$aSearch['oNear']->withinSQL('geometry');
1328 $sSQL .= " AND linked_place_id is null";
1329 if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
1330 $sSQL .= " ORDER BY ".$aSearch['oNear']->distanceSQL('centroid')." ASC";
1331 $sSQL .= " LIMIT $this->iLimit";
1332 if (CONST_Debug) var_dump($sSQL);
1333 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1336 } elseif ($aSearch['oNear'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
1337 // If a coordinate is given, the search must either
1338 // be for a name or a special search. Ignore everythin else.
1339 $aPlaceIDs = array();
1340 } elseif ($aSearch['sOperator'] == 'postcode') {
1341 $sSQL = "SELECT p.place_id FROM location_postcode p ";
1342 if (sizeof($aSearch['aAddress'])) {
1343 $sSQL .= ", search_name s ";
1344 $sSQL .= "WHERE s.place_id = p.parent_place_id ";
1345 $sSQL .= "AND array_cat(s.nameaddress_vector, s.name_vector) @> ARRAY[".join($aSearch['aAddress'], ",")."] AND ";
1349 $sSQL .= "p.postcode = '".pg_escape_string(reset($aSearch['aName']))."'";
1350 if ($aSearch['sCountryCode']) {
1351 $sSQL .= " AND p.country_code = '".$aSearch['sCountryCode']."'";
1352 } elseif ($sCountryCodesSQL) {
1353 $sSQL .= " AND p.country_code in ($sCountryCodesSQL)";
1355 $sSQL .= " LIMIT $this->iLimit";
1356 if (CONST_Debug) var_dump($sSQL);
1357 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1359 $aPlaceIDs = array();
1361 // First we need a position, either aName or fLat or both
1365 if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress'])) {
1366 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1369 $aOrder[0] .= " EXISTS(";
1370 $aOrder[0] .= " SELECT place_id ";
1371 $aOrder[0] .= " FROM placex ";
1372 $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
1373 $aOrder[0] .= " AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."' ";
1374 $aOrder[0] .= " LIMIT 1";
1375 $aOrder[0] .= " ) ";
1376 // also housenumbers from interpolation lines table are needed
1377 $aOrder[0] .= " OR EXISTS(";
1378 $aOrder[0] .= " SELECT place_id ";
1379 $aOrder[0] .= " FROM location_property_osmline ";
1380 $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
1381 $aOrder[0] .= " AND startnumber is not NULL";
1382 $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber']).">=startnumber ";
1383 $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber'])."<=endnumber ";
1384 $aOrder[0] .= " LIMIT 1";
1387 $aOrder[0] .= " DESC";
1390 // TODO: filter out the pointless search terms (2 letter name tokens and less)
1391 // they might be right - but they are just too darned expensive to run
1392 if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'], ",")."]";
1393 if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'], ",")."]";
1394 if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) {
1395 // For infrequent name terms disable index usage for address
1396 if (CONST_Search_NameOnlySearchFrequencyThreshold
1397 && sizeof($aSearch['aName']) == 1
1398 && $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold
1400 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'], $aSearch['aAddressNonSearch']), ",")."]";
1402 $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'], ",")."]";
1403 if (sizeof($aSearch['aAddressNonSearch'])) {
1404 $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'], ",")."]";
1408 if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1409 if ($aSearch['sHouseNumber']) {
1410 $aTerms[] = "address_rank between 16 and 27";
1411 } elseif (!$aSearch['sClass'] || $aSearch['sOperator'] == 'name') {
1412 if ($this->iMinAddressRank > 0) {
1413 $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1415 if ($this->iMaxAddressRank < 30) {
1416 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1419 if ($aSearch['oNear']) {
1420 $aTerms[] = $aSearch['oNear']->withinSQL('centroid');
1422 $aOrder[] = $aSearch['oNear']->distanceSQL('centroid');
1423 } elseif ($aSearch['sPostcode']) {
1424 if (!sizeof($aSearch['aAddress'])) {
1425 $aTerms[] = "EXISTS(SELECT place_id FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."' AND ST_DWithin(search_name.centroid, p.geometry, 0.1))";
1427 $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."')";
1430 if (sizeof($this->aExcludePlaceIDs)) {
1431 $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1433 if ($sCountryCodesSQL) {
1434 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1437 if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1439 $aOrder[] = $oNearPoint->distanceSQL('centroid');
1442 if ($aSearch['sHouseNumber']) {
1443 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1445 $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
1447 if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * CASE WHEN ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1448 if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * CASE WHEN ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1450 $aOrder[] = "$sImportanceSQL DESC";
1451 if (sizeof($aSearch['aFullNameAddress'])) {
1452 $sExactMatchSQL = ' ( ';
1453 $sExactMatchSQL .= ' SELECT count(*) FROM ( ';
1454 $sExactMatchSQL .= ' SELECT unnest(ARRAY['.join($aSearch['aFullNameAddress'], ",").']) ';
1455 $sExactMatchSQL .= ' INTERSECT ';
1456 $sExactMatchSQL .= ' SELECT unnest(nameaddress_vector)';
1457 $sExactMatchSQL .= ' ) s';
1458 $sExactMatchSQL .= ') as exactmatch';
1459 $aOrder[] = 'exactmatch DESC';
1461 $sExactMatchSQL = '0::int as exactmatch';
1464 if (sizeof($aTerms)) {
1465 $sSQL = "SELECT place_id, ";
1466 $sSQL .= $sExactMatchSQL;
1467 $sSQL .= " FROM search_name";
1468 $sSQL .= " WHERE ".join(' and ', $aTerms);
1469 $sSQL .= " ORDER BY ".join(', ', $aOrder);
1470 if ($aSearch['sHouseNumber'] || $aSearch['sClass']) {
1471 $sSQL .= " LIMIT 20";
1472 } elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass']) {
1473 $sSQL .= " LIMIT 1";
1475 $sSQL .= " LIMIT ".$this->iLimit;
1478 if (CONST_Debug) var_dump($sSQL);
1479 $aViewBoxPlaceIDs = chksql(
1480 $this->oDB->getAll($sSQL),
1481 "Could not get places for search terms."
1483 //var_dump($aViewBoxPlaceIDs);
1484 // Did we have an viewbox matches?
1485 $aPlaceIDs = array();
1486 $bViewBoxMatch = false;
1487 foreach ($aViewBoxPlaceIDs as $aViewBoxRow) {
1488 //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1489 //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1490 //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1491 //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1492 $aPlaceIDs[] = $aViewBoxRow['place_id'];
1493 $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1496 //var_Dump($aPlaceIDs);
1499 //now search for housenumber, if housenumber provided
1500 if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs)) {
1501 $searchedHousenumber = intval($aSearch['sHouseNumber']);
1502 $aRoadPlaceIDs = $aPlaceIDs;
1503 $sPlaceIDs = join(',', $aPlaceIDs);
1505 // Now they are indexed, look for a house attached to a street we found
1506 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1507 $sSQL = "SELECT place_id FROM placex ";
1508 $sSQL .= "WHERE parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1509 if (sizeof($this->aExcludePlaceIDs)) {
1510 $sSQL .= " AND place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1512 $sSQL .= " LIMIT $this->iLimit";
1513 if (CONST_Debug) var_dump($sSQL);
1514 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1516 // if nothing found, search in the interpolation line table
1517 if (!sizeof($aPlaceIDs)) {
1518 // do we need to use transliteration and the regex for housenumbers???
1519 //new query for lines, not housenumbers anymore
1520 $sSQL = "SELECT distinct place_id FROM location_property_osmline";
1521 $sSQL .= " WHERE startnumber is not NULL and parent_place_id in (".$sPlaceIDs.") and (";
1522 if ($searchedHousenumber%2 == 0) {
1523 //if housenumber is even, look for housenumber in streets with interpolationtype even or all
1524 $sSQL .= "interpolationtype='even'";
1526 //look for housenumber in streets with interpolationtype odd or all
1527 $sSQL .= "interpolationtype='odd'";
1529 $sSQL .= " or interpolationtype='all') and ";
1530 $sSQL .= $searchedHousenumber.">=startnumber and ";
1531 $sSQL .= $searchedHousenumber."<=endnumber";
1533 if (sizeof($this->aExcludePlaceIDs)) {
1534 $sSQL .= " AND place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1536 //$sSQL .= " limit $this->iLimit";
1537 if (CONST_Debug) var_dump($sSQL);
1539 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1542 // If nothing found try the aux fallback table
1543 if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs)) {
1544 $sSQL = "SELECT place_id FROM location_property_aux ";
1545 $sSQL .= " WHERE parent_place_id in (".$sPlaceIDs.") ";
1546 $sSQL .= " AND housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1547 if (sizeof($this->aExcludePlaceIDs)) {
1548 $sSQL .= " AND parent_place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1550 //$sSQL .= " limit $this->iLimit";
1551 if (CONST_Debug) var_dump($sSQL);
1552 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1555 //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
1556 if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs)) {
1557 $sSQL = "SELECT distinct place_id FROM location_property_tiger";
1558 $sSQL .= " WHERE parent_place_id in (".$sPlaceIDs.") and (";
1559 if ($searchedHousenumber%2 == 0) {
1560 $sSQL .= "interpolationtype='even'";
1562 $sSQL .= "interpolationtype='odd'";
1564 $sSQL .= " or interpolationtype='all') and ";
1565 $sSQL .= $searchedHousenumber.">=startnumber and ";
1566 $sSQL .= $searchedHousenumber."<=endnumber";
1568 if (sizeof($this->aExcludePlaceIDs)) {
1569 $sSQL .= " AND place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1571 //$sSQL .= " limit $this->iLimit";
1572 if (CONST_Debug) var_dump($sSQL);
1574 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
1577 // Fallback to the road (if no housenumber was found)
1578 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber'])
1579 && ($aSearch['aAddress'] || $aSearch['sCountryCode'])) {
1580 $aPlaceIDs = $aRoadPlaceIDs;
1581 //set to -1, if no housenumbers were found
1582 $searchedHousenumber = -1;
1584 //else: housenumber was found, remains saved in searchedHousenumber
1588 if ($aSearch['sClass'] && sizeof($aPlaceIDs)) {
1589 $sPlaceIDs = join(',', $aPlaceIDs);
1590 $aClassPlaceIDs = array();
1592 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name') {
1593 // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1594 $sSQL = "SELECT place_id ";
1595 $sSQL .= " FROM placex ";
1596 $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
1597 $sSQL .= " AND class='".$aSearch['sClass']."' ";
1598 $sSQL .= " AND type='".$aSearch['sType']."'";
1599 $sSQL .= " AND linked_place_id is null";
1600 if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
1601 $sSQL .= " ORDER BY rank_search ASC ";
1602 $sSQL .= " LIMIT $this->iLimit";
1603 if (CONST_Debug) var_dump($sSQL);
1604 $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
1607 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') { // & in
1608 $sClassTable = 'place_classtype_'.$aSearch['sClass'].'_'.$aSearch['sType'];
1609 $sSQL = "SELECT count(*) FROM pg_tables ";
1610 $sSQL .= "WHERE tablename = '$sClassTable'";
1611 $bCacheTable = chksql($this->oDB->getOne($sSQL));
1613 $sSQL = "SELECT min(rank_search) FROM placex WHERE place_id in ($sPlaceIDs)";
1615 if (CONST_Debug) var_dump($sSQL);
1616 $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
1618 // For state / country level searches the normal radius search doesn't work very well
1619 $sPlaceGeom = false;
1620 if ($this->iMaxRank < 9 && $bCacheTable) {
1621 // Try and get a polygon to search in instead
1622 $sSQL = "SELECT geometry ";
1623 $sSQL .= " FROM placex";
1624 $sSQL .= " WHERE place_id in ($sPlaceIDs)";
1625 $sSQL .= " AND rank_search < $this->iMaxRank + 5";
1626 $sSQL .= " AND ST_Geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon')";
1627 $sSQL .= " ORDER BY rank_search ASC ";
1628 $sSQL .= " LIMIT 1";
1629 if (CONST_Debug) var_dump($sSQL);
1630 $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
1636 $this->iMaxRank += 5;
1637 $sSQL = "SELECT place_id FROM placex WHERE place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1638 if (CONST_Debug) var_dump($sSQL);
1639 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
1640 $sPlaceIDs = join(',', $aPlaceIDs);
1643 if ($sPlaceIDs || $sPlaceGeom) {
1646 // More efficient - can make the range bigger
1651 $sOrderBySQL = $oNearPoint->distanceSQL('l.centroid');
1652 } elseif ($sPlaceIDs) {
1653 $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1654 } elseif ($sPlaceGeom) {
1655 $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1658 $sSQL = "select distinct i.place_id".($sOrderBySQL?', i.order_term':'')." from (";
1659 $sSQL .= "select l.place_id".($sOrderBySQL?','.$sOrderBySQL.' as order_term':'')." from ".$sClassTable." as l";
1660 if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1662 $sSQL .= ",placex as f where ";
1663 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1667 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1669 if (sizeof($this->aExcludePlaceIDs)) {
1670 $sSQL .= " and l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1672 if ($sCountryCodesSQL) $sSQL .= " and lp.country_code in ($sCountryCodesSQL)";
1673 $sSQL .= 'limit 300) i ';
1674 if ($sOrderBySQL) $sSQL .= "order by order_term asc";
1675 if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1676 $sSQL .= " limit $this->iLimit";
1677 if (CONST_Debug) var_dump($sSQL);
1678 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1680 if ($aSearch['oNear']) {
1681 $fRange = $aSearch['oNear']->radius();
1686 $sOrderBySQL = $oNearPoint->distanceSQL('l.geometry');
1688 $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1691 $sSQL = "SELECT distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'');
1692 $sSQL .= " FROM placex as l, placex as f ";
1693 $sSQL .= " WHERE f.place_id in ($sPlaceIDs) ";
1694 $sSQL .= " AND ST_DWithin(l.geometry, f.centroid, $fRange) ";
1695 $sSQL .= " AND l.class='".$aSearch['sClass']."' ";
1696 $sSQL .= " AND l.type='".$aSearch['sType']."' ";
1697 if (sizeof($this->aExcludePlaceIDs)) {
1698 $sSQL .= " AND l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
1700 if ($sCountryCodesSQL) $sSQL .= " AND l.country_code in ($sCountryCodesSQL)";
1701 if ($sOrderBySQL) $sSQL .= "ORDER BY ".$sOrderBySQL." ASC";
1702 if ($this->iOffset) $sSQL .= " OFFSET $this->iOffset";
1703 $sSQL .= " limit $this->iLimit";
1704 if (CONST_Debug) var_dump($sSQL);
1705 $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
1709 $aPlaceIDs = $aClassPlaceIDs;
1714 echo "<br><b>Place IDs:</b> ";
1715 var_Dump($aPlaceIDs);
1718 if (sizeof($aPlaceIDs) && $aSearch['sPostcode']) {
1719 $sSQL = 'SELECT place_id FROM placex';
1720 $sSQL .= ' WHERE place_id in ('.join(',', $aPlaceIDs).')';
1721 $sSQL .= " AND postcode = '".pg_escape_string($aSearch['sPostcode'])."'";
1722 if (CONST_Debug) var_dump($sSQL);
1723 $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1724 if ($aFilteredPlaceIDs) {
1725 $aPlaceIDs = $aFilteredPlaceIDs;
1727 echo "<br><b>Place IDs after postcode filtering:</b> ";
1728 var_Dump($aPlaceIDs);
1733 foreach ($aPlaceIDs as $iPlaceID) {
1734 // array for placeID => -1 | Tiger housenumber
1735 $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;
1737 if ($iQueryLoop > 20) break;
1740 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
1741 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1742 // reduces the number of place ids, like a filter
1743 // rank_address is 30 for interpolated housenumbers
1744 $sWherePlaceId = 'WHERE place_id in (';
1745 $sWherePlaceId .= join(',', array_keys($aResultPlaceIDs)).') ';
1747 $sSQL = "SELECT place_id ";
1748 $sSQL .= "FROM placex ".$sWherePlaceId;
1750 $sSQL .= " placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1751 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) {
1752 $sSQL .= " OR (extratags->'place') = 'city'";
1754 if ($this->aAddressRankList) {
1755 $sSQL .= " OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
1757 $sSQL .= " ) UNION ";
1758 $sSQL .= " SELECT place_id FROM location_postcode lp ".$sWherePlaceId;
1759 $sSQL .= " AND (lp.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1760 if ($this->aAddressRankList) {
1761 $sSQL .= " OR lp.rank_address in (".join(',', $this->aAddressRankList).")";
1764 if (CONST_Use_US_Tiger_Data && $this->iMaxAddressRank == 30) {
1766 $sSQL .= " SELECT place_id ";
1767 $sSQL .= " FROM location_property_tiger ".$sWherePlaceId;
1769 if ($this->iMaxAddressRank == 30) {
1771 $sSQL .= " SELECT place_id ";
1772 $sSQL .= " FROM location_property_osmline ".$sWherePlaceId;
1774 if (CONST_Debug) var_dump($sSQL);
1775 $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
1777 foreach ($aFilteredPlaceIDs as $placeID) {
1778 $tempIDs[$placeID] = $aResultPlaceIDs[$placeID]; //assign housenumber to placeID
1780 $aResultPlaceIDs = $tempIDs;
1784 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1785 if ($iGroupLoop > 4) break;
1786 if ($iQueryLoop > 30) break;
1789 // Did we find anything?
1790 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) {
1791 $aSearchResults = $this->getDetails($aResultPlaceIDs);
1794 // Just interpret as a reverse geocode
1795 $oReverse = new ReverseGeocode($this->oDB);
1796 $oReverse->setZoom(18);
1798 $aLookup = $oReverse->lookup(
1804 if (CONST_Debug) var_dump("Reverse search", $aLookup);
1806 if ($aLookup['place_id']) {
1807 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
1808 $aResultPlaceIDs[$aLookup['place_id']] = -1;
1810 $aSearchResults = array();
1815 if (!sizeof($aSearchResults)) {
1816 if ($this->bFallback) {
1817 if ($this->fallbackStructuredQuery()) {
1818 return $this->lookup();
1825 $aClassType = getClassTypesWithImportance();
1826 $aRecheckWords = preg_split('/\b[\s,\\-]*/u', $sQuery);
1827 foreach ($aRecheckWords as $i => $sWord) {
1828 if (!preg_match('/[\pL\pN]/', $sWord)) unset($aRecheckWords[$i]);
1832 echo '<i>Recheck words:<\i>';
1833 var_dump($aRecheckWords);
1836 $oPlaceLookup = new PlaceLookup($this->oDB);
1837 $oPlaceLookup->setIncludePolygonAsPoints($this->bIncludePolygonAsPoints);
1838 $oPlaceLookup->setIncludePolygonAsText($this->bIncludePolygonAsText);
1839 $oPlaceLookup->setIncludePolygonAsGeoJSON($this->bIncludePolygonAsGeoJSON);
1840 $oPlaceLookup->setIncludePolygonAsKML($this->bIncludePolygonAsKML);
1841 $oPlaceLookup->setIncludePolygonAsSVG($this->bIncludePolygonAsSVG);
1842 $oPlaceLookup->setPolygonSimplificationThreshold($this->fPolygonSimplificationThreshold);
1844 foreach ($aSearchResults as $iResNum => $aResult) {
1846 $fDiameter = getResultDiameter($aResult);
1848 $aOutlineResult = $oPlaceLookup->getOutlines($aResult['place_id'], $aResult['lon'], $aResult['lat'], $fDiameter/2);
1849 if ($aOutlineResult) {
1850 $aResult = array_merge($aResult, $aOutlineResult);
1853 if ($aResult['extra_place'] == 'city') {
1854 $aResult['class'] = 'place';
1855 $aResult['type'] = 'city';
1856 $aResult['rank_search'] = 16;
1859 // Is there an icon set for this type of result?
1860 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1861 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon']
1863 $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1866 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1867 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label']
1869 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1870 } elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1871 && $aClassType[$aResult['class'].':'.$aResult['type']]['label']
1873 $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1875 // if tag '&addressdetails=1' is set in query
1876 if ($this->bIncludeAddressDetails) {
1877 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
1878 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
1879 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city'])) {
1880 $aResult['address'] = array_merge(array('city' => array_values($aResult['address'])[0]), $aResult['address']);
1884 if ($this->bIncludeExtraTags) {
1885 if ($aResult['extra']) {
1886 $aResult['sExtraTags'] = json_decode($aResult['extra']);
1888 $aResult['sExtraTags'] = (object) array();
1892 if ($this->bIncludeNameDetails) {
1893 if ($aResult['names']) {
1894 $aResult['sNameDetails'] = json_decode($aResult['names']);
1896 $aResult['sNameDetails'] = (object) array();
1900 // Adjust importance for the number of exact string matches in the result
1901 $aResult['importance'] = max(0.001, $aResult['importance']);
1903 $sAddress = $aResult['langaddress'];
1904 foreach ($aRecheckWords as $i => $sWord) {
1905 if (stripos($sAddress, $sWord)!==false) {
1907 if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1911 $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
1913 $aResult['name'] = $aResult['langaddress'];
1914 // secondary ordering (for results with same importance (the smaller the better):
1915 // - approximate importance of address parts
1916 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1917 // - number of exact matches from the query
1918 if (isset($this->exactMatchCache[$aResult['place_id']])) {
1919 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1920 } elseif (isset($this->exactMatchCache[$aResult['parent_place_id']])) {
1921 $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1923 // - importance of the class/type
1924 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1925 && $aClassType[$aResult['class'].':'.$aResult['type']]['importance']
1927 $aResult['foundorder'] += 0.0001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1929 $aResult['foundorder'] += 0.01;
1931 if (CONST_Debug) var_dump($aResult);
1932 $aSearchResults[$iResNum] = $aResult;
1934 uasort($aSearchResults, 'byImportance');
1936 $aOSMIDDone = array();
1937 $aClassTypeNameDone = array();
1938 $aToFilter = $aSearchResults;
1939 $aSearchResults = array();
1942 foreach ($aToFilter as $iResNum => $aResult) {
1943 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1945 $fLat = $aResult['lat'];
1946 $fLon = $aResult['lon'];
1947 if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1950 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1951 && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']]))
1953 $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1954 $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1955 $aSearchResults[] = $aResult;
1958 // Absolute limit on number of results
1959 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1962 return $aSearchResults;