Skip to content

Commit

Permalink
Added wkb with srid meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
jdejesus007 committed Nov 26, 2019
1 parent b165428 commit cf0ac24
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion geos/geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ func (g *Geometry) String() string {
return str
}

// WKB returns the geoemtry encoded as a Well-Known Binary (WKB).
// WKB returns the geometry encoded as a Well-Known Binary (WKB).
func (g *Geometry) WKB() ([]byte, error) {
encoder := newWkbEncoder()
return encoder.encode(g)
}

// EWKB returns the geometry encoded as a Well-Known Binary (WKB) with SRID meta data.
func (g *Geometry) EWKB() ([]byte, error) {
encoder := newWkbEncoder()
return encoder.encodeEWkb(g)
}

// Hex returns the geometry as a Well-Known Binary (WKB) hex-encoded byte slice.
func (g *Geometry) Hex() ([]byte, error) {
encoder := newWkbEncoder()
Expand Down
8 changes: 8 additions & 0 deletions geos/wkb.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func encodeWkb(e *wkbEncoder, g *Geometry, fn func(*C.GEOSWKBWriter, *C.GEOSGeom
return out, nil
}

func (e *wkbEncoder) encodeEWkb(g *Geometry) ([]byte, error) {
srid, _ := g.SRID()
if srid > 0 {
cGEOSWKBWriter_setIncludeSRID(e.w, C.char(1))
}
return encodeWkb(e, g, cGEOSWKBWriter_write)
}

func (e *wkbEncoder) encode(g *Geometry) ([]byte, error) {
return encodeWkb(e, g, cGEOSWKBWriter_write)
}
Expand Down
24 changes: 24 additions & 0 deletions geos/wkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package geos

import (
"bytes"
"encoding/hex"
"strings"
"testing"
)

Expand Down Expand Up @@ -72,6 +74,16 @@ var wkbEncoderHexTests = []struct {
{"POINT(-117 35)", []byte("01010000000000000000405DC00000000000804140")},
}

var ewkbEncoderHexTests = []struct {
wkt string
srid int
wkb []byte
}{
{"POINT(-117 35)", 4326, []byte("0101000020E61000000000000000405DC00000000000804140")},
{"POINT(-117 35)", 900913, []byte("010100002031BF0D000000000000405DC00000000000804140")},
{"POINT(-117 35)", 0, []byte("01010000000000000000405DC00000000000804140")},
}

func TestWkbEncoderEncodeHex(t *testing.T) {
wktDecoder := newWktDecoder()
wkbEncoder := newWkbEncoder()
Expand All @@ -85,4 +97,16 @@ func TestWkbEncoderEncodeHex(t *testing.T) {
t.Errorf("#%d: want %v got %v", i, string(test.wkb), string(actual))
}
}
for i, test := range ewkbEncoderHexTests {
g1 := Must(wktDecoder.decode(test.wkt))
g1.SetSRID(test.srid)
eg1, err := wkbEncoder.encodeEWkb(g1)
if err != nil {
panic(err)
}
actual := []byte(strings.ToUpper(hex.EncodeToString(eg1)))
if !bytes.Equal(actual, test.wkb) {
t.Errorf("#%d: want %v got %v", i, string(test.wkb), string(actual))
}
}
}

0 comments on commit cf0ac24

Please sign in to comment.