forked from paulmach/go.geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
douglas_peucker.go
179 lines (141 loc) · 4.38 KB
/
douglas_peucker.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package reducers
import (
"github.com/paulmach/go.geo"
)
// A DouglasPeuckerReducer wraps the DouglasPeucker function
// to fulfill the geo.Reducer and geo.GeoReducer interfaces.
type DouglasPeuckerReducer struct {
Threshold float64
}
// NewDouglasPeucker creates a new DouglasPeuckerReducer.
func NewDouglasPeucker(threshold float64) *DouglasPeuckerReducer {
return &DouglasPeuckerReducer{
Threshold: threshold,
}
}
// Reduce runs the DouglasPeucker using the threshold of the DouglasPeuckerReducer.
func (r DouglasPeuckerReducer) Reduce(path *geo.Path) *geo.Path {
return DouglasPeucker(path, r.Threshold)
}
// GeoReduce runs the DouglasPeucker on a lng/lat path.
// The threshold is expected to be in meters.
func (r DouglasPeuckerReducer) GeoReduce(path *geo.Path) *geo.Path {
factor := geo.MercatorScaleFactor(path.Bound().Center().Lat())
merc := path.Clone().Transform(geo.Mercator.Project)
reduced := DouglasPeucker(merc, r.Threshold*factor)
return reduced.Transform(geo.Mercator.Inverse)
}
// DouglasPeucker simplifies the path using the Douglas Peucker method.
// Returns a new path and DOES NOT modify the original.
func DouglasPeucker(path *geo.Path, threshold float64) *geo.Path {
if path.Length() <= 2 {
return path.Clone()
}
mask := make([]byte, path.Length())
mask[0] = 1
mask[path.Length()-1] = 1
points := path.Points()
found := dpWorker(points, threshold, mask)
newPoints := make([]geo.Point, 0, found)
for i, v := range mask {
if v == 1 {
newPoints = append(newPoints, points[i])
}
}
return (&geo.Path{}).SetPoints(newPoints)
}
// DouglasPeuckerIndexMap is similar to DouglasPeucker but returns an array that maps
// each new path index to its original path index.
// Returns a new path and DOES NOT modify the original.
func DouglasPeuckerIndexMap(path *geo.Path, threshold float64) (reduced *geo.Path, indexMap []int) {
if path.Length() == 0 {
return path.Clone(), []int{}
}
if path.Length() == 1 {
return path.Clone(), []int{0}
}
if path.Length() == 2 {
return path.Clone(), []int{0, 1}
}
mask := make([]byte, path.Length())
mask[0] = 1
mask[path.Length()-1] = 1
originalPoints := path.Points()
found := dpWorker(originalPoints, threshold, mask)
points := make([]geo.Point, 0, found)
for i, v := range mask {
if v == 1 {
points = append(points, originalPoints[i])
indexMap = append(indexMap, i)
}
}
reduced = &geo.Path{}
return reduced.SetPoints(points), indexMap
}
// DouglasPeuckerGeoIndexMap is similar to GeoReduce but returns an array that maps
// each new path index to its original path index.
// Returns a new path and DOES NOT modify the original.
func DouglasPeuckerGeoIndexMap(path *geo.Path, meters float64) (reduced *geo.Path, indexMap []int) {
if path.Length() == 0 {
return path.Clone(), []int{}
}
if path.Length() == 1 {
return path.Clone(), []int{0}
}
if path.Length() == 2 {
return path.Clone(), []int{0, 1}
}
mask := make([]byte, path.Length())
mask[0] = 1
mask[path.Length()-1] = 1
factor := geo.MercatorScaleFactor(path.Bound().Center().Lat())
originalPoints := path.Clone().Transform(geo.Mercator.Project).Points()
found := dpWorker(originalPoints, meters*factor, mask)
points := make([]geo.Point, 0, found)
for i, v := range mask {
if v == 1 {
points = append(points, originalPoints[i])
indexMap = append(indexMap, i)
}
}
reduced = &geo.Path{}
reduced.SetPoints(points)
reduced.Transform(geo.Mercator.Inverse)
return reduced, indexMap
}
// dpWorker does the recursive threshold checks.
// Using a stack array with a stackLength variable resulted in 4x speed improvement
// over calling the function recursively.
func dpWorker(points []geo.Point, threshold float64, mask []byte) int {
found := 0
var stack []int
stack = append(stack, 0, len(points)-1)
l := &geo.Line{}
for len(stack) > 0 {
start := stack[len(stack)-2]
end := stack[len(stack)-1]
// modify the line in place
a := l.A()
a[0], a[1] = points[start][0], points[start][1]
b := l.B()
b[0], b[1] = points[end][0], points[end][1]
maxDist := 0.0
maxIndex := 0
for i := start + 1; i < end; i++ {
dist := l.SquaredDistanceFrom(&points[i])
if dist > maxDist {
maxDist = dist
maxIndex = i
}
}
if maxDist > threshold*threshold {
found++
mask[maxIndex] = 1
stack[len(stack)-1] = maxIndex
stack = append(stack, maxIndex, end)
} else {
stack = stack[:len(stack)-2]
}
}
return found
}