/
distance.go
386 lines (335 loc) · 12.9 KB
/
distance.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package geogfn
import (
"math"
"github.com/ruiaylin/pgparser/types/geo"
"github.com/ruiaylin/pgparser/types/geo/geodist"
"github.com/ruiaylin/pgparser/types/geo/geographiclib"
"github.com/cockroachdb/errors"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
)
// SpheroidErrorFraction is an error fraction to compensate for using a sphere
// to calculate the distance for what is actually a spheroid. The distance
// calculation has an error that is bounded by (2 * spheroid.Flattening)%.
// This 5% margin is pretty safe.
const SpheroidErrorFraction = 0.05
// Distance returns the distance between geographies a and b on a sphere or spheroid.
// Returns a geo.EmptyGeometryError if any of the Geographies are EMPTY.
func Distance(
a *geo.Geography, b *geo.Geography, useSphereOrSpheroid UseSphereOrSpheroid,
) (float64, error) {
if a.SRID() != b.SRID() {
return 0, geo.NewMismatchingSRIDsError(a, b)
}
aRegions, err := a.AsS2(geo.EmptyBehaviorError)
if err != nil {
return 0, err
}
bRegions, err := b.AsS2(geo.EmptyBehaviorError)
if err != nil {
return 0, err
}
spheroid, err := a.Spheroid()
if err != nil {
return 0, err
}
return distanceGeographyRegions(spheroid, useSphereOrSpheroid, aRegions, bRegions, 0)
}
//
// Spheroids
//
// s2GeodistLineString implements geodist.LineString.
type s2GeodistLineString struct {
*s2.Polyline
}
var _ geodist.LineString = (*s2GeodistLineString)(nil)
// IsShape implements the geodist.LineString interface.
func (*s2GeodistLineString) IsShape() {}
// LineString implements the geodist.LineString interface.
func (*s2GeodistLineString) IsLineString() {}
// Edge implements the geodist.LineString interface.
func (g *s2GeodistLineString) Edge(i int) geodist.Edge {
return geodist.Edge{
V0: geodist.Point{GeogPoint: (*g.Polyline)[i]},
V1: geodist.Point{GeogPoint: (*g.Polyline)[i+1]},
}
}
// NumEdges implements the geodist.LineString interface.
func (g *s2GeodistLineString) NumEdges() int {
return len(*g.Polyline) - 1
}
// Vertex implements the geodist.LineString interface.
func (g *s2GeodistLineString) Vertex(i int) geodist.Point {
return geodist.Point{
GeogPoint: (*g.Polyline)[i],
}
}
// NumVertexes implements the geodist.LineString interface.
func (g *s2GeodistLineString) NumVertexes() int {
return len(*g.Polyline)
}
// s2GeodistLinearRing implements geodist.LinearRing.
type s2GeodistLinearRing struct {
*s2.Loop
}
var _ geodist.LinearRing = (*s2GeodistLinearRing)(nil)
// IsShape implements the geodist.LinearRing interface.
func (*s2GeodistLinearRing) IsShape() {}
// LinearRing implements the geodist.LinearRing interface.
func (*s2GeodistLinearRing) IsLinearRing() {}
// Edge implements the geodist.LinearRing interface.
func (g *s2GeodistLinearRing) Edge(i int) geodist.Edge {
return geodist.Edge{
V0: geodist.Point{GeogPoint: g.Loop.Vertex(i)},
V1: geodist.Point{GeogPoint: g.Loop.Vertex(i + 1)},
}
}
// NumEdges implements the geodist.LinearRing interface.
func (g *s2GeodistLinearRing) NumEdges() int {
return g.Loop.NumEdges()
}
// Vertex implements the geodist.LinearRing interface.
func (g *s2GeodistLinearRing) Vertex(i int) geodist.Point {
return geodist.Point{
GeogPoint: g.Loop.Vertex(i),
}
}
// NumVertexes implements the geodist.LinearRing interface.
func (g *s2GeodistLinearRing) NumVertexes() int {
return g.Loop.NumVertices()
}
// s2GeodistPolygon implements geodist.Polygon.
type s2GeodistPolygon struct {
*s2.Polygon
}
var _ geodist.Polygon = (*s2GeodistPolygon)(nil)
// IsShape implements the geodist.Polygon interface.
func (*s2GeodistPolygon) IsShape() {}
// Polygon implements the geodist.Polygon interface.
func (*s2GeodistPolygon) IsPolygon() {}
// LinearRing implements the geodist.Polygon interface.
func (g *s2GeodistPolygon) LinearRing(i int) geodist.LinearRing {
return &s2GeodistLinearRing{Loop: g.Polygon.Loop(i)}
}
// NumLinearRings implements the geodist.Polygon interface.
func (g *s2GeodistPolygon) NumLinearRings() int {
return g.Polygon.NumLoops()
}
// s2GeodistEdgeCrosser implements geodist.EdgeCrosser.
type s2GeodistEdgeCrosser struct {
*s2.EdgeCrosser
}
var _ geodist.EdgeCrosser = (*s2GeodistEdgeCrosser)(nil)
// ChainCrossing implements geodist.EdgeCrosser.
func (c *s2GeodistEdgeCrosser) ChainCrossing(p geodist.Point) (bool, geodist.Point) {
// Returns nil for the intersection point as we don't require the intersection
// point as we do not have to implement ShortestLine in geography.
return c.EdgeCrosser.ChainCrossingSign(p.GeogPoint) != s2.DoNotCross, geodist.Point{}
}
// distanceGeographyRegions calculates the distance between two sets of regions.
// It will quit if it finds a distance that is less than stopAfterLE.
// It is not guaranteed to find the absolute minimum distance if stopAfterLE > 0.
//
// !!! SURPRISING BEHAVIOR WARNING FOR SPHEROIDS !!!
// PostGIS evaluates the distance between spheroid regions by computing the min of
// the pair-wise distance between the cross-product of the regions in A and the regions
// in B, where the pair-wise distance is computed as:
// * Find the two closest points between the pairs of regions using the sphere
// for distance calculations.
// * Compute the spheroid distance between the two closest points.
//
// This is technically incorrect, since it is possible that the two closest points on
// the spheroid are different than the two closest points on the sphere.
// See distance_test.go for examples of the "truer" distance values.
// Since we aim to be compatible with PostGIS, we adopt the same approach.
func distanceGeographyRegions(
spheroid *geographiclib.Spheroid,
useSphereOrSpheroid UseSphereOrSpheroid,
aRegions []s2.Region,
bRegions []s2.Region,
stopAfterLE float64,
) (float64, error) {
minDistance := math.MaxFloat64
for _, aRegion := range aRegions {
aGeodist, err := regionToGeodistShape(aRegion)
if err != nil {
return 0, err
}
for _, bRegion := range bRegions {
minDistanceUpdater := newGeographyMinDistanceUpdater(spheroid, useSphereOrSpheroid, stopAfterLE)
bGeodist, err := regionToGeodistShape(bRegion)
if err != nil {
return 0, err
}
earlyExit, err := geodist.ShapeDistance(
&geographyDistanceCalculator{updater: minDistanceUpdater},
aGeodist,
bGeodist,
)
if err != nil {
return 0, err
}
minDistance = math.Min(minDistance, minDistanceUpdater.Distance())
if earlyExit {
return minDistance, nil
}
}
}
return minDistance, nil
}
// geographyMinDistanceUpdater finds the minimum distance using a sphere.
// Methods will return early if it finds a minimum distance <= stopAfterLE.
type geographyMinDistanceUpdater struct {
spheroid *geographiclib.Spheroid
useSphereOrSpheroid UseSphereOrSpheroid
minEdge s2.Edge
minD s1.ChordAngle
stopAfterLE s1.ChordAngle
}
var _ geodist.DistanceUpdater = (*geographyMinDistanceUpdater)(nil)
// newGeographyMinDistanceUpdater returns a new geographyMinDistanceUpdater with the
// correct arguments set up.
func newGeographyMinDistanceUpdater(
spheroid *geographiclib.Spheroid, useSphereOrSpheroid UseSphereOrSpheroid, stopAfterLE float64,
) *geographyMinDistanceUpdater {
multiplier := 1.0
if useSphereOrSpheroid == UseSpheroid {
// Modify the stopAfterLE distance to be less by the error fraction, since
// we use the sphere to calculate the distance and we want to leave a
// buffer for spheroid distances being slightly off.
multiplier -= SpheroidErrorFraction
}
stopAfterLEChordAngle := s1.ChordAngleFromAngle(s1.Angle(stopAfterLE * multiplier / spheroid.SphereRadius))
return &geographyMinDistanceUpdater{
spheroid: spheroid,
minD: math.MaxFloat64,
useSphereOrSpheroid: useSphereOrSpheroid,
stopAfterLE: stopAfterLEChordAngle,
}
}
// Distance implements the DistanceUpdater interface.
func (u *geographyMinDistanceUpdater) Distance() float64 {
// If the distance is zero, avoid the call to spheroidDistance and return early.
if u.minD == 0 {
return 0
}
if u.useSphereOrSpheroid == UseSpheroid {
return spheroidDistance(u.spheroid, u.minEdge.V0, u.minEdge.V1)
}
return u.minD.Angle().Radians() * u.spheroid.SphereRadius
}
// Update implements the geodist.DistanceUpdater interface.
func (u *geographyMinDistanceUpdater) Update(aPoint geodist.Point, bPoint geodist.Point) bool {
a := aPoint.GeogPoint
b := bPoint.GeogPoint
sphereDistance := s2.ChordAngleBetweenPoints(a, b)
if sphereDistance < u.minD {
u.minD = sphereDistance
u.minEdge = s2.Edge{V0: a, V1: b}
// If we have a threshold, determine if we can stop early.
// If the sphere distance is within range of the stopAfter, we can
// definitively say we've reach the close enough point.
if u.minD <= u.stopAfterLE {
return true
}
}
return false
}
// OnIntersects implements the geodist.DistanceUpdater interface.
func (u *geographyMinDistanceUpdater) OnIntersects(p geodist.Point) bool {
u.minD = 0
return true
}
// IsMaxDistance implements the geodist.DistanceUpdater interface.
func (u *geographyMinDistanceUpdater) IsMaxDistance() bool {
return false
}
// FlipGeometries implements the geodist.DistanceUpdater interface.
func (u *geographyMinDistanceUpdater) FlipGeometries() {
// FlipGeometries is unimplemented for geographyMinDistanceUpdater as we don't
// require the order of geometries for calculation of minimum distance.
}
// geographyDistanceCalculator implements geodist.DistanceCalculator
type geographyDistanceCalculator struct {
updater *geographyMinDistanceUpdater
}
var _ geodist.DistanceCalculator = (*geographyDistanceCalculator)(nil)
// DistanceUpdater implements geodist.DistanceCalculator.
func (c *geographyDistanceCalculator) DistanceUpdater() geodist.DistanceUpdater {
return c.updater
}
// BoundingBoxIntersects implements geodist.DistanceCalculator.
func (c *geographyDistanceCalculator) BoundingBoxIntersects() bool {
// Return true, as it does the safer thing beneath.
// TODO(otan): update bounding box intersects.
return true
}
// NewEdgeCrosser implements geodist.DistanceCalculator.
func (c *geographyDistanceCalculator) NewEdgeCrosser(
edge geodist.Edge, startPoint geodist.Point,
) geodist.EdgeCrosser {
return &s2GeodistEdgeCrosser{
EdgeCrosser: s2.NewChainEdgeCrosser(
edge.V0.GeogPoint,
edge.V1.GeogPoint,
startPoint.GeogPoint,
),
}
}
// PointInLinearRing implements geodist.DistanceCalculator.
func (c *geographyDistanceCalculator) PointInLinearRing(
point geodist.Point, polygon geodist.LinearRing,
) bool {
return polygon.(*s2GeodistLinearRing).ContainsPoint(point.GeogPoint)
}
// ClosestPointToEdge implements geodist.DistanceCalculator.
//
// ClosestPointToEdge projects the point onto the infinite line represented
// by the edge. This will return the point on the line closest to the edge.
// It will return the closest point on the line, as well as a bool representing
// whether the point that is projected lies directly on the edge as a segment.
//
// For visualization and more, see: Section 6 / Figure 4 of
// "Projective configuration theorems: old wine into new wineskins", Tabachnikov, Serge, 2016/07/16
func (c *geographyDistanceCalculator) ClosestPointToEdge(
edge geodist.Edge, point geodist.Point,
) (geodist.Point, bool) {
eV0 := edge.V0.GeogPoint
eV1 := edge.V1.GeogPoint
// Project the point onto the normal of the edge. A great circle passing through
// the normal and the point will intersect with the great circle represented
// by the given edge.
normal := eV0.Vector.Cross(eV1.Vector).Normalize()
// To find the point where the great circle represented by the edge and the
// great circle represented by (normal, point), we project the point
// onto the normal.
normalScaledToPoint := normal.Mul(normal.Dot(point.GeogPoint.Vector))
// The difference between the point and the projection of the normal when normalized
// should give us a point on the great circle which contains the vertexes of the edge.
closestPoint := s2.Point{Vector: point.GeogPoint.Vector.Sub(normalScaledToPoint).Normalize()}
// We then check whether the given point lies on the geodesic of the edge,
// as the above algorithm only generates a point on the great circle
// represented by the edge.
return geodist.Point{GeogPoint: closestPoint}, (&s2.Polyline{eV0, eV1}).IntersectsCell(s2.CellFromPoint(closestPoint))
}
// regionToGeodistShape converts the s2 Region to a geodist object.
func regionToGeodistShape(r s2.Region) (geodist.Shape, error) {
switch r := r.(type) {
case s2.Point:
return &geodist.Point{GeogPoint: r}, nil
case *s2.Polyline:
return &s2GeodistLineString{Polyline: r}, nil
case *s2.Polygon:
return &s2GeodistPolygon{Polygon: r}, nil
}
return nil, errors.Newf("unknown region: %T", r)
}