Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtins: add Geography builtins #48529

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions docs/generated/sql/functions.md

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions pkg/geo/geogfn/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,9 @@ import (
"github.com/golang/geo/s2"
)

type useSphereOrSpheroid bool

const (
// UseSpheroid indicates to use the spheroid for calculations.
UseSpheroid useSphereOrSpheroid = true
// UseSphere indicates to use the sphere for calculations.
UseSphere useSphereOrSpheroid = false
)

// Distance returns the distance between geographies a and b on a sphere or spheroid.
func Distance(
a *geo.Geography, b *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid,
a *geo.Geography, b *geo.Geography, useSphereOrSpheroid UseSphereOrSpheroid,
) (float64, error) {
if a.SRID() != b.SRID() {
return 0, geo.NewMismatchingSRIDsError(a, b)
Expand Down
2 changes: 1 addition & 1 deletion pkg/geo/geogfn/distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestDistance(t *testing.T) {
for _, subTC := range []struct {
desc string
expected float64
useSphereOrSpheroid useSphereOrSpheroid
useSphereOrSpheroid UseSphereOrSpheroid
}{
{"sphere", tc.expectedSphereDistance, UseSphere},
{"spheroid", tc.expectedSpheroidDistance, UseSpheroid},
Expand Down
2 changes: 1 addition & 1 deletion pkg/geo/geogfn/dwithin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// DWithin returns whether a is within distance d of b, i.e. Distance(a, b) <= d.
func DWithin(
a *geo.Geography, b *geo.Geography, distance float64, useSphereOrSpheroid useSphereOrSpheroid,
a *geo.Geography, b *geo.Geography, distance float64, useSphereOrSpheroid UseSphereOrSpheroid,
) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a, b)
Expand Down
2 changes: 1 addition & 1 deletion pkg/geo/geogfn/dwithin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDWithin(t *testing.T) {
for _, subTC := range []struct {
desc string
expected float64
useSphereOrSpheroid useSphereOrSpheroid
useSphereOrSpheroid UseSphereOrSpheroid
}{
{"sphere", tc.expectedSphereDistance, UseSphere},
{"spheroid", tc.expectedSpheroidDistance, UseSpheroid},
Expand Down
22 changes: 22 additions & 0 deletions pkg/geo/geogfn/geogfn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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

// UseSphereOrSpheroid indicates whether to use a Sphere or Spheroid
// for certain calculations.
type UseSphereOrSpheroid bool

const (
// UseSpheroid indicates to use the spheroid for calculations.
UseSpheroid UseSphereOrSpheroid = true
// UseSphere indicates to use the sphere for calculations.
UseSphere UseSphereOrSpheroid = false
)
8 changes: 4 additions & 4 deletions pkg/geo/geogfn/unary_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// Area returns the area of a given Geography.
func Area(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float64, error) {
func Area(g *geo.Geography, useSphereOrSpheroid UseSphereOrSpheroid) (float64, error) {
regions, err := g.AsS2()
if err != nil {
return 0, err
Expand Down Expand Up @@ -51,7 +51,7 @@ func Area(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float64, e
}

// Perimeter returns the perimeter of a given Geography.
func Perimeter(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float64, error) {
func Perimeter(g *geo.Geography, useSphereOrSpheroid UseSphereOrSpheroid) (float64, error) {
gt, err := g.AsGeomT()
if err != nil {
return 0, err
Expand All @@ -67,7 +67,7 @@ func Perimeter(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float
}

// Length returns length of a given Geography.
func Length(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float64, error) {
func Length(g *geo.Geography, useSphereOrSpheroid UseSphereOrSpheroid) (float64, error) {
gt, err := g.AsGeomT()
if err != nil {
return 0, err
Expand All @@ -84,7 +84,7 @@ func Length(g *geo.Geography, useSphereOrSpheroid useSphereOrSpheroid) (float64,

// length returns the sum of the lengtsh and perimeters in the shapes of the Geography.
// In OGC parlance, length returns both LineString lengths _and_ Polygon perimeters.
func length(regions []s2.Region, useSphereOrSpheroid useSphereOrSpheroid) (float64, error) {
func length(regions []s2.Region, useSphereOrSpheroid UseSphereOrSpheroid) (float64, error) {
spheroid := geographiclib.WGS84Spheroid

var totalLength float64
Expand Down
6 changes: 3 additions & 3 deletions pkg/geo/geogfn/unary_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestArea(t *testing.T) {

for _, subTC := range []struct {
desc string
useSphereOrSpheroid useSphereOrSpheroid
useSphereOrSpheroid UseSphereOrSpheroid
expected float64
}{
{"sphere", UseSphere, tc.sphere.expectedArea},
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestPerimeter(t *testing.T) {

for _, subTC := range []struct {
desc string
useSphereOrSpheroid useSphereOrSpheroid
useSphereOrSpheroid UseSphereOrSpheroid
expected float64
}{
{"sphere", UseSphere, tc.sphere.expectedPerimeter},
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestLength(t *testing.T) {

for _, subTC := range []struct {
desc string
useSphereOrSpheroid useSphereOrSpheroid
useSphereOrSpheroid UseSphereOrSpheroid
expected float64
}{
{"sphere", UseSphere, tc.sphere.expectedLength},
Expand Down
Loading