Skip to content

Commit

Permalink
Merge pull request #114 from twpayne/ewkb
Browse files Browse the repository at this point in the history
Add ToEWKBWithSRID
  • Loading branch information
twpayne authored Jan 15, 2024
2 parents 003a1f3 + eb40d7a commit ba7784a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type Context struct {
sync.Mutex
handle C.GEOSContextHandle_t
ewkbWriter *C.struct_GEOSWKBWriter_t
geoJSONReader *C.struct_GEOSGeoJSONReader_t
geoJSONWriter *C.struct_GEOSGeoJSONWriter_t
wkbReader *C.struct_GEOSWKBReader_t
Expand Down
16 changes: 16 additions & 0 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ func (g *Geom) String() string {
return g.ToWKT()
}

// ToEWKB returns g in Extended WKB format with its SRID.
func (g *Geom) ToEWKBWithSRID() []byte {
g.mustNotBeDestroyed()
g.context.Lock()
defer g.context.Unlock()
if g.context.ewkbWriter == nil {
g.context.ewkbWriter = C.GEOSWKBWriter_create_r(g.context.handle)
C.GEOSWKBWriter_setFlavor_r(g.context.handle, g.context.ewkbWriter, C.GEOS_WKB_EXTENDED)
C.GEOSWKBWriter_setIncludeSRID_r(g.context.handle, g.context.ewkbWriter, 1)
}
var size C.size_t
ewkbCBuf := C.GEOSWKBWriter_write_r(g.context.handle, g.context.ewkbWriter, g.geom, &size)
defer C.GEOSFree_r(g.context.handle, unsafe.Pointer(ewkbCBuf))
return C.GoBytes(unsafe.Pointer(ewkbCBuf), C.int(size))
}

// ToGeoJSON returns g in GeoJSON format.
func (g *Geom) ToGeoJSON(indent int) string {
g.mustNotBeDestroyed()
Expand Down
20 changes: 20 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,26 @@ func TestWKXRoundTrip(t *testing.T) {
}
}

func TestEWKBWithSRIDRoundTrip(t *testing.T) {
c := geos.NewContext()
for _, tc := range []struct {
name string
geom *geos.Geom
}{
{
name: "point",
geom: mustNewGeomFromWKT(t, c, "POINT (0 0)").SetSRID(4326),
},
} {
t.Run(tc.name, func(t *testing.T) {
newG, err := c.NewGeomFromWKB(tc.geom.ToEWKBWithSRID())
assert.NoError(t, err)
assert.True(t, newG.Equals(tc.geom))
assert.Equal(t, tc.geom.SRID(), newG.SRID())
})
}
}

func TestGeomRelate(t *testing.T) {
c := geos.NewContext()
g1 := mustNewGeomFromWKT(t, c, "POINT (0 0)")
Expand Down
2 changes: 1 addition & 1 deletion geometry/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewGeometryFromWKB(wkb []byte) (*Geometry, error) {

// MarshalBinary implements encoding.BinaryMarshaler.
func (g *Geometry) MarshalBinary() ([]byte, error) {
return g.Geom.ToWKB(), nil
return g.Geom.ToEWKBWithSRID(), nil
}

// UnmarshalBinary implements encoding.BinaryUnmarshaler.
Expand Down
2 changes: 1 addition & 1 deletion geometry/gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func (g *Geometry) GobEncode() ([]byte, error) {
if g.Geom == nil {
return nil, nil
}
return g.Geom.ToWKB(), nil
return g.Geom.ToEWKBWithSRID(), nil
}
2 changes: 1 addition & 1 deletion geometry/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func (g Geometry) Value() (driver.Value, error) {
if g.Geom == nil {
return nil, nil
}
return hex.EncodeToString(g.Geom.ToWKB()), nil
return hex.EncodeToString(g.Geom.ToEWKBWithSRID()), nil
}

0 comments on commit ba7784a

Please sign in to comment.