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