github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geogfn/dwithin.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package geogfn 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/geo" 15 "github.com/cockroachdb/cockroach/pkg/geo/geographiclib" 16 "github.com/cockroachdb/errors" 17 ) 18 19 // DWithin returns whether a is within distance d of b, i.e. Distance(a, b) <= d. 20 // If A or B contains empty Geography objects, this will return false. 21 func DWithin( 22 a *geo.Geography, b *geo.Geography, distance float64, useSphereOrSpheroid UseSphereOrSpheroid, 23 ) (bool, error) { 24 if a.SRID() != b.SRID() { 25 return false, geo.NewMismatchingSRIDsError(a, b) 26 } 27 if distance < 0 { 28 return false, errors.Newf("dwithin distance cannot be less than zero") 29 } 30 31 aRegions, err := a.AsS2(geo.EmptyBehaviorError) 32 if err != nil { 33 if geo.IsEmptyGeometryError(err) { 34 return false, nil 35 } 36 return false, err 37 } 38 bRegions, err := b.AsS2(geo.EmptyBehaviorError) 39 if err != nil { 40 if geo.IsEmptyGeometryError(err) { 41 return false, nil 42 } 43 return false, err 44 } 45 spheroid := geographiclib.WGS84Spheroid 46 maybeClosestDistance, err := distanceGeographyRegions(spheroid, useSphereOrSpheroid, aRegions, bRegions, distance) 47 if err != nil { 48 return false, err 49 } 50 return maybeClosestDistance <= distance, nil 51 }