From cf0ac2471f0184793b27ed16d990acb3d59827ec Mon Sep 17 00:00:00 2001 From: jdejesus007 Date: Tue, 26 Nov 2019 12:10:17 -0500 Subject: [PATCH] Added wkb with srid meta data --- geos/geom.go | 8 +++++++- geos/wkb.go | 8 ++++++++ geos/wkb_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/geos/geom.go b/geos/geom.go index 784e6de..3777676 100644 --- a/geos/geom.go +++ b/geos/geom.go @@ -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() diff --git a/geos/wkb.go b/geos/wkb.go index aaef940..0a568e2 100644 --- a/geos/wkb.go +++ b/geos/wkb.go @@ -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) } diff --git a/geos/wkb_test.go b/geos/wkb_test.go index 3cd5436..c8ce754 100644 --- a/geos/wkb_test.go +++ b/geos/wkb_test.go @@ -2,6 +2,8 @@ package geos import ( "bytes" + "encoding/hex" + "strings" "testing" ) @@ -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() @@ -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)) + } + } }