]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/SearchContext.php
coding style and some documentation
[nominatim.git] / lib / SearchContext.php
index a6b63586564184c3d89eefb4141928efaf36dd12..9bab8658ac8a9b24a86cfc85cef81d83c3cd0f8b 100644 (file)
@@ -6,7 +6,7 @@ require_once(CONST_BasePath.'/lib/lib.php');
 
 
 /**
- * Collects search constraints that are independent of the
+ * Collection of search constraints that are independent of the
  * actual interpretation of the search query.
  *
  * The search context is shared between all SearchDescriptions. This
@@ -15,11 +15,24 @@ require_once(CONST_BasePath.'/lib/lib.php');
  */
 class SearchContext
 {
+    /// Search radius around a given Near reference point.
     private $fNearRadius = false;
+    /// True if search must be restricted to viewbox only.
+    public $bViewboxBounded = false;
 
-    // cached SQL
-
+    /// Reference point for search (as SQL).
     public $sqlNear = '';
+    /// Viewbox selected for search (as SQL).
+    public $sqlViewboxSmall = '';
+    /// Viewbox with a larger buffer around (as SQL).
+    public $sqlViewboxLarge = '';
+    /// Reference along a route (as SQL).
+    public $sqlViewboxCentre = '';
+    /// List of countries to restrict search to (as SQL).
+    public $sqlCountryList = '';
+    /// List of place IDs to exclude (as SQL).
+    private $sqlExcludeList = '';
+
 
     public function hasNearPoint()
     {
@@ -37,6 +50,67 @@ class SearchContext
         $this->sqlNear = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
     }
 
+    public function isBoundedSearch()
+    {
+        return $this->hasNearPoint() || ($this->sqlViewboxSmall && $this->bViewboxBounded);
+    }
+
+    public function setViewboxFromBox(&$aViewBox, $bBounded)
+    {
+        $this->bViewboxBounded = $bBounded;
+        $this->sqlViewboxCentre = '';
+
+        $this->sqlViewboxSmall = sprintf(
+            'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
+            $aViewBox[0],
+            $aViewBox[1],
+            $aViewBox[2],
+            $aViewBox[3]
+        );
+
+        $fHeight = $aViewBox[0] - $aViewBox[2];
+        $fWidth = $aViewBox[1] - $aViewBox[3];
+
+        $this->sqlViewboxLarge = sprintf(
+            'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
+            max($aViewBox[0], $aViewBox[2]) + $fHeight,
+            max($aViewBox[1], $aViewBox[3]) + $fWidth,
+            min($aViewBox[0], $aViewBox[2]) - $fHeight,
+            min($aViewBox[1], $aViewBox[3]) - $fWidth
+        );
+    }
+
+    public function setViewboxFromRoute(&$oDB, $aRoutePoints, $fRouteWidth, $bBounded)
+    {
+        $this->bViewboxBounded = $bBounded;
+        $this->sqlViewboxCentre = "ST_SetSRID('LINESTRING(";
+        $sSep = '';
+        foreach ($aRoutePoints as $aPoint) {
+            $fPoint = (float)$aPoint;
+            $this->sqlViewboxCentre .= $sSep.$fPoint;
+            $sSep = ($sSep == ' ') ? ',' : ' ';
+        }
+        $this->sqlViewboxCentre .= ")'::geometry,4326)";
+
+        $sSQL = 'ST_BUFFER('.$this->sqlViewboxCentre.','.($fRouteWidth/69).')';
+        $sGeom = chksql($oDB->getOne("select ".$sSQL), "Could not get small viewbox");
+        $this->sqlViewboxSmall = "'".$sGeom."'::geometry";
+
+        $sSQL = 'ST_BUFFER('.$this->sqlViewboxCentre.','.($fRouteWidth/30).')';
+        $sGeom = chksql($oDB->getOne("select ".$sSQL), "Could not get large viewbox");
+        $this->sqlViewboxLarge = "'".$sGeom."'::geometry";
+    }
+
+    public function setExcludeList($aExcluded)
+    {
+        $this->sqlExcludeList = ' not in ('.join(',', $aExcluded).')';
+    }
+
+    public function setCountryList($aCountries)
+    {
+        $this->sqlCountryList = '('.join(',', array_map('addQuotes', $aCountries)).')';
+    }
+
     /**
      * Extract a coordinate point from a query string.
      *
@@ -70,4 +144,27 @@ class SearchContext
     {
         return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sqlNear, $this->fNearRadius);
     }
+
+    public function viewboxImportanceSQL($sObj)
+    {
+        $sSQL = '';
+
+        if ($this->sqlViewboxSmall) {
+            $sSQL = " * CASE WHEN ST_Contains($this->sqlViewboxSmall, $sObj) THEN 1 ELSE 0.5 END";
+        }
+        if ($this->sqlViewboxLarge) {
+            $sSQL = " * CASE WHEN ST_Contains($this->sqlViewboxLarge, $sObj) THEN 1 ELSE 0.5 END";
+        }
+
+        return $sSQL;
+    }
+
+    public function excludeSQL($sVariable)
+    {
+        if ($this->sqlExcludeList) {
+            return $sVariable.$this->sqlExcludeList;
+        }
+
+        return '';
+    }
 }