forked from arne-cl/tile38
/
bbox.go
260 lines (229 loc) 路 6.06 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package geojson
import (
"math"
"strconv"
"github.com/quesurifn/tile38/pkg/geojson/poly"
"github.com/tidwall/gjson"
)
// BBox is a bounding box
type BBox struct {
Min Position
Max Position
}
// New2DBBox creates a new bounding box
func New2DBBox(minX, minY, maxX, maxY float64) BBox {
return BBox{Min: Position{X: minX, Y: minY, Z: 0}, Max: Position{X: maxX, Y: maxY, Z: 0}}
}
func fillBBox(json string) (*BBox, error) {
var bbox *BBox
res := gjson.Get(json, "bbox")
switch res.Type {
default:
return nil, errBBoxInvalidType
case gjson.Null:
case gjson.JSON:
v := res.Array()
if !(len(v) == 4 || len(v) == 6) {
return nil, errBBoxInvalidNumberOfValues
}
bbox = &BBox{}
for i := 0; i < len(v); i++ {
if v[i].Type != gjson.Number {
return nil, errBBoxInvalidValue
}
}
bbox.Min.X = v[0].Float()
bbox.Min.Y = v[1].Float()
i := 2
if len(v) == 6 {
bbox.Min.Z = v[2].Float()
i = 3
} else {
bbox.Min.Z = nilz
}
bbox.Max.X = v[i+0].Float()
bbox.Max.Y = v[i+1].Float()
if len(v) == 6 {
bbox.Max.Z = v[i+2].Float()
i = 3
} else {
bbox.Max.Z = nilz
}
}
return bbox, nil
}
func (b *BBox) isCordZDefined() bool {
return b != nil && (b.Min.Z != nilz || b.Max.Z != nilz)
}
func appendBBoxJSON(json []byte, b *BBox) []byte {
if b == nil {
return json
}
hasZ := b.Min.Z != nilz && b.Max.Z != nilz
json = append(json, `,"bbox":[`...)
json = strconv.AppendFloat(json, b.Min.X, 'f', -1, 64)
json = append(json, ',')
json = strconv.AppendFloat(json, b.Min.Y, 'f', -1, 64)
if hasZ {
json = append(json, ',')
json = strconv.AppendFloat(json, b.Min.Z, 'f', -1, 64)
}
json = append(json, ',')
json = strconv.AppendFloat(json, b.Max.X, 'f', -1, 64)
json = append(json, ',')
json = strconv.AppendFloat(json, b.Max.Y, 'f', -1, 64)
if hasZ {
json = append(json, ',')
json = strconv.AppendFloat(json, b.Max.Z, 'f', -1, 64)
}
json = append(json, ']')
return json
}
func (b BBox) center() Position {
return Position{
(b.Max.X-b.Min.X)/2 + b.Min.X,
(b.Max.Y-b.Min.Y)/2 + b.Min.Y,
0,
}
}
func (b BBox) union(bbox BBox) BBox {
if bbox.Min.X < b.Min.X {
b.Min.X = bbox.Min.X
}
if bbox.Min.Y < b.Min.Y {
b.Min.Y = bbox.Min.Y
}
if bbox.Max.X > b.Max.X {
b.Max.X = bbox.Max.X
}
if bbox.Max.Y > b.Max.Y {
b.Max.Y = bbox.Max.Y
}
return b
}
func (b BBox) exterior() []Position {
return []Position{
{b.Min.X, b.Min.Y, 0},
{b.Min.X, b.Max.Y, 0},
{b.Max.X, b.Max.Y, 0},
{b.Max.X, b.Min.Y, 0},
{b.Min.X, b.Min.Y, 0},
}
}
func rectBBox(bbox BBox) poly.Rect {
return poly.Rect{
Min: poly.Point{X: bbox.Min.X, Y: bbox.Min.Y, Z: 0},
Max: poly.Point{X: bbox.Max.X, Y: bbox.Max.Y, Z: 0},
}
}
// ExternalJSON is the simple json representation of the bounding box used for external applications.
func (b BBox) ExternalJSON() string {
sw, ne := b.Min, b.Max
sw.Z, ne.Z = 0, 0
return `{"sw":` + sw.ExternalJSON() + `,"ne":` + ne.ExternalJSON() + `}`
}
// Sparse returns back an evenly distributed number of sub bboxs.
func (b BBox) Sparse(amount byte) []BBox {
if amount == 0 {
return []BBox{b}
}
var bboxes []BBox
split := 1 << amount
var xsize, ysize float64
if b.Max.X < b.Min.X {
// crosses the prime meridian
xsize = (b.Min.X - b.Max.X) / float64(split)
} else {
xsize = (b.Max.X - b.Min.X) / float64(split)
}
if b.Max.Y < b.Min.Y {
// crosses the equator
ysize = (b.Min.Y - b.Max.Y) / float64(split)
} else {
ysize = (b.Max.Y - b.Min.Y) / float64(split)
}
for y := b.Min.Y; y < b.Max.Y; y += ysize {
for x := b.Min.X; x < b.Max.X; x += xsize {
bboxes = append(bboxes, BBox{
Min: Position{X: x, Y: y, Z: b.Min.Z},
Max: Position{X: x + xsize, Y: y + ysize, Z: b.Max.Z},
})
}
}
return bboxes
}
// BBoxesFromCenter calculates the bounding box surrounding a circle.
func BBoxesFromCenter(lat, lon, meters float64) (outer BBox) {
outer.Min.Y, outer.Min.X, outer.Max.Y, outer.Max.X = BoundsFromCenter(lat, lon, meters)
if outer.Min.X == outer.Max.X {
switch outer.Min.X {
case -180:
outer.Max.X = 180
case 180:
outer.Min.X = -180
}
}
return outer
}
// BoundsFromCenter calculates the bounding box surrounding a circle.
func BoundsFromCenter(lat, lon, meters float64) (latMin, lonMin, latMax, lonMax float64) {
// see http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#Latitude
lat = toRadians(lat)
lon = toRadians(lon)
r := meters / earthRadius // angular radius
latMin = lat - r
latMax = lat + r
latT := math.Asin(math.Sin(lat) / math.Cos(r))
lon螖 := math.Acos((math.Cos(r) - math.Sin(latT)*math.Sin(lat)) / (math.Cos(latT) * math.Cos(lat)))
lonMin = lon - lon螖
lonMax = lon + lon螖
// Adjust for north poll
if latMax > math.Pi/2 {
lonMin = -math.Pi
latMax = math.Pi / 2
lonMax = math.Pi
}
// Adjust for south poll
if latMin < -math.Pi/2 {
latMin = -math.Pi / 2
lonMin = -math.Pi
lonMax = math.Pi
}
// Adjust for wraparound. Remove this if the commented-out condition below this block is added.
if lonMin < -math.Pi || lonMax > math.Pi {
lonMin = -math.Pi
lonMax = math.Pi
}
/*
// Consider splitting area into two bboxes, using the below checks, and erasing above block for performance. See http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#PolesAnd180thMeridian
// Adjust for wraparound if minimum longitude is less than -180 degrees.
if lonMin < -math.Pi {
// box 1:
latMin = latMin
latMax = latMax
lonMin += 2*math.Pi
lonMax = math.Pi
// box 2:
latMin = latMin
latMax = latMax
lonMin = -math.Pi
lonMax = lonMax
}
// Adjust for wraparound if maximum longitude is greater than 180 degrees.
if lonMax > math.Pi {
// box 1:
latMin = latMin
latMax = latMax
lonMin = lonMin
lonMax = -math.Pi
// box 2:
latMin = latMin
latMax = latMax
lonMin = -math.Pi
lonMax -= 2*math.Pi
}
*/
lonMin = math.Mod(lonMin+3*math.Pi, 2*math.Pi) - math.Pi // normalise to -180..+180掳
lonMax = math.Mod(lonMax+3*math.Pi, 2*math.Pi) - math.Pi
return toDegrees(latMin), toDegrees(lonMin), toDegrees(latMax), toDegrees(lonMax)
}