Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: twpayne/go-geos
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.15.0
Choose a base ref
...
head repository: twpayne/go-geos
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.16.0
Choose a head ref
  • 5 commits
  • 6 files changed
  • 2 contributors

Commits on Jan 15, 2024

  1. Add ToEWKBWithSRID

    twpayne committed Jan 15, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    baacdc1 View commit details
  2. Use EWKB with SRID in geometry.Geometry

    twpayne committed Jan 15, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    eb40d7a View commit details
  3. Merge pull request #114 from twpayne/ewkb

    Add ToEWKBWithSRID
    twpayne authored Jan 15, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    ba7784a View commit details
  4. Add Geom.ClipByBounds

    twpayne committed Jan 15, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    76e2fb2 View commit details
  5. Merge pull request #115 from twpayne/clip-by-bounds

    Add Geom.ClipByBounds
    twpayne authored Jan 15, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    acfb301 View commit details
Showing with 44 additions and 3 deletions.
  1. +1 −0 context.go
  2. +20 −0 geom.go
  3. +20 −0 geom_test.go
  4. +1 −1 geometry/binary.go
  5. +1 −1 geometry/gob.go
  6. +1 −1 geometry/sql.go
1 change: 1 addition & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions geom.go
Original file line number Diff line number Diff line change
@@ -54,6 +54,10 @@ func (g *Geom) MakeValidWithParams(method MakeValidMethod, collapse MakeValidCol
return g.context.newGeom(cRes, nil)
}

func (g *Geom) ClipByBounds(bounds *Bounds) *Geom {
return g.ClipByRect(bounds.MinX, bounds.MinY, bounds.MaxX, bounds.MaxY)
}

// CoordSeq returns g's coordinate sequence.
func (g *Geom) CoordSeq() *CoordSeq {
g.mustNotBeDestroyed()
@@ -262,6 +266,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()
20 changes: 20 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
@@ -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)")
2 changes: 1 addition & 1 deletion geometry/binary.go
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion geometry/gob.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
}