]> git.openstreetmap.org Git - nominatim.git/blob - lib/NearPoint.php
NearPoint::extractFromQuery - greedy-match optional quote sign
[nominatim.git] / lib / NearPoint.php
1 <?php
2
3 namespace Nominatim;
4
5 /**
6  * A geographic point with a search radius.
7  */
8 class NearPoint
9 {
10     private $fLat;
11     private $fLon;
12     private $fRadius;
13
14     private $sSQL;
15
16
17     public function __construct($lat, $lon, $radius = 0.1)
18     {
19         $this->fLat = (float)$lat;
20         $this->fLon = (float)$lon;
21         $this->fRadius = (float)$radius;
22         $this->sSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
23     }
24
25     public function lat()
26     {
27         return $this->fLat;
28     }
29
30     public function lon()
31     {
32         return $this->fLon;
33     }
34
35     public function radius()
36     {
37         return $this->fRadius;
38     }
39
40     public function distanceSQL($sObj)
41     {
42         return 'ST_Distance('.$this->sSQL.", $sObj)";
43     }
44
45     public function withinSQL($sObj)
46     {
47         return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sSQL, $this->fRadius);
48     }
49
50     /**
51      * Check that the coordinates are valid WSG84 coordinates.
52      *
53      * @return bool True if the coordinates are correctly bounded.
54      */
55     public function isValid()
56     {
57         return ($this->fLat <= 90.1
58                 && $this->fLat >= -90.1
59                 && $this->fLon <= 180.1
60                 && $this->fLon >= -180.1);
61     }
62
63     /**
64      * Extract a coordinate point from a query string.
65      *
66      * If a coordinate is found an array of a new NearPoint and the
67      * remaining query is returned or false otherwise.
68      *
69      * @param string $sQuery Query to scan.
70      *
71      * @return array|false If a coordinate was found, an array with
72      *                     `pt` as the NearPoint coordinates and `query`
73      *                      with the remaining query string. False otherwiese.
74      */
75     public static function extractFromQuery($sQuery)
76     {
77         // Do we have anything that looks like a lat/lon pair?
78         // returns array(lat,lon,query_with_lat_lon_removed)
79         // or null
80         $sFound    = null;
81         $fQueryLat = null;
82         $fQueryLon = null;
83
84         if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
85             /*               1         2                   3                    4         5            6
86              * degrees decimal minutes
87              * N 40 26.767, W 79 58.933
88              * N 40°26.767′, W 79°58.933′
89              */
90             $sFound    = $aData[0];
91             $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
92             $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
93         } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
94             /*                     1            2                         3          4            5                      6
95              * degrees decimal minutes
96              * 40 26.767 N, 79 58.933 W
97              * 40° 26.767′ N 79° 58.933′ W
98              */
99             $sFound    = $aData[0];
100             $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
101             $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
102         } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
103             /*                     1        2            3              4                 5        6            7              8
104              * degrees decimal seconds
105              * N 40 26 46 W 79 58 56
106              * N 40° 26′ 46″, W 79° 58′ 56″
107              */
108             $sFound    = $aData[0];
109             $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
110             $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
111         } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\s*/', $sQuery, $aData)) {
112             /*                     1            2              3             4          5            6              7             8
113              * degrees decimal seconds
114              * 40 26 46 N 79 58 56 W
115              * 40° 26′ 46″ N, 79° 58′ 56″ W
116              */
117             $sFound    = $aData[0];
118             $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
119             $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
120         } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
121             /*                     1        2                               3        4
122              * degrees decimal
123              * N 40.446° W 79.982°
124              */
125             $sFound    = $aData[0];
126             $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
127             $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
128         } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
129             /*                     1                           2          3                           4
130              * degrees decimal
131              * 40.446° N 79.982° W
132              */
133             $sFound    = $aData[0];
134             $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
135             $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
136         } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
137             /*                 1                   2                             3                        4
138              * degrees decimal
139              * 12.34, 56.78
140              * 12.34 56.78
141              * [12.456,-78.90]
142              */
143             $sFound    = $aData[0];
144             $fQueryLat = $aData[2];
145             $fQueryLon = $aData[3];
146         } else {
147             return false;
148         }
149
150         $oPt = new NearPoint($fQueryLat, $fQueryLon);
151
152         if (!$oPt->isValid()) return false;
153
154         $sQuery = trim(str_replace($sFound, ' ', $sQuery));
155
156         return array('pt' => $oPt, 'query' => $sQuery);
157     }
158 }