Skip to content

Commit

Permalink
geogfn: apply bounding box calculations for DWithin
Browse files Browse the repository at this point in the history
Expand a BoundingRect by a given radius (using s2.Cap) and apply
intersection to do fairly cheap bounding box calculations before doing
the expensive DWithin logic.

Release note: None
  • Loading branch information
otan committed Jul 1, 2020
1 parent 791ee6b commit 51ddf02
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 6 additions & 1 deletion pkg/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (g *Geography) AsS2(emptyBehavior EmptyBehavior) ([]s2.Region, error) {
return S2RegionsFromGeom(geomRepr, emptyBehavior)
}

// BoundingRect returns the bounding rectangle of the given Geography.
// BoundingRect returns the bounding s2.Rect of the given Geography.
func (g *Geography) BoundingRect() s2.Rect {
bbox := g.spatialObject.BoundingBox
if bbox == nil {
Expand All @@ -484,6 +484,11 @@ func (g *Geography) BoundingRect() s2.Rect {
}
}

// BoundingCap returns the bounding s2.Cap of the given Geography.
func (g *Geography) BoundingCap() s2.Cap {
return g.BoundingRect().CapBound()
}

// IsLinearRingCCW returns whether a given linear ring is counter clock wise.
// See 2.07 of http://www.faqs.org/faqs/graphics/algorithms-faq/.
// "Find the lowest vertex (or, if there is more than one vertex with the same lowest coordinate,
Expand Down
12 changes: 6 additions & 6 deletions pkg/geo/geogfn/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"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(
Expand Down Expand Up @@ -217,12 +223,6 @@ func distanceGeographyRegions(
return minDistance, nil
}

// 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

// geographyMinDistanceUpdater finds the minimum distance using a sphere.
// Methods will return early if it finds a minimum distance <= stopAfterLE.
type geographyMinDistanceUpdater struct {
Expand Down
17 changes: 13 additions & 4 deletions pkg/geo/geogfn/dwithin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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.
Expand All @@ -26,6 +27,18 @@ func DWithin(
if distance < 0 {
return false, errors.Newf("dwithin distance cannot be less than zero")
}
spheroid, err := a.Spheroid()
if err != nil {
return false, err
}

angleToExpand := s1.Angle(distance / spheroid.SphereRadius)
if useSphereOrSpheroid == UseSpheroid {
angleToExpand *= (1 + SpheroidErrorFraction)
}
if !a.BoundingCap().Expanded(angleToExpand).Intersects(b.BoundingCap()) {
return false, nil
}

aRegions, err := a.AsS2(geo.EmptyBehaviorError)
if err != nil {
Expand All @@ -41,10 +54,6 @@ func DWithin(
}
return false, err
}
spheroid, err := a.Spheroid()
if err != nil {
return false, err
}
maybeClosestDistance, err := distanceGeographyRegions(spheroid, useSphereOrSpheroid, aRegions, bRegions, distance)
if err != nil {
return false, err
Expand Down

0 comments on commit 51ddf02

Please sign in to comment.