diff --git a/geos/example_test.go b/geos/example_test.go index 488df4f..5454a9c 100644 --- a/geos/example_test.go +++ b/geos/example_test.go @@ -3,7 +3,7 @@ package geos_test import ( "fmt" - "github.com/paulsmith/gogeos/geos" + "github.com/helmi03/gogeos/geos" ) func ExampleGeometry_LineInterpolatePoint() { diff --git a/geos/examples.go b/geos/examples.go index 593c1d9..19a602f 100644 --- a/geos/examples.go +++ b/geos/examples.go @@ -13,7 +13,7 @@ import ( "code.google.com/p/draw2d/draw2d" - "github.com/paulsmith/gogeos/geos" + "github.com/helmi03/gogeos/geos" ) const ( diff --git a/geos/geom.go b/geos/geom.go index 964bcd3..f9eda3e 100644 --- a/geos/geom.go +++ b/geos/geom.go @@ -89,12 +89,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/geos_test.go b/geos/geos_test.go index c99c7bd..e345b9b 100644 --- a/geos/geos_test.go +++ b/geos/geos_test.go @@ -6,7 +6,7 @@ import ( ) func TestVersion(t *testing.T) { - const re = `3\.3\.\d+-CAPI-1\.7\.\d+$` + const re = `3\.[34]?\.\d+-CAPI-1\.[78]?\.\d+( .+)?$` version := Version() matched, err := regexp.MatchString(re, version) if err != nil { 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)) + } + } }