-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathgeo.go
More file actions
256 lines (223 loc) · 7.35 KB
/
Copy pathgeo.go
File metadata and controls
256 lines (223 loc) · 7.35 KB
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
package models
import (
"time"
"github.com/golang/geo/s2"
"github.com/interuss/dss/pkg/geo"
"github.com/interuss/stacktrace"
)
const (
minLat = -90.0
maxLat = 90.0
minLng = -180.0
maxLng = 180.0
)
func float32p(v float32) *float32 {
return &v
}
func timeP(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
// Volume4D is a Contiguous block of geographic spacetime.
type Volume4D struct {
// Constant spatial extent of this volume.
SpatialVolume *Volume3D
// End time of this volume.
EndTime *time.Time
// Beginning time of this volume.
StartTime *time.Time
}
// Volume3D is A three-dimensional geographic volume consisting of a vertically-extruded shape.
type Volume3D struct {
// Maximum bounding altitude (meters above the WGS84 ellipsoid) of this volume.
AltitudeHi *float32
// Minimum bounding altitude (meters above the WGS84 ellipsoid) of this volume.
AltitudeLo *float32
// Projection of this volume onto the earth's surface.
Footprint Geometry
}
// Geometry models a geometry.
type Geometry interface {
// CalculateCovering returns an s2 cell covering for a geometry.
CalculateCovering() (s2.CellUnion, error)
}
// GeometryFunc is an implementation of Geometry
type GeometryFunc func() (s2.CellUnion, error)
type precomputedCellGeometry map[s2.CellID]struct{}
func (pcg precomputedCellGeometry) merge(ids ...s2.CellID) precomputedCellGeometry {
for _, id := range ids {
pcg[id] = struct{}{}
}
return pcg
}
func (pcg precomputedCellGeometry) CalculateCovering() (s2.CellUnion, error) {
var (
result = make(s2.CellUnion, len(pcg))
idx int
)
for id := range pcg {
result[idx] = id
idx++
}
return result, nil
}
// UnionVolumes4D unions volumes and returns a volume that covers all the
// individual volumes in space and time, or one of these root causes:
// * geo.ErrMissingFootprint
// * geo.ErrNotEnoughPointsInPolygon
// * geo.ErrBadCoordSet
// * geo.ErrRadiusMustBeLargerThan0
func UnionVolumes4D(volumes ...*Volume4D) (*Volume4D, error) {
result := &Volume4D{}
unbounded := struct{ startTime, endTime, altitudeLo, altitudeHi bool }{}
for _, volume := range volumes {
if volume.EndTime == nil {
unbounded.endTime = true
result.EndTime = nil
} else if !unbounded.endTime {
if result.EndTime != nil {
if volume.EndTime.After(*result.EndTime) {
*result.EndTime = *volume.EndTime
}
} else {
result.EndTime = timeP(*volume.EndTime)
}
}
if volume.StartTime == nil {
unbounded.startTime = true
result.StartTime = nil
} else if !unbounded.startTime {
if result.StartTime != nil {
if volume.StartTime.Before(*result.StartTime) {
*result.StartTime = *volume.StartTime
}
} else {
result.StartTime = timeP(*volume.StartTime)
}
}
if volume.SpatialVolume != nil {
if result.SpatialVolume == nil {
result.SpatialVolume = &Volume3D{}
}
if volume.SpatialVolume.AltitudeLo == nil {
unbounded.altitudeLo = true
result.SpatialVolume.AltitudeLo = nil
} else if !unbounded.altitudeLo {
if result.SpatialVolume.AltitudeLo != nil {
if *volume.SpatialVolume.AltitudeLo < *result.SpatialVolume.AltitudeLo {
*result.SpatialVolume.AltitudeLo = *volume.SpatialVolume.AltitudeLo
}
} else {
result.SpatialVolume.AltitudeLo = float32p(*volume.SpatialVolume.AltitudeLo)
}
}
if volume.SpatialVolume.AltitudeHi == nil {
unbounded.altitudeHi = true
result.SpatialVolume.AltitudeHi = nil
} else if !unbounded.altitudeHi {
if result.SpatialVolume.AltitudeHi != nil {
if *volume.SpatialVolume.AltitudeHi > *result.SpatialVolume.AltitudeHi {
*result.SpatialVolume.AltitudeHi = *volume.SpatialVolume.AltitudeHi
}
} else {
result.SpatialVolume.AltitudeHi = float32p(*volume.SpatialVolume.AltitudeHi)
}
}
if volume.SpatialVolume.Footprint != nil {
cells, err := volume.SpatialVolume.Footprint.CalculateCovering()
if err != nil {
return nil, stacktrace.Propagate(err, "Error calculating footprint covering")
}
if result.SpatialVolume.Footprint == nil {
result.SpatialVolume.Footprint = precomputedCellGeometry{}
}
result.SpatialVolume.Footprint.(precomputedCellGeometry).merge(cells...)
}
}
}
return result, nil
}
// CalculateSpatialCovering returns the spatial covering of vol4, or one of:
// * geo.ErrMissingSpatialVolume
// * geo.ErrMissingFootprint
// * geo.ErrNotEnoughPointsInPolygon
// * geo.ErrBadCoordSet
// * geo.ErrRadiusMustBeLargerThan0
func (vol4 *Volume4D) CalculateSpatialCovering() (s2.CellUnion, error) {
if vol4.SpatialVolume == nil {
return nil, geo.ErrMissingSpatialVolume
}
return vol4.SpatialVolume.CalculateCovering()
}
// CalculateCovering returns the spatial covering of vol3, or one of:
// * geo.ErrMissingFootprint
// * geo.ErrNotEnoughPointsInPolygon
// * geo.ErrBadCoordSet
// * geo.ErrRadiusMustBeLargerThan0
func (vol3 *Volume3D) CalculateCovering() (s2.CellUnion, error) {
if vol3.Footprint == nil {
return nil, geo.ErrMissingFootprint
}
return vol3.Footprint.CalculateCovering()
}
// CalculateCovering returns the result of invoking gf, with possible errors:
// * geo.ErrNotEnoughPointsInPolygon
// * geo.ErrBadCoordSet
// * geo.ErrRadiusMustBeLargerThan0
func (gf GeometryFunc) CalculateCovering() (s2.CellUnion, error) {
return gf()
}
// GeoCircle models a circular enclosed area on earth's surface.
type GeoCircle struct {
Center LatLngPoint
RadiusMeter float32
}
// CalculateCovering returns the spatial covering of gc.
func (gc *GeoCircle) CalculateCovering() (s2.CellUnion, error) {
if (gc.Center.Lat > maxLat) || (gc.Center.Lat < minLat) || (gc.Center.Lng > maxLng) || (gc.Center.Lng < minLng) {
return nil, geo.ErrBadCoordSet
}
if !(gc.RadiusMeter > 0) {
return nil, geo.ErrRadiusMustBeLargerThan0
}
// TODO: Use an S2 Cap as an inscribed polygon does not fully cover the defined circle
return geo.RegionCoverer.Covering(s2.RegularLoop(
s2.PointFromLatLng(s2.LatLngFromDegrees(gc.Center.Lat, gc.Center.Lng)),
geo.DistanceMetersToAngle(float64(gc.RadiusMeter)),
20,
)), nil
}
// GeoPolygon models an enclosed area on the earth.
// The bounding edges of this polygon shall be the shortest paths between connected vertices. This means, for instance, that the edge between two points both defined at a particular latitude is not generally contained at that latitude.
// The winding order shall be interpreted as the order which produces the smaller area.
// The path between two vertices shall be the shortest possible path between those vertices.
// Edges may not cross.
// Vertices may not be duplicated. In particular, the final polygon vertex shall not be identical to the first vertex.
type GeoPolygon struct {
Vertices []*LatLngPoint
}
// CalculateCovering returns the spatial covering of gp.
func (gp *GeoPolygon) CalculateCovering() (s2.CellUnion, error) {
var points []s2.Point
if gp == nil {
return nil, geo.ErrBadCoordSet
}
for _, v := range gp.Vertices {
// ensure that coordinates passed are actually on earth
if (v.Lat > maxLat) || (v.Lat < minLat) || (v.Lng > maxLng) || (v.Lng < minLng) {
return nil, geo.ErrBadCoordSet
}
points = append(points, s2.PointFromLatLng(s2.LatLngFromDegrees(v.Lat, v.Lng)))
}
if len(points) < 3 {
return nil, geo.ErrNotEnoughPointsInPolygon
}
return geo.Covering(points)
}
// LatLngPoint models a point on the earth's surface.
type LatLngPoint struct {
Lat float64
Lng float64
}