-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdwithin.go
62 lines (57 loc) · 1.76 KB
/
dwithin.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
// 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 (
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/errors"
"github.com/golang/geo/s1"
)
// DWithin returns whether a is within distance d of b, i.e. Distance(a, b) <= d.
// If A or B contains empty Geography objects, this will return false.
func DWithin(
a *geo.Geography, b *geo.Geography, distance float64, useSphereOrSpheroid UseSphereOrSpheroid,
) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a, b)
}
if distance < 0 {
return false, errors.Newf("dwithin distance cannot be less than zero")
}
spheroid, err := a.Spheroid()
if err != nil {
return false, err
}
distanceToExpand := s1.Angle(distance / spheroid.SphereRadius)
if useSphereOrSpheroid == UseSpheroid {
distanceToExpand *= (1 + SpheroidErrorFraction)
}
if !a.BoundingCap().Expanded(distanceToExpand).Intersects(b.BoundingCap()) {
return false, nil
}
aRegions, err := a.AsS2(geo.EmptyBehaviorError)
if err != nil {
if geo.IsEmptyGeometryError(err) {
return false, nil
}
return false, err
}
bRegions, err := b.AsS2(geo.EmptyBehaviorError)
if err != nil {
if geo.IsEmptyGeometryError(err) {
return false, nil
}
return false, err
}
maybeClosestDistance, err := distanceGeographyRegions(spheroid, useSphereOrSpheroid, aRegions, bRegions, distance)
if err != nil {
return false, err
}
return maybeClosestDistance <= distance, nil
}