]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/PlaceLookup.php
use PlaceLookup::loadParamArray in search and lookup
[nominatim.git] / lib / PlaceLookup.php
index 5fb88062473ba5176a8952c2fe6cbad766860c51..a48f5597e66c2218cd1d79891e382b281a20756d 100644 (file)
@@ -32,75 +32,104 @@ class PlaceLookup
         $this->oDB =& $oDB;
     }
 
-    public function setAnchorSql($sPoint)
+    public function doDeDupe()
     {
-        $this->sAnchorSql = $sPoint;
+        return $this->bDeDupe;
     }
 
-    public function setDeDupe($bDeDupe)
+    public function setIncludePolygonAsPoints($b = true)
     {
-        $this->bDeDupe = $bDeDupe;
+        $this->bIncludePolygonAsPoints = $b;
     }
 
-    public function setAddressRankList($aList)
+    public function loadParamArray($oParams, $sGeomType = null)
     {
-        $this->sAddressRankListSql = '('.join(',', $aList).')';
-    }
+        $aLangs = $oParams->getPreferredLanguages();
+        $this->aLangPrefOrderSql =
+            'ARRAY['.join(',', array_map('getDBQuoted', $aLangs)).']';
 
-    public function setAllowedTypesSQLList($sSql)
-    {
-        $this->sAllowedTypesSQLList = $sSql;
-    }
+        $this->bAddressDetails = $oParams->getBool('addressdetails', true);
+        $this->bExtraTags = $oParams->getBool('extratags', false);
+        $this->bNameDetails = $oParams->getBool('namedetails', false);
 
-    public function setLanguagePreference($aLangPrefOrder)
-    {
-        $this->aLangPrefOrderSql =
-            'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
-    }
+        $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
 
-    public function setIncludeAddressDetails($bAddressDetails = true)
-    {
-        $this->bAddressDetails = $bAddressDetails;
+        if ($sGeomType === null || $sGeomType == 'text') {
+            $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
+        }
+        if ($sGeomType === null || $sGeomType == 'geojson') {
+            $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
+        }
+        if ($sGeomType === null || $sGeomType == 'kml') {
+            $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
+        }
+        if ($sGeomType === null || $sGeomType == 'svg') {
+            $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
+        }
+        $this->fPolygonSimplificationThreshold
+            = $oParams->getFloat('polygon_threshold', 0.0);
+
+        $iWantedTypes =
+            ($this->bIncludePolygonAsText ? 1 : 0) +
+            ($this->bIncludePolygonAsGeoJSON ? 1 : 0) +
+            ($this->bIncludePolygonAsKML ? 1 : 0) +
+            ($this->bIncludePolygonAsSVG ? 1 : 0);
+        if ($iWantedTypes > CONST_PolygonOutput_MaximumTypes) {
+            if (CONST_PolygonOutput_MaximumTypes) {
+                userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
+            } else {
+                userError("Polygon output is disabled");
+            }
+        }
     }
 
-    public function setIncludeExtraTags($bExtraTags = false)
+    public function getMoreUrlParams()
     {
-        $this->bExtraTags = $bExtraTags;
-    }
+        $aParams = array();
 
-    public function setIncludeNameDetails($bNameDetails = false)
-    {
-        $this->bNameDetails = $bNameDetails;
-    }
+        if ($this->bAddressDetails) $aParams['addressdetails'] = '1';
+        if ($this->bExtraTags) $aParams['extratags'] = '1';
+        if ($this->bNameDetails) $aParams['namedetails'] = '1';
 
-    public function setIncludePolygonAsPoints($b = true)
-    {
-        $this->bIncludePolygonAsPoints = $b;
+        if ($this->bIncludePolygonAsPoints) $aParams['polygon'] = '1';
+        if ($this->bIncludePolygonAsText) $aParams['polygon_text'] = '1';
+        if ($this->bIncludePolygonAsGeoJSON) $aParams['polygon_geojson'] = '1';
+        if ($this->bIncludePolygonAsKML) $aParams['polygon_kml'] = '1';
+        if ($this->bIncludePolygonAsSVG) $aParams['polygon_svg'] = '1';
+
+        if ($this->fPolygonSimplificationThreshold > 0.0) {
+            $aParams['polygon_threshold'] = $this->fPolygonSimplificationThreshold;
+        }
+
+        if (!$this->bDeDupe) $aParams['dedupe'] = '0';
+
+        return $aParams;
     }
 
