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