-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify.py
275 lines (211 loc) · 7.89 KB
/
simplify.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
simplify.py is a simple port of simplify.js by Vladimir Agafonkin
(https://github.com/mourner/simplify-js)
It uses a combination of Douglas-Peucker and Radial Distance algorithms.
"""
try:
rangefunc = xrange
except NameError:
rangefunc = range
def defaultAccessor(sequence, index, **kwargs):
"""
given a sequence of objects and an index, return a sequence
of 2 (2D) or 3 coordinates (3D) of the object.
Extraneous values are ignored.
Any extra kwargs to simplify() are passed to this function.
"""
# if 'argToAccessor' in kwargs:
# print(kwargs['argToAccessor'])
return sequence[index]
class Simplify(object):
def getSquareDistance2d(self, points, p1, p2, **kwargs):
"""
Square distance between two points (x,y)
"""
(p1_0, p1_1, *_) = self.get(points, p1, **kwargs)
(p2_0, p2_1, *_) = self.get(points, p2, **kwargs)
dx = p1_0 - p2_0
dy = p1_1 - p2_1
return dx * dx + dy * dy
def getSquareDistance3d(self, points, p1, p2, **kwargs):
"""
Square distance between two points (x,y,z)
"""
(p1_0, p1_1, p1_2, *_) = self.get(points, p1, **kwargs)
(p2_0, p2_1, p2_2, *_) = self.get(points, p2, **kwargs)
dx = p1_0 - p2_0
dy = p1_1 - p2_1
dz = p1_2 - p2_2
return dx * dx + dy * dy + dz * dz
def getSquareSegmentDistance2d(self, points, p, p1, p2, **kwargs):
"""
Square distance between point and a segment
"""
(x, y, *_) = self.get(points, p1, **kwargs)
(p2_0, p2_1, *_) = self.get(points, p2, **kwargs)
(p_0, p_1, *_) = self.get(points, p, **kwargs)
dx = p2_0 - x
dy = p2_1 - y
if dx or dy:
t = ((p_0 - x) * dx + (p_1 - y) * dy) / (dx * dx + dy * dy)
if t > 1:
x = p2_0
y = p2_1
elif t > 0:
x += dx * t
y += dy * t
dx = p_0 - x
dy = p_1 - y
return dx * dx + dy * dy
def getSquareSegmentDistance3d(self, points, p, p1, p2, **kwargs):
"""
Square distance between point and a segment
"""
(x, y, z, *_) = self.get(points, p1, **kwargs)
(p2_0, p2_1, p2_2, *_) = self.get(points, p2, **kwargs)
dx = p2_0 - x
dy = p2_1 - y
dz = p2_2 - z
(p_0, p_1, p_2, *_) = self.get(points, p, **kwargs)
if dx or dy:
t = ((p_0 - x) * dx + (p_1 - y) * dy + (p_2 - z) * dz) / (
dx * dx + dy * dy + dz * dz
)
if t > 1:
x = p2_0
y = p2_1
z = p2_2
elif t > 0:
x += dx * t
y += dy * t
z += dz * t
dx = p_0 - x
dy = p_1 - y
dz = p_2 - z
return dx * dx + dy * dy + dz * dz
def simplifyRadialDistance(self, points, point_range, tolerance, **kwargs):
first = point_range[0]
last = point_range[-1]
prev_point = 0
markers = [0]
for i in point_range: # rangefunc(first, last):
if self.getSquareDistance(points, i, prev_point, **kwargs) > tolerance:
markers.append(i)
prev_point = i
if prev_point != i:
markers.append(i)
return markers
def simplifyDouglasPeucker(self, points, point_range, tolerance, **kwargs):
first = point_range[0]
last = point_range[-1]
first_stack = []
last_stack = []
markers = [first, last]
while last:
max_sqdist = 0
for i in rangefunc(first, last):
sqdist = self.getSquareSegmentDistance(points, i, first, last, **kwargs)
if sqdist > max_sqdist:
index = i
max_sqdist = sqdist
if max_sqdist > tolerance:
markers.append(index)
first_stack.append(first)
last_stack.append(index)
first_stack.append(index)
last_stack.append(last)
# Can pop an empty array in Javascript, but not Python,
# so check the list first
first = first_stack.pop() if first_stack else None
last = last_stack.pop() if last_stack else None
markers.sort()
return markers
def simplify(
self, points, tolerance=0.1, highestQuality=True, returnMarkers=False, **kwargs
):
"""
Simplifies a sequence of points.
`points`: A sequences of objects containing coordinates in some shape or form. The algorithm requires
an accessor method as instantiation parameter. See the documentation of defaultAccessor for details.
`tolerance (optional, 0.1 by default)`: Affects the amount of simplification that occurs (the smaller, the less simplification).
`highestQuality (optional, True by default)`: Flag to exclude the distance pre-processing. Produces higher quality results, but runs slower.
`returnMarkers`: if set, return a list of ints denoting the sequence elements in the simplified list.
By default, return a list of objects taken from the original sequence.
`kwargs`: Any extra keyword arguments are passed to the accessor function.
"""
sqtolerance = tolerance * tolerance
markers = list(range(0, len(points)))
if not highestQuality:
markers = self.simplifyRadialDistance(
points, markers, sqtolerance, **kwargs
)
markers = self.simplifyDouglasPeucker(points, markers, sqtolerance, **kwargs)
if returnMarkers:
return markers
else:
return [points[i] for i in markers]
class Simplify3D(Simplify):
def __init__(self, accessor=defaultAccessor):
self.get = accessor
self.getSquareDistance = self.getSquareDistance3d
self.getSquareSegmentDistance = self.getSquareSegmentDistance3d
class Simplify2D(Simplify):
def __init__(self, accessor=defaultAccessor):
self.get = accessor
self.getSquareDistance = self.getSquareDistance2d
self.getSquareSegmentDistance = self.getSquareSegmentDistance2d
def featureAccessor(sequence, index, **kwargs):
"""
accessor for a FeatureCollection of point features
"""
return sequence[index].geometry.coordinates
if __name__ == "__main__":
tolerance = 0.01
highestQuality = False
import geojson
# contains a FeatureCollection of Points
fn = "radiosonde.geojson"
with open(fn, "r") as file:
s = file.read()
gj = geojson.loads(s.encode("utf8"))
# the data structure of the original code:
# a list of triplets (lat, lon, alt)
points = list()
i = 0
for f in gj.features:
coord = f.geometry.coordinates
coord.append(i)
points.append(coord)
i += 1
# this format can be handled by the default accessor
s = Simplify3D()
r = s.simplify(
points,
tolerance=tolerance,
highestQuality=highestQuality,
returnMarkers=True,
argToAccessor="demo",
) # passed to accessor
print(f"from {len(gj.features)} -> {len(r)}: markers={r}")
# now use a custom accessor to directly access
# the points of the FeatureCollection
s2 = Simplify3D(accessor=featureAccessor)
r = s2.simplify(
gj.features,
tolerance=tolerance,
highestQuality=highestQuality,
returnMarkers=False,
)
print(f"from {len(gj.features)} -> {len(r)}: points={r}")
import timeit
iterations = 10000
secs = timeit.timeit(
lambda: s2.simplify(
gj.features, tolerance=tolerance, highestQuality=highestQuality
),
number=iterations,
)
d = (secs / iterations) * 1e6
print(
f"time to simplify {len(gj.features)} points: {d:.0f} uS ({d/len(gj.features):.0f} uS/point)"
)