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