Skip to content

Commit

Permalink
wkb.MultiPoint fix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxbee committed Apr 20, 2016
1 parent 1d0b39d commit 7eabfd3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
28 changes: 27 additions & 1 deletion wkb/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Point struct {
X, Y float64
}

func (p Point) Equal(other Point) bool {
return p.X == other.X && p.Y == other.Y
}

func (p *Point) Scan(src interface{}) error {
b, dec, err := header(src, GeomPoint)
if err != nil {
Expand Down Expand Up @@ -38,7 +42,29 @@ func readPoint(b []byte, dec binary.ByteOrder) ([]byte, Point, error) {
}

func readMultiPoint(b []byte, dec binary.ByteOrder) ([]byte, MultiPoint, error) {
return readPoints(b, dec)
b, n, err := readCount(b, dec)
if err != nil {
return nil, nil, err
}

if len(b) < (HeaderSize+PointSize)*n {
return nil, nil, ErrInvalidStorage
}

mp := make([]Point, n)
for i := 0; i < n; i++ {
b, dec, err = byteHeader(b, GeomPoint)
if err != nil {
return nil, nil, err
}

b, mp[i], err = readPoint(b, dec)
if err != nil {
return nil, nil, err
}
}

return b, mp, nil
}

func readPoints(b []byte, dec binary.ByteOrder) ([]byte, []Point, error) {
Expand Down
30 changes: 28 additions & 2 deletions wkb/point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,33 @@ func TestPoint(t *testing.T) {
}
p := Point{}
if assert.NoError(t, p.Scan(valid)) {
assert.Equal(t, 30.0, p.X)
assert.Equal(t, 10.0, p.Y)
assert.Equal(t, Point{30, 10}, p)
}
}

func TestMultipoint(t *testing.T) {
b := []byte{
0x01, 0x04, 0x00, 0x00, 0x00, // header
0x04, 0x00, 0x00, 0x00, // numpoints - 4
0x01, 0x01, 0x00, 0x00, 0x00, // point 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x40,
0x01, 0x01, 0x00, 0x00, 0x00, // point 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x40,
0x01, 0x01, 0x00, 0x00, 0x00, // point 3
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x40,
0x01, 0x01, 0x00, 0x00, 0x00, // point 4
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40,
}

mp := MultiPoint{}
if assert.NoError(t, mp.Scan(b)) && assert.Equal(t, 4, len(mp)) {
assert.Equal(t, Point{10, 40}, mp[0])
assert.Equal(t, Point{40, 30}, mp[1])
assert.Equal(t, Point{20, 20}, mp[2])
assert.Equal(t, Point{30, 10}, mp[3])
}
}
4 changes: 4 additions & 0 deletions wkb/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func header(src interface{}, tpe Kind) ([]byte, binary.ByteOrder, error) {
return nil, nil, ErrInvalidStorage
}

return byteHeader(b, tpe)
}

func byteHeader(b []byte, tpe Kind) ([]byte, binary.ByteOrder, error) {
if len(b) < HeaderSize {
return nil, nil, ErrInvalidStorage
}
Expand Down

0 comments on commit 7eabfd3

Please sign in to comment.