-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbox.go
190 lines (159 loc) · 4.62 KB
/
bbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This package is an implementation of the geospatial algorithms located here:
// https://web.archive.org/web/20180508002202/http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#UsingIndex
// Bronshtein, Semendyayev, Musiol, Mühlig: Handbook of Mathematics. Springer, Berlin.
//
// This port uses Earth's equatorial radius for compatibility with Google Maps,
// Bing Maps, and Mapbox.
// Ported by Justin Lowery (ISC License)
package bbox
import "math"
func toRadians(deg float64) float64 { return deg * math.Pi / 180 }
func toDegrees(rad float64) float64 { return rad * 180 / math.Pi }
func normalizeMeridian(lon float64) float64 {
return math.Mod(lon+3*math.Pi, 2*math.Pi) - math.Pi
}
func calcAngularRadius(radius float64) float64 {
const equatorialRadius = 6378137
return 1000 * radius / equatorialRadius
}
// Point represents a physical point on Earth, referenced by latitude and longitude coordinates.
type Point struct {
Latitude,
Longitude float64
}
func (point Point) toRadians() Point {
point.Latitude = toRadians(point.Latitude)
point.Longitude = toRadians(point.Longitude)
return point
}
func (point Point) toDegrees() Point {
point.Latitude = toDegrees(point.Latitude)
point.Longitude = toDegrees(point.Longitude)
return point
}
func (point Point) normalizeMeridian() Point {
point.Longitude = normalizeMeridian(point.Longitude)
return point
}
func (point *Point) calcLatT(angularRadius float64) float64 {
return math.Asin(math.Sin(point.Latitude) / math.Cos(angularRadius))
}
func (point *Point) calcDeltaLon(angularRadius, latT float64) float64 {
return math.Acos((math.Cos(angularRadius) - math.Sin(latT)*math.Sin(point.Latitude)) / (math.Cos(latT) * math.Cos(point.Latitude)))
}
// BBox represents minimum and maximum bounding box point coordinates.
type BBox struct {
Min,
Max Point
}
func (bbox BBox) toDegrees() BBox {
bbox.Min = bbox.Min.toDegrees()
bbox.Max = bbox.Max.toDegrees()
return bbox
}
func (bbox BBox) normalizeMeridian() BBox {
bbox.Min = bbox.Min.normalizeMeridian()
bbox.Max = bbox.Max.normalizeMeridian()
return bbox
}
func (bbox BBox) applyAngularRadius(angularRadius float64, point Point) BBox {
bbox.Min.Latitude = point.Latitude - angularRadius
bbox.Max.Latitude = point.Latitude + angularRadius
return bbox
}
func (bbox BBox) applyDeltaLon(deltaLon float64, point Point) BBox {
bbox.Min.Longitude = point.Longitude - deltaLon
bbox.Max.Longitude = point.Longitude + deltaLon
return bbox
}
func (bbox BBox) handleNorthPole() BBox {
if bbox.Max.Latitude > math.Pi/2 {
bbox.Min.Longitude = -math.Pi
bbox.Max.Latitude = math.Pi / 2
bbox.Max.Longitude = math.Pi
}
return bbox
}
func (bbox BBox) handleSouthPole() BBox {
if bbox.Min.Latitude < -math.Pi/2 {
bbox.Min.Latitude = -math.Pi / 2
bbox.Min.Longitude = -math.Pi
bbox.Max.Longitude = math.Pi
}
return bbox
}
func (bbox BBox) handleMeridian180() []BBox {
// Handle wraparound if minimum longitude is less than -180 degrees.
if bbox.Min.Longitude < -math.Pi {
return []BBox{
{
Min: Point{
Latitude: bbox.Min.Latitude,
Longitude: bbox.Min.Longitude + 2*math.Pi,
},
Max: Point{
Latitude: bbox.Max.Latitude,
Longitude: math.Pi,
},
},
{
Min: Point{
Latitude: bbox.Min.Latitude,
Longitude: -math.Pi,
},
Max: Point{
Latitude: bbox.Max.Latitude,
Longitude: bbox.Max.Longitude,
},
},
}
}
// Handle wraparound if maximum longitude is greater than 180 degrees.
if bbox.Max.Longitude > math.Pi {
return []BBox{
{
Min: Point{
Latitude: bbox.Min.Latitude,
Longitude: bbox.Min.Longitude,
},
Max: Point{
Latitude: bbox.Max.Latitude,
Longitude: math.Pi,
},
},
{
Min: Point{
Latitude: bbox.Min.Latitude,
Longitude: -math.Pi,
},
Max: Point{
Latitude: bbox.Max.Latitude,
Longitude: bbox.Max.Longitude - 2*math.Pi,
},
},
}
}
return []BBox{bbox}
}
func normalizeBBoxes(bboxes []BBox) []BBox {
for i, bbox := range bboxes {
bboxes[i] = bbox.normalizeMeridian()
bboxes[i] = bbox.toDegrees()
}
return bboxes
}
// New calculates and returns one or more bounding boxes using a coordinate point
// and radius in kilometers.
func New(radius float64, pointVal Point) []BBox {
angularRadius := calcAngularRadius(radius)
point := pointVal.toRadians()
latT := point.calcLatT(angularRadius)
deltaLon := point.calcDeltaLon(angularRadius, latT)
bbox := BBox{}
bbox = bbox.applyAngularRadius(angularRadius, point)
bbox = bbox.applyDeltaLon(deltaLon, point)
bbox = bbox.handleNorthPole()
bbox = bbox.handleSouthPole()
bboxes := bbox.handleMeridian180()
return normalizeBBoxes(bboxes)
}