-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geo/geogfn: implement ST_Azimuth({geometry,geometry})
Fixes #48887 Release note (sql change): This PR implement adds the `ST_Azimuth({geometry,geometry})` builtin.
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 geomfn | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/cockroachdb/errors" | ||
"github.com/twpayne/go-geom" | ||
) | ||
|
||
// Azimuth returns the azimuth in radians of the segment defined by the given point geometries. | ||
// The azimuth is angle is referenced from north, and is positive clockwise. | ||
// North = 0; East = π/2; South = π; West = 3π/2. | ||
// Returns nil if the two points are the same. | ||
// Returns an error if any of the two Geometry items are not points. | ||
func Azimuth(a *geo.Geometry, b *geo.Geometry) (*float64, error) { | ||
aGeomT, err := a.AsGeomT() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
aPoint, ok := aGeomT.(*geom.Point) | ||
if !ok { | ||
return nil, errors.Newf("Argument must be POINT geometries") | ||
} | ||
|
||
bGeomT, err := b.AsGeomT() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bPoint, ok := bGeomT.(*geom.Point) | ||
if !ok { | ||
return nil, errors.Newf("Argument must be POINT geometries") | ||
} | ||
|
||
if aPoint.X() == bPoint.X() && aPoint.Y() == bPoint.Y() { | ||
return nil, nil | ||
} | ||
|
||
atan := math.Atan2(bPoint.Y()-aPoint.Y(), bPoint.X()-aPoint.X()) // Started at East(90) counterclockwise. | ||
const degree360 = 2 * math.Pi // Added 360 degrees for always returns a positive value. | ||
const degree90 = math.Pi / 2 // Added 90 degrees to get it started at North(0). | ||
|
||
azimuth := math.Mod(degree360+degree90-atan, 2*math.Pi) | ||
|
||
return &azimuth, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 geomfn | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAzimuth(t *testing.T) { | ||
zero := 0.0 | ||
aQuarterPi := 0.7853981633974483 | ||
towQuartersPi := 1.5707963267948966 | ||
threeQuartersPi := 2.356194490192344 | ||
|
||
testCases := []struct { | ||
a string | ||
b string | ||
expected *float64 | ||
}{ | ||
{ | ||
"POINT(0 0)", | ||
"POINT(0 0)", | ||
nil, | ||
}, | ||
{ | ||
"POINT(0 0)", | ||
"POINT(1 1)", | ||
&aQuarterPi, | ||
}, | ||
{ | ||
"POINT(0 0)", | ||
"POINT(1 0)", | ||
&towQuartersPi, | ||
}, | ||
{ | ||
"POINT(0 0)", | ||
"POINT(1 -1)", | ||
&threeQuartersPi, | ||
}, | ||
{ | ||
"POINT(0 0)", | ||
"POINT(0 1)", | ||
&zero, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("tc:%d", i), func(t *testing.T) { | ||
a, err := geo.ParseGeometry(tc.a) | ||
require.NoError(t, err) | ||
b, err := geo.ParseGeometry(tc.b) | ||
require.NoError(t, err) | ||
|
||
r, err := Azimuth(a, b) | ||
require.NoError(t, err) | ||
|
||
if tc.expected == nil { | ||
require.Nil(t, r) | ||
} else { | ||
require.Equal(t, *tc.expected, *r) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters