Skip to content

Commit

Permalink
chore: move Float64Slices methods into pj.go
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Oct 31, 2024
1 parent f8e60d6 commit 7a33fe0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 88 deletions.
22 changes: 0 additions & 22 deletions float64slices.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package proj

// ForwardFloat64Slices transforms float64Slices in the forward direction.
func (pj *PJ) ForwardFloat64Slices(float64Slices [][]float64) error {
return pj.TransFloat64Slices(DirectionFwd, float64Slices)
}

// InverseFloat64Slices transforms float64Slices in the inverse direction.
func (pj *PJ) InverseFloat64Slices(float64Slices [][]float64) error {
return pj.TransFloat64Slices(DirectionInv, float64Slices)
}

// TransFloat64Slices transforms float64Slices.
func (pj *PJ) TransFloat64Slices(direction Direction, float64Slices [][]float64) error {
coords := Float64SlicesToCoords(float64Slices)
if err := pj.TransArray(direction, coords); err != nil {
return err
}
for i, coord := range coords {
copy(float64Slices[i], coord[:])
}
return nil
}

// CoordsToFloat64Slices is a convenience function that converts a slice of
// Coords to a slice of []float64s. For performance, the returned []float64s
// alias coords.
Expand Down
66 changes: 0 additions & 66 deletions float64slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,69 +42,3 @@ func TestCoordsToFloat64Slices(t *testing.T) {
})
}
}

func TestTransFloat64Slices(t *testing.T) {
for i, tc := range []struct {
float64Slices [][]float64
expected [][]float64
delta float64
}{
{
float64Slices: nil,
expected: nil,
},
{
float64Slices: [][]float64{},
expected: [][]float64{},
},
{
float64Slices: [][]float64{
{48.856613, 2.352222, 78},
{40.712778, -74.006111, 10},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563, 78},
{-8238322.592110482, 4970068.348185822, 10},
},
delta: 1e-9,
},
{
float64Slices: [][]float64{
{48.856613, 2.352222, 78, 1},
{40.712778, -74.006111, 10, 2},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563, 78, 1},
{-8238322.592110482, 4970068.348185822, 10, 2},
},
delta: 1e-9,
},
{
float64Slices: [][]float64{
{48.856613, 2.352222},
{40.712778, -74.006111, 10, 2},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563},
{-8238322.592110482, 4970068.348185822, 10, 2},
},
delta: 1e-9,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
pj, err := proj.NewCRSToCRS("EPSG:4326", "EPSG:3857", nil)
assert.NoError(t, err)
float64Slices := slices.Clone(tc.float64Slices)
assert.NoError(t, pj.ForwardFloat64Slices(float64Slices))
assertInDeltaFloat64Slices(t, tc.expected, float64Slices, tc.delta)
})
}
}

func assertInDeltaFloat64Slices(tb testing.TB, expected, actual [][]float64, delta float64) {
tb.Helper()
assert.Equal(tb, len(expected), len(actual))
for i := range expected {
assertInDeltaFloat64Slice(tb, expected[i], actual[i], delta)
}
}
22 changes: 22 additions & 0 deletions pj.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (pj *PJ) ForwardFloat64Slice(float64Slice []float64) ([]float64, error) {
return pj.TransFloat64Slice(DirectionFwd, float64Slice)
}

// ForwardFloat64Slices transforms float64Slices in the forward direction.
func (pj *PJ) ForwardFloat64Slices(float64Slices [][]float64) error {
return pj.TransFloat64Slices(DirectionFwd, float64Slices)
}

// Geod returns the distance, forward azimuth, and reverse azimuth between a and b.
func (pj *PJ) Geod(a, b Coord) (float64, float64, float64) {
pj.context.Lock()
Expand Down Expand Up @@ -140,6 +145,11 @@ func (pj *PJ) InverseFloat64Slice(float64Slice []float64) ([]float64, error) {
return pj.TransFloat64Slice(DirectionInv, float64Slice)
}

// InverseFloat64Slices transforms float64Slices in the inverse direction.
func (pj *PJ) InverseFloat64Slices(float64Slices [][]float64) error {
return pj.TransFloat64Slices(DirectionInv, float64Slices)
}

// LPDist returns the geodesic distance between a and b in geodetic coordinates.
func (pj *PJ) LPDist(a, b Coord) float64 {
pj.context.Lock()
Expand Down Expand Up @@ -248,6 +258,18 @@ func (pj *PJ) TransFloat64Slice(direction Direction, float64Slice []float64) ([]
return float64Slice, nil
}

// TransFloat64Slices transforms float64Slices.
func (pj *PJ) TransFloat64Slices(direction Direction, float64Slices [][]float64) error {
coords := Float64SlicesToCoords(float64Slices)
if err := pj.TransArray(direction, coords); err != nil {
return err
}
for i, coord := range coords {
copy(float64Slices[i], coord[:])
}
return nil
}

// TransGeneric transforms a series of coordinates.
func (pj *PJ) TransGeneric(direction Direction, x *float64, sx, nx int, y *float64, sy, ny int, z *float64, sz, nz int, m *float64, sm, nm int) error {
pj.context.Lock()
Expand Down
66 changes: 66 additions & 0 deletions pj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,64 @@ func TestPJ_NormalizeForVisualizationForEastingNorthingCRS(t *testing.T) {
assertInDeltaFloat64Slice(t, newYorkEPSG3857[:], actualCoord[:], 1e-7)
}

func TestPJ_TransFloat64Slices(t *testing.T) {
for i, tc := range []struct {
float64Slices [][]float64
expected [][]float64
delta float64
}{
{
float64Slices: nil,
expected: nil,
},
{
float64Slices: [][]float64{},
expected: [][]float64{},
},
{
float64Slices: [][]float64{
{48.856613, 2.352222, 78},
{40.712778, -74.006111, 10},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563, 78},
{-8238322.592110482, 4970068.348185822, 10},
},
delta: 1e-9,
},
{
float64Slices: [][]float64{
{48.856613, 2.352222, 78, 1},
{40.712778, -74.006111, 10, 2},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563, 78, 1},
{-8238322.592110482, 4970068.348185822, 10, 2},
},
delta: 1e-9,
},
{
float64Slices: [][]float64{
{48.856613, 2.352222},
{40.712778, -74.006111, 10, 2},
},
expected: [][]float64{
{261848.15527273554, 6250566.54904563},
{-8238322.592110482, 4970068.348185822, 10, 2},
},
delta: 1e-9,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
pj, err := proj.NewCRSToCRS("EPSG:4326", "EPSG:3857", nil)
assert.NoError(t, err)
float64Slices := slices.Clone(tc.float64Slices)
assert.NoError(t, pj.ForwardFloat64Slices(float64Slices))
assertInDeltaFloat64Slices(t, tc.expected, float64Slices, tc.delta)
})
}
}

func assertInDelta(tb testing.TB, expected, actual, delta float64) {
tb.Helper()
if actualDelta := math.Abs(expected - actual); actualDelta > delta {
Expand All @@ -558,3 +616,11 @@ func assertInDeltaFloat64Slice(tb testing.TB, expected, actual []float64, delta
}
}
}

func assertInDeltaFloat64Slices(tb testing.TB, expected, actual [][]float64, delta float64) {
tb.Helper()
assert.Equal(tb, len(expected), len(actual))
for i := range expected {
assertInDeltaFloat64Slice(tb, expected[i], actual[i], delta)
}
}

0 comments on commit 7a33fe0

Please sign in to comment.