Commit f4b863df authored by Oliver Tan's avatar Oliver Tan

builtins: add Geography builtins

Fruits of labour be released to the public!

Added some more helpers for InfoBuilder to include the source library in
which our calculations are done.

Release note (sql change): We introduce the following newly implemented
functions that work on GEOGRAPHY types:
* ST_Covers
* ST_CoveredBy
* ST_Intersects
* ST_Distance
* ST_DWithin
* ST_Perimeter
* ST_Area
* ST_Length
parent ba608ef2
This diff is collapsed.
......@@ -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)
......
......@@ -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},
......
......@@ -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)
......
......@@ -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},
......
// 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
)
......@@ -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
......@@ -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
......@@ -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
......@@ -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
......
......@@ -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},
......@@ -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},
......@@ -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},
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment