Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXP] Multilinestring simplification experiments #290

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions internal/xy/topo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package xy

import "fmt"

func RemoveIndex(s []int, index int) []int {
return append(s[:index], s[index+1:]...)
}

type Segment [2]Point

func TopoSimplify(lines [][]Point) [][]Point {
sharedPoints := map[Point]int{}
segs := map[Segment]bool{}
for _, line := range lines {
sharedPoints[line[0]] += 2
sharedPoints[line[len(line)-1]] += 2
for i := 1; i < len(line); i++ {
sharedPoints[line[i]] += 1
segs[Segment{line[i-1], line[i]}] = true
}
}
fmt.Println("shared points:", sharedPoints)
lineSegs := [][]Segment{}
for pt, v := range sharedPoints {
delete(sharedPoints, pt)
if v < 2 {
continue
}
fmt.Println("processing point:", pt)
lineSeg := []Segment{}
// get open seg
for {
found := false
nseg := Segment{}
for nseg = range segs {
if nseg[0] == pt {
// ok
delete(segs, nseg)
found = true
break
}
// if nseg[1] == pt {
// // ok
// delete(segs, nseg)
// nseg = Segment{nseg[1], nseg[0]}
// found = true
// break
// }
}
if !found {
fmt.Println("\t\tno seg for pt:", pt)
lineSegs = append(lineSegs, lineSeg)
lineSeg = nil
break
}
lineSeg = append(lineSeg, nseg)
pt = nseg[1]
fmt.Println("\t\tfound:", found, "nseg:", nseg)
fmt.Println("\t\tnow:", lineSeg)
}
}
ret := [][]Point{}
for _, lineSeg := range lineSegs {
rl := []Point{lineSeg[0][0], lineSeg[0][1]}
for i := 1; i < len(lineSeg); i++ {
rl = append(rl, lineSeg[i][1])
}
ret = append(ret, rl)
}
fmt.Println("result:")
for _, line := range ret {
fmt.Println(line)
}
return ret
}
87 changes: 87 additions & 0 deletions internal/xy/topo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package xy

import (
"io/ioutil"
"testing"

geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
)

func TestTopoSimplify(t *testing.T) {
testcases := []struct {
name string
line [][]Point
}{
{
"a",
[][]Point{
{{0, 1}, {0, 2}, {0, 3}},
{{0, 1}, {0, 2}, {0, 3}},
},
},
{
"b",
[][]Point{
{{0, 1}, {0, 4}, {0, 2}, {0, 3}},
{{0, 1}, {0, 2}, {0, 3}},
},
},
{
"c",
[][]Point{
{{0, 1}, {0, 1}, {0, 1}, {0, 1}},
{{0, 1}, {0, 2}, {0, 3}},
},
},
{
"d",
[][]Point{
{{1, 1}, {1, 2}, {1, 3}},
{{0, 1}, {0, 2}, {0, 3}},
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
TopoSimplify(tc.line)
})
}
}

func TestTopoSimplify_File(t *testing.T) {
testcases := []struct {
name string
fn string
}{
{
"a",
"../../test/data/geojson/mls.geojson",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
data, err := ioutil.ReadFile(tc.fn)
if err != nil {
t.Fatal(err)
}
fc := geojson.FeatureCollection{}
if err := fc.UnmarshalJSON(data); err != nil {
t.Fatal(err)
}
g, ok := fc.Features[0].Geometry.(*geom.MultiLineString)
if !ok {
return
}
var lines [][]Point
for i := 0; i < g.NumLineStrings(); i++ {
var line []Point
for _, c := range g.LineString(i).Coords() {
line = append(line, Point{c.X(), c.Y()})
}
lines = append(lines, line)
}
TopoSimplify(lines)
})
}
}
Loading