5 require_once(CONST_BasePath.'/lib/lib.php');
9 * Collects search constraints that are independent of the
10 * actual interpretation of the search query.
12 * The search context is shared between all SearchDescriptions. This
13 * object mainly serves as context provider for the database queries.
14 * Therefore most data is directly cached as SQL statements.
18 private $fNearRadius = false;
19 public $bViewboxBounded = false;
22 public $sqlViewboxSmall = '';
23 public $sqlViewboxLarge = '';
24 public $sqlViewboxCentre = '';
26 public function hasNearPoint()
28 return $this->fNearRadius !== false;
31 public function nearRadius()
33 return $this->fNearRadius;
36 public function setNearPoint($fLat, $fLon, $fRadius = 0.1)
38 $this->fNearRadius = $fRadius;
39 $this->sqlNear = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
42 public function isBoundedSearch()
44 return $this->hasNearPoint() || ($this->sqlViewboxSmall && $this->bViewboxBounded);
48 public function setViewboxFromBox(&$aViewBox, $bBounded)
50 $this->bViewboxBounded = $bBounded;
51 $this->sqlViewboxCentre = '';
53 $this->sqlViewboxSmall = sprintf(
54 'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
61 $fHeight = $aViewBox[0] - $aViewBox[2];
62 $fWidth = $aViewBox[1] - $aViewBox[3];
64 $this->sqlViewboxLarge = sprintf(
65 'ST_SetSRID(ST_MakeBox2D(ST_Point(%F,%F),ST_Point(%F,%F)),4326)',
66 max($aViewBox[0], $aViewBox[2]) + $fHeight,
67 max($aViewBox[1], $aViewBox[3]) + $fWidth,
68 min($aViewBox[0], $aViewBox[2]) - $fHeight,
69 min($aViewBox[1], $aViewBox[3]) - $fWidth
73 public function setViewboxFromRoute(&$oDB, $aRoutePoints, $fRouteWidth, $bBounded)
75 $this->bViewboxBounded = $bBounded;
76 $this->sqlViewboxCentre = "ST_SetSRID('LINESTRING(";
78 foreach ($aRoutePoints as $aPoint) {
79 $fPoint = (float)$aPoint;
80 $this->sqlViewboxCentre .= $sSep.$fPoint;
81 $sSep = ($sSep == ' ') ? ',' : ' ';
83 $this->sqlViewboxCentre .= ")'::geometry,4326)";
85 $sSQL = 'ST_BUFFER('.$this->sqlViewboxCentre.','.($fRouteWidth/69).')';
86 $sGeom = chksql($oDB->getOne("select ".$sSQL), "Could not get small viewbox");
87 $this->sqlViewboxSmall = "'".$sGeom."'::geometry";
89 $sSQL = 'ST_BUFFER('.$this->sqlViewboxCentre.','.($fRouteWidth/30).')';
90 $sGeom = chksql($oDB->getOne("select ".$sSQL), "Could not get large viewbox");
91 $this->sqlViewboxLarge = "'".$sGeom."'::geometry";
95 * Extract a coordinate point from a query string.
97 * @param string $sQuery Query to scan.
99 * @return The remaining query string.
101 public function setNearPointFromQuery($sQuery)
103 $aResult = parseLatLon($sQuery);
105 if ($aResult !== false
106 && $aResult[1] <= 90.1
107 && $aResult[1] >= -90.1
108 && $aResult[2] <= 180.1
109 && $aResult[2] >= -180.1
111 $this->setNearPoint($aResult[1], $aResult[2]);
112 $sQuery = trim(str_replace($aResult[0], ' ', $sQuery));
118 public function distanceSQL($sObj)
120 return 'ST_Distance('.$this->sqlNear.", $sObj)";
123 public function withinSQL($sObj)
125 return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sqlNear, $this->fNearRadius);
128 public function viewboxImportanceSQL($sObj)
132 if ($this->sqlViewboxSmall) {
133 $sSQL = " * CASE WHEN ST_Contains($this->sqlViewboxSmall, $sObj) THEN 1 ELSE 0.5 END";
135 if ($this->sqlViewboxLarge) {
136 $sSQL = " * CASE WHEN ST_Contains($this->sqlViewboxLarge, $sObj) THEN 1 ELSE 0.5 END";