]> git.openstreetmap.org Git - nominatim.git/blob - lib/SearchContext.php
a6b63586564184c3d89eefb4141928efaf36dd12
[nominatim.git] / lib / SearchContext.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/lib.php');
6
7
8 /**
9  * Collects search constraints that are independent of the
10  * actual interpretation of the search query.
11  *
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.
15  */
16 class SearchContext
17 {
18     private $fNearRadius = false;
19
20     // cached SQL
21
22     public $sqlNear = '';
23
24     public function hasNearPoint()
25     {
26         return $this->fNearRadius !== false;
27     }
28
29     public function nearRadius()
30     {
31         return $this->fNearRadius;
32     }
33
34     public function setNearPoint($fLat, $fLon, $fRadius = 0.1)
35     {
36         $this->fNearRadius = $fRadius;
37         $this->sqlNear = 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)';
38     }
39
40     /**
41      * Extract a coordinate point from a query string.
42      *
43      * @param string $sQuery Query to scan.
44      *
45      * @return The remaining query string.
46      */
47     public function setNearPointFromQuery($sQuery)
48     {
49         $aResult = parseLatLon($sQuery);
50
51         if ($aResult !== false
52             && $aResult[1] <= 90.1
53             && $aResult[1] >= -90.1
54             && $aResult[2] <= 180.1
55             && $aResult[2] >= -180.1
56         ) {
57             $this->setNearPoint($aResult[1], $aResult[2]);
58             $sQuery = trim(str_replace($aResult[0], ' ', $sQuery));
59         }
60
61         return $sQuery;
62     }
63
64     public function distanceSQL($sObj)
65     {
66         return 'ST_Distance('.$this->sqlNear.", $sObj)";
67     }
68
69     public function withinSQL($sObj)
70     {
71         return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sqlNear, $this->fNearRadius);
72     }
73 }