Skip to content
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
94 changes: 80 additions & 14 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,56 @@ type Pointer interface {

// PointZer is a 3D point.
type PointZer interface {
Geometry
XYZ() [3]float64
Geometry
XYZ() [3]float64
}

// PointMer is a 2D+1D point.
type PointMer interface {
Geometry
XYM() [3]float64
Geometry
XYM() [3]float64
}

// PointZMer is a 3D+1D point.
type PointZMer interface {
Geometry
XYZM() [4]float64
Geometry
XYZM() [4]float64
}

// PointSer is a 2D point + SRID
type PointSer interface {
Geometry
XYS() struct {Srid uint32; Xy Point}
Geometry
XYS() struct {
Srid uint32
Xy Point
}
}

// PointZSer is a 3D point + SRID
type PointZSer interface {
Geometry
XYZS() struct {Srid uint32; Xyz PointZ}
Geometry
XYZS() struct {
Srid uint32
Xyz PointZ
}
}

// PointMSer is a 2D+1D point + SRID
type PointMSer interface {
Geometry
XYMS() struct {Srid uint32; Xym PointM}
Geometry
XYMS() struct {
Srid uint32
Xym PointM
}
}

// PointZMSer is a 3D+1D point + SRID
type PointZMSer interface {
Geometry
XYZMS() struct {Srid uint32; Xyzm PointZM}
Geometry
XYZMS() struct {
Srid uint32
Xyzm PointZM
}
}

// MultiPointer is a geometry with multiple points.
Expand All @@ -70,6 +82,60 @@ type LineStringer interface {
Vertices() [][2]float64
}

// LineStringMer is a line of two or more M points.
type LineStringMer interface {
Geometry
Vertices() [][3]float64
}

// LineStringZer is a line of two or more Z points.
type LineStringZer interface {
Geometry
Vertices() [][3]float64
}

// LineStringZMer is a line of two or more ZM points.
type LineStringZMer interface {
Geometry
Vertices() [][4]float64
}

// LineStringSer is a line of two or more points + SRID.
type LineStringSer interface {
Geometry
Vertices() struct {
Srid uint32
Ls LineString
}
}

// LineStringMSer is a line of two or more M points + SRID.
type LineStringMSer interface {
Geometry
Vertices() struct {
Srid uint32
Lsm LineStringM
}
}

// LineStringZSer is a line of two or more Z points + SRID.
type LineStringZSer interface {
Geometry
Vertices() struct {
Srid uint32
Lsz LineStringZ
}
}

// LineStringZMSer is a line of two or more ZM points + SRID.
type LineStringZMSer interface {
Geometry
Vertices() struct {
Srid uint32
Lszm LineStringZM
}
}

// MultiLineStringer is a geometry with multiple LineStrings.
type MultiLineStringer interface {
Geometry
Expand Down
41 changes: 41 additions & 0 deletions line_stringm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package geom

import (
"errors"
)

// ErrNilLineStringM is thrown when a LineStringM is nil but shouldn't be
var ErrNilLineStringM = errors.New("geom: nil LineStringM")

// ErrInvalidLineStringM is thrown when a LineStringM is malformed
var ErrInvalidLineStringM = errors.New("geom: invalid LineStringM")

// LineString is a basic line type which is made up of two or more points that don't interacted.
type LineStringM [][3]float64

// Vertices returns a slice of XYM values
func (lsm LineStringM) Vertices() [][3]float64 { return lsm }

// SetVertices modifies the array of 2D+1 coordinates
func (lsm *LineStringM) SetVertices(input [][3]float64) (err error) {
if lsm == nil {
return ErrNilLineStringM
}

*lsm = append((*lsm)[:0], input...)
return
}

// Get the simple 2D linestring
func (lsm LineStringM) LineString() LineString {
var lsv [][2]float64
var ls LineString

verts := lsm.Vertices()
for i := 0; i < len(verts); i++ {
lsv = append(lsv, [2]float64{verts[i][0], verts[i][1]})
}

ls.SetVertices(lsv)
return ls
}
66 changes: 66 additions & 0 deletions line_stringm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package geom_test

import (
"reflect"
"strconv"
"testing"

"github.com/go-spatial/geom"
)

func TestLineStringMSetter(t *testing.T) {
type tcase struct {
points [][3]float64
setter geom.LineStringMSetter
expected geom.LineStringMSetter
err error
}
fn := func(t *testing.T, tc tcase) {
err := tc.setter.SetVertices(tc.points)
if tc.err == nil && err != nil {
t.Errorf("error, expected nil got %v", err)
return
}
if tc.err != nil {
if tc.err.Error() != err.Error() {
t.Errorf("error, expected %v got %v", tc.err, err)
}
return
}
// compare the results
if !reflect.DeepEqual(tc.expected, tc.setter) {
t.Errorf("setter, expected %v got %v", tc.expected, tc.setter)
}
lsm := tc.setter.Vertices()
if !reflect.DeepEqual(tc.points, lsm) {
t.Errorf("Vertices, expected %v got %v", tc.points, lsm)
}
}
tests := []tcase{
{
points: [][3]float64{
{15, 20, 30},
{35, 40, 30},
{-15, -5, 12},
},
setter: &geom.LineStringM{
{10, 20, 30},
{30, 40, 30},
{-10, -5, -2},
},
expected: &geom.LineStringM{
{15, 20, 30},
{35, 40, 30},
{-15, -5, 12},
},
},
{
setter: (*geom.LineStringM)(nil),
err: geom.ErrNilLineStringM,
},
}
for i, tc := range tests {
tc := tc
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) { fn(t, tc) })
}
}
39 changes: 39 additions & 0 deletions line_stringms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package geom

import (
"errors"
)

// ErrNilLineStringMS is thrown when a LineStringS is nil but shouldn't be
var ErrNilLineStringMS = errors.New("geom: nil LineStringMS")

// ErrInvalidLineStringMS is thrown when a LineStringMS is malformed
var ErrInvalidLineStringMS = errors.New("geom: invalid LineStringMS")

// LineStringMS is a basic line type which is made up of two or more points that don't interacted.
type LineStringMS struct {
Srid uint32
Lsm LineStringM
}

// Vertices returns a slice of referenced XYM values
func (lsms LineStringMS) Vertices() struct {
Srid uint32
Lsm LineStringM
} { return lsms }

// SetVertices modifies the struct containing the SRID int and the array of 2D + 1 coordinates
func (lsms *LineStringMS) SetSRID(srid uint32, lsm LineStringM) (err error) {
if lsms == nil {
return ErrNilLineStringMS
}

lsms.Srid = srid
lsms.Lsm = lsm
return
}

// Get the simple 2D + 1 linestring
func (lsms LineStringMS) LineStringM() LineStringM {
return lsms.Lsm
}
61 changes: 61 additions & 0 deletions line_stringms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package geom_test

import (
"reflect"
"strconv"
"testing"

"github.com/go-spatial/geom"
)

func TestLineStringMSSetter(t *testing.T) {
type tcase struct {
srid uint32
linestringm geom.LineStringM
setter geom.LineStringMSSetter
expected geom.LineStringMSSetter
err error
}
fn := func(t *testing.T, tc tcase) {
err := tc.setter.SetSRID(tc.srid, tc.linestringm)
if tc.err == nil && err != nil {
t.Errorf("error, expected nil got %v", err)
return
}
if tc.err != nil {
if tc.err.Error() != err.Error() {
t.Errorf("error, expected %v got %v", tc.err, err)
}
return
}
// compare the results
if !reflect.DeepEqual(tc.expected, tc.setter) {
t.Errorf("setter, expected %v got %v", tc.expected, tc.setter)
}

lsms := tc.setter.Vertices()
tc_lsms := struct {
Srid uint32
Lsm geom.LineStringM
}{tc.srid, tc.linestringm}
if !reflect.DeepEqual(tc_lsms, lsms) {
t.Errorf("Referenced LineString, expected %v got %v", tc_lsms, lsms)
}
}
tests := []tcase{
{
srid: 4326,
linestringm: geom.LineStringM{{10, 20, 0.5}, {30, 40, 0.9}},
setter: &geom.LineStringMS{Srid: 4326, Lsm: geom.LineStringM{{15, 20, 0.5}, {35, 40, 0.9}}},
expected: &geom.LineStringMS{Srid: 4326, Lsm: geom.LineStringM{{10, 20, 0.5}, {30, 40, 0.9}}},
},
{
setter: (*geom.LineStringMS)(nil),
err: geom.ErrNilLineStringMS,
},
}
for i, tc := range tests {
tc := tc
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) { fn(t, tc) })
}
}
39 changes: 39 additions & 0 deletions line_strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package geom

import (
"errors"
)

// ErrNilLineStringS is thrown when a LineStringS is nil but shouldn't be
var ErrNilLineStringS = errors.New("geom: nil LineStringS")

// ErrInvalidLineStringS is thrown when a LineStringS is malformed
var ErrInvalidLineStringS = errors.New("geom: invalid LineStringS")

// LineString is a basic line type which is made up of two or more points that don't interacted.
type LineStringS struct {
Srid uint32
Ls LineString
}

// Vertices returns a slice of referenced XY values
func (lss LineStringS) Vertices() struct {
Srid uint32
Ls LineString
} { return lss }

// SetVertices modifies the struct containing the SRID int and the array of 2D coordinates
func (lss *LineStringS) SetSRID(srid uint32, ls LineString) (err error) {
if lss == nil {
return ErrNilLineStringS
}

lss.Srid = srid
lss.Ls = ls
return
}

// Get the simple 2D linestring
func (lss LineStringS) LineString() LineString {
return lss.Ls
}
Loading