forked from rubenv/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify.go
49 lines (43 loc) · 987 Bytes
/
simplify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package topojson
import (
"github.com/paulmach/orb"
"github.com/paulmach/orb/simplify"
)
func (t *Topology) simplify() {
t.deletedArcs = make(map[int]bool)
t.shiftArcs = make(map[int]int)
if t.opts.Simplify == 0 {
for i := range t.Arcs {
t.deletedArcs[i] = false
t.shiftArcs[i] = 0
}
return
}
newArcs := make([][][]float64, 0)
s := simplify.VisvalingamThreshold(t.opts.Simplify)
for i, arc := range t.Arcs {
ls := make([]orb.Point, len(arc))
for i, a := range arc {
ls[i] = orb.Point{a[1], a[0]}
}
ls = s.LineString(ls)
newArc := make([][]float64, len(ls))
for j, p := range ls {
newArc[j] = []float64{p[1], p[0]}
}
if i == 0 {
t.shiftArcs[i] = 0
} else {
t.shiftArcs[i] = t.shiftArcs[i-1]
}
remove := len(newArc) <= 2 && pointEquals(newArc[0], newArc[1])
if remove {
// Zero-length arc, remove it!
t.deletedArcs[i] = true
t.shiftArcs[i]++
} else {
newArcs = append(newArcs, newArc)
}
}
t.Arcs = newArcs
}