Skip to content

Commit

Permalink
Merge pull request #113 from twpayne/no-aliasing
Browse files Browse the repository at this point in the history
Fix aliasing of coordinates in coordinate sequences
  • Loading branch information
twpayne authored Jan 11, 2024
2 parents 0c7611e + f970fe3 commit 003a1f3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (c *Context) newCoordsFromGEOSCoordSeq(s *C.struct_GEOSCoordSeq_t) [][]floa
}
coords := make([][]float64, 0, size)
for i := 0; i < int(size); i++ {
coord := flatCoords[i*int(dimensions) : (i+1)*int(dimensions)]
coord := flatCoords[i*int(dimensions) : (i+1)*int(dimensions) : (i+1)*int(dimensions)]
coords = append(coords, coord)
}
return coords
Expand Down
2 changes: 1 addition & 1 deletion coordseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *CoordSeq) ToCoords() [][]float64 {
coords := make([][]float64, s.size)
j := 0
for i := 0; i < s.size; i++ {
coords[i] = flatCoords[j : j+s.dimensions]
coords[i] = flatCoords[j : j+s.dimensions : j+s.dimensions]
j += s.dimensions
}
return coords
Expand Down
6 changes: 6 additions & 0 deletions coordseq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/twpayne/go-geos"
)

func TestCoordSeqAliasing(t *testing.T) {
coords := geos.NewContext().NewCoordSeqFromCoords([][]float64{{0, 1}, {2, 3}}).ToCoords()
coords[0] = append(coords[0], 4)
assert.Equal(t, []float64{2, 3}, coords[1])
}

func TestCoordSeqEmpty(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
Expand Down
9 changes: 9 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ func TestNewGeomFromGeoJSONError(t *testing.T) {
assert.Error(t, err)
}

func TestGeomNearestPointsAliasing(t *testing.T) {
c := geos.NewContext()
geom1 := mustNewGeomFromWKT(t, c, "POINT (0 1)")
geom2 := mustNewGeomFromWKT(t, c, "POINT (2 3)")
points := geom1.NearestPoints(geom2)
points[0] = append(points[0], 4)
assert.Equal(t, []float64{2, 3}, points[1])
}

func TestGeomToJSON(t *testing.T) {
geom := mustNewGeomFromWKT(t, geos.NewContext(), "POINT (1 2)")
assert.Equal(t, `{"type":"Point","coordinates":[1.0,2.0]}`, geom.ToGeoJSON(-1))
Expand Down

0 comments on commit 003a1f3

Please sign in to comment.