-    public function setIncludePolygonAsText($b = true)
+    public function setAnchorSql($sPoint)
     {
-        $this->bIncludePolygonAsText = $b;
+        $this->sAnchorSql = $sPoint;
     }
 
-    public function setIncludePolygonAsGeoJSON($b = true)
+    public function setAddressRankList($aList)
     {
-        $this->bIncludePolygonAsGeoJSON = $b;
+        $this->sAddressRankListSql = '('.join(',', $aList).')';
     }
 
-    public function setIncludePolygonAsKML($b = true)
+    public function setAllowedTypesSQLList($sSql)
     {
-        $this->bIncludePolygonAsKML = $b;
+        $this->sAllowedTypesSQLList = $sSql;
     }
 
-    public function setIncludePolygonAsSVG($b = true)
+    public function setLanguagePreference($aLangPrefOrder)
     {
-        $this->bIncludePolygonAsSVG = $b;
+        $this->aLangPrefOrderSql =
+            'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
     }
 
-    public function setPolygonSimplificationThreshold($f)
+    public function setIncludeAddressDetails($bAddressDetails = true)
     {
-        $this->fPolygonSimplificationThreshold = $f;
+        $this->bAddressDetails = $bAddressDetails;
     }
 
     private function addressImportanceSql($sGeometry, $sPlaceId)
@@ -133,17 +162,16 @@ class PlaceLookup
             return null;
         }
 
-        return $this->lookup(new Result($iPlaceID));
+        $aResults = $this->lookup(array($iPlaceID => new Result($iPlaceID)));
+
+        return sizeof($aResults) ? reset($aResults) : null;
     }
 
-    public function lookup($oResult, $iMinRank = 0, $iMaxRank = 30)
+    public function lookup($aResults, $iMinRank = 0, $iMaxRank = 30)
     {
-        if ($oResult === null) {
-            return null;
+        if (!sizeof($aResults)) {
+            return array();
         }
-
-        $aResults = array($oResult->iId => $oResult);
-
         $aSubSelects = array();
 
         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
@@ -174,7 +202,7 @@ class PlaceLookup
             $sSQL .= '    avg(ST_Y(centroid)) AS lat, ';
             $sSQL .= '    COALESCE(importance,0.75-(rank_search::float/40)) AS importance, ';
             $sSQL .= $this->addressImportanceSql(
-                'centroid',
+                'ST_Collect(centroid)',
                 'min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END)'
             );
             $sSQL .= "    (extratags->'place') AS extra_place ";
@@ -381,7 +409,7 @@ class PlaceLookup
         if (CONST_Debug) var_dump($aSubSelects);
 
         if (!sizeof($aSubSelects)) {
-            return null;
+            return array();
         }
 
         $aPlaces = chksql(
@@ -389,12 +417,7 @@ class PlaceLookup
             "Could not lookup place"
         );
 
-        if (!sizeof($aPlaces)) {
-            return null;
-        }
-
-        if (CONST_Debug) var_dump($aPlaces);
-
+        $aClassType = getClassTypes();
         foreach ($aPlaces as &$aPlace) {
             if ($this->bAddressDetails) {
                 // to get addressdetails for tiger data, the housenumber is needed
@@ -420,7 +443,6 @@ class PlaceLookup
                 }
             }
 
-            $aClassType = getClassTypes();
             $sAddressType = '';
             $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
             if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel'])) {
@@ -437,7 +459,7 @@ class PlaceLookup
 
         if (CONST_Debug) var_dump($aPlaces);
 
-        return reset($aPlaces);
+        return $aPlaces;
     }
 
     private function getAddressDetails($iPlaceID, $bAll, $sHousenumber)