forked from Marginal/OverlayEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.py
320 lines (284 loc) · 15.2 KB
/
nodes.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from math import atan2, cos, hypot, radians, sin
import copy
from elevation import minres, round2res
# Basic node with no bezier control points. e.g. Forest. Also used for Draped orthos and unknown polygon types.
class Node:
def __init__(self, coords):
if isinstance(coords, Node): # being demoted
(self.lon, self.lat) = (coords.lon, coords.lat)
self.rest = []
else:
(self.lon, self.lat) = coords[:2]
self.rest = list(coords[2:])
self.loc = None # (x,y,z) in local coordinates
self.bezier = False
self.pointidx = 0
def clone(self):
return copy.copy(self) # can just do a shallow copy because our instance variables are immutable
def setloc(self, x, y, z):
if y is not None:
self.loc = (x, y, z)
else:
self.loc = (x, self.loc[1], z)
def move(self, dlat, dlon, tile):
if tile:
self.lon = max(tile[1], min(tile[1]+1, self.lon + dlon)) # points can be on upper/right boundary of tile
self.lat = max(tile[0], min(tile[0]+1, self.lat + dlat))
else: # don't round, e.g. if taking a copy
self.lon = self.lon + dlon
self.lat = self.lat + dlat
def rotate(self, dhdg, loc, tile):
assert loc and tile
(lat,lon) = loc
h = atan2(self.lon-lon, self.lat-lat) + radians(dhdg)
l = hypot(self.lon-lon, self.lat-lat)
self.lon = max(tile[1], min(tile[1]+1, round2res(lon + sin(h) * l)))
self.lat = max(tile[0], min(tile[0]+1, round2res(lat + cos(h) * l)))
def write(self, south, west):
# DSFTool rounds down, so round up here first
s = 'POLYGON_POINT\t%14.9f %14.9f' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4))
for p in self.rest:
s += ' %14.9f' % p
return s + '\n'
def coordcount(self):
return 2 + len(self.rest)
class BezierNode(Node):
def __init__(self, coords):
if isinstance(coords, Node): # being promoted
coords = [coords.lon, coords.lat] + coords.rest[-2:] # drop param if any
assert len(coords) in [2,4], coords
Node.__init__(self, coords[:2])
if len(coords)==2:
# Not actually a bezier node, but in a polygon where others are bezier nodes
self.bezlon = self.bezlat = self.bz2lon = self.bz2lat = 0
else:
assert len(coords)==4, coords
(self.lon, self.lat, self.bezlon, self.bezlat) = coords[:4]
self.bezlon -= self.lon
self.bezlat -= self.lat
self.bz2lon = -self.bezlon # backwards-pointing bezier is mirror of forwards, unless split
self.bz2lat = -self.bezlat
self.bezier = bool(self.bezlon or self.bezlat) # non-beziers are encoded by repeating the location
self.bezloc = self.bz2loc = None # (x,y,z) in local coordinates
self.split = False
def setbezloc(self, x, y, z):
if y is not None:
self.bezloc = (x, y, z)
else:
self.bezloc = (x, self.loc[1], z)
def setbz2loc(self, x, y, z):
if y is not None:
self.bz2loc = (x, y, z)
else:
self.bz2loc = (x, self.loc[1], z)
def swapbez(self):
# swap bezier points. Needed when order of nodes is reversed.
(a,b) = (self.bezlat,self.bezlon)
(self.bezlat,self.bezlon) = (self.bz2lat,self.bz2lon)
(self.bz2lat,self.bz2lon) = (a,b)
def rotate(self, dhdg, loc, tile):
Node.rotate(self, dhdg, loc, tile)
(lat,lon) = loc
if self.bezlon or self.bezlat:
h = atan2(self.bezlon, self.bezlat) + radians(dhdg)
l = hypot(self.bezlon, self.bezlat)
self.bezlon = round2res(sin(h) * l)
self.bezlat = round2res(cos(h) * l)
if not self.split:
self.bz2lon = -self.bezlon
self.bz2lat = -self.bezlat
elif self.bz2lon or self.bz2lat:
h = atan2(self.bz2lon, self.bz2lat) + radians(dhdg)
l = hypot(self.bz2lon, self.bz2lat)
self.bz2lon = round2res(sin(h) * l)
self.bz2lat = round2res(cos(h) * l)
def write(self, south, west):
# DSFTool rounds down, so round up here first
if not self.bezier:
return 'POLYGON_POINT\t%14.9f %14.9f %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4)) # repeat location in bezier fields
elif not self.split:
return 'POLYGON_POINT\t%14.9f %14.9f %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), min(west+1, self.lon+self.bezlon+minres/4), min(south+1, self.lat+self.bezlat+minres/4)) # standard forward-pointing bezier
else: # split bezier
s = 'POLYGON_POINT\t%14.9f %14.9f %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), min(west+1, self.lon-self.bz2lon + minres/4), min(south+1, self.lat-self.bz2lat + minres/4)) # mirror of backwards-pointing bezier
s+= 'POLYGON_POINT\t%14.9f %14.9f %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4)) # dummy - repeat location in bezier fields
s+= 'POLYGON_POINT\t%14.9f %14.9f %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), min(west+1, self.lon+self.bezlon+minres/4), min(south+1, self.lat+self.bezlat+minres/4)) # standard forward-pointing bezier
return s
def coordcount(self):
return 4
@classmethod
def fromNodes(cls, nodes):
# promote to a bezier polygon
newnodes = [[cls(node) for node in w] for w in nodes]
# detect split nodes
for nodes in newnodes:
j = 1
while j < len(nodes):
if j==1: # WED splits split node 0 across first and last
(a,b,c) = (nodes[-1],nodes[0],nodes[1])
else:
(a,b,c) = nodes[j-2:j+1]
if (a.lat == b.lat == c.lat) and (a.lon == b.lon == c.lon) and not b.bezier:
b.bezier = True
b.bz2lat = a.bz2lat
b.bz2lon = a.bz2lon
b.bezlat = c.bezlat
b.bezlon = c.bezlon
b.split = a.bezlat!=c.bezlat or a.bezlon!=c.bezlon # node sometimes encoded as split when it isn't
nodes.pop(j) # c
nodes.pop(j-2) # a
else:
j += 1
return newnodes
# Node with single integer parameter - v10 facade, v10 network
class ParamNode(Node):
def __init__(self, coords):
if isinstance(coords, Node): # being promoted
coords = [coords.lon, coords.lat] + coords.rest
assert len(coords) <= 3, coords
Node.__init__(self, coords[:2])
if len(coords)==3:
self.param = int(coords[2])
else:
assert len(coords)==2, coords # mismatch
self.param = 0
Node.__init__(self, coords)
def write(self, south, west):
# DSFTool rounds down, so round up here first
return 'POLYGON_POINT\t%14.9f %14.9f %5d\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param)
def coordcount(self):
return 3
# Node with single integer parameter - v10 facade, v10 network
class BezierParamNode(BezierNode,ParamNode):
def __init__(self, coords):
if isinstance(coords, ParamNode): # being promoted
coords = [coords.lon, coords.lat, coords.param]
elif isinstance(coords, Node):
coords = [coords.lon, coords.lat] + coords.rest
if len(coords) in [3,5]:
self.param = int(coords[2])
BezierNode.__init__(self, coords[:2]+coords[3:])
else:
assert len(coords) in [2,4], coords # mismatch
self.param = 0
BezierNode.__init__(self, coords)
def write(self, south, west):
# DSFTool rounds down, so round up here first
if not self.bezier:
return 'POLYGON_POINT\t%14.9f %14.9f %5d %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param, min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4)) # repeat location in bezier fields
elif not self.split:
return 'POLYGON_POINT\t%14.9f %14.9f %5d %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param, min(west+1, self.lon+self.bezlon+minres/4), min(south+1, self.lat+self.bezlat+minres/4)) # standard forward-pointing bezier
else: # split bezier
s = 'POLYGON_POINT\t%14.9f %14.9f %5d %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param, min(west+1, self.lon-self.bz2lon + minres/4), min(south+1, self.lat-self.bz2lat + minres/4)) # mirror of backwards-pointing bezier
s+= 'POLYGON_POINT\t%14.9f %14.9f %5d %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param, min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4)) # dummy - repeat location in bezier fields
s+= 'POLYGON_POINT\t%14.9f %14.9f %5d %14.9f %14.9f\n' % (min(west+1, self.lon+minres/4), min(south+1, self.lat+minres/4), self.param, min(west+1, self.lon+self.bezlon+minres/4), min(south+1, self.lat+self.bezlat+minres/4)) # standard forward-pointing bezier
return s
def coordcount(self):
return 5
class NetworkNode(BezierParamNode):
def __init__(self, coords):
if isinstance(coords, ParamNode): # being promoted
self.param = coords.param
BezierNode.__init__(self, [coords.lon, coords.lat])
elif isinstance(coords, Node): # being promoted
self.param = int(coords.rest[0])
BezierNode.__init__(self, [coords.lon, coords.lat])
else:
self.param = int(coords[2])
BezierNode.__init__(self, coords[:2])
def write(self, type_id, junction_id):
s = ''
if self.bezier and not type_id and (self.bz2lon or self.bz2lat): # Preceding Bezier (not at first node)
s += 'SHAPE_POINT\t\t\t%14.9f %14.9f %d\n' % (self.lon+self.bz2lon, self.lat+self.bz2lat, 1)
if type_id: # First
s += 'BEGIN_SEGMENT\t%d %d\t%d\t%14.9f %14.9f %d\n' % (0, type_id, junction_id, self.lon, self.lat, self.param)
elif junction_id: # Last
s += 'END_SEGMENT\t\t%d\t%14.9f %14.9f %d\n' % (junction_id, self.lon, self.lat, self.param)
else: # Mid
s += 'SHAPE_POINT\t\t\t%14.9f %14.9f %d\n' % (self.lon, self.lat, 0)
if self.bezier and (type_id or not junction_id) and (self.bezlon or self.bezlat): # Following Bezier (not at last node)
s += 'SHAPE_POINT\t\t\t%14.9f %14.9f %d\n' % (self.lon+self.bezlon, self.lat+self.bezlat, 1)
return s
def coordcount(self):
assert False # has no meaning here
return 3
@classmethod
def fromNodes(cls, nodes):
# promote to a bezier polygon
assert len(nodes)==1, nodes # network segments can't have holes
newnodes = [[NetworkNode(node) for node in w] for w in nodes]
# bezier control points are currently free-floating. Attach them to nodes, in pairs if possible..
for nodes in newnodes:
j = 1
while j < len(nodes)-1:
node = nodes[j]
if node.param: # bezier control point
prv = nodes[j-1]
if prv.bezier:
# previous node already a bezier, so this is a quadratic curve. attach control point to next node
nxt = nodes[j+1]
# assert not nxt.param
if j<len(nodes)-2 and nodes[j+2].param:
# next node will be a bezier anyway
nxt.param = 0
nxt.bezier = True
nxt.bezlat = nodes[j+2].lat - nxt.lat
nxt.bezlon = nodes[j+2].lon - nxt.lon
nxt.bz2lat = node.lat - nxt.lat
nxt.bz2lon = node.lon - nxt.lon
nxt.split = nxt.bezlat != -nxt.bz2lat or nxt.bezlon != -nxt.bz2lon
nodes.pop(j+2)
nodes.pop(j)
j += 1 # skip the node we've just turned into a bezier
elif j==len(nodes)-2:
# attach to last node, and don't split it
nxt.bezier = True
nxt.bz2lat = node.lat - nxt.lat
nxt.bz2lon = node.lon - nxt.lon
nxt.bezlat = -nxt.bz2lat
nxt.bezlon = -nxt.bz2lon
nxt.split = False
nodes.pop(j)
j += 1 # skip the node we've just turned into a bezier
else:
# ugly half-split node
nxt.bezier = True
nxt.bezlat = 0
nxt.bezlon = 0
nxt.bz2lat = node.lat - nxt.lat
nxt.bz2lon = node.lon - nxt.lon
nxt.split = True
nodes.pop(j)
j += 1 # skip the node we've just turned into a bezier
elif j<len(nodes)-2 and not nodes[j+1].param and nodes[j+2].param:
# next node will be a bezier so attach it to that
nxt = nodes[j+1]
nxt.bezier = True
nxt.bezlat = nodes[j+2].lat - nxt.lat
nxt.bezlon = nodes[j+2].lon - nxt.lon
nxt.bz2lat = node.lat - nxt.lat
nxt.bz2lon = node.lon - nxt.lon
nxt.split = nxt.bezlat != -nxt.bz2lat or nxt.bezlon != -nxt.bz2lon
nodes.pop(j+2)
nodes.pop(j)
j += 1 # skip the node we've just turned into a bezier
elif j==1:
# attach to first node, and don't split it
prv.bezier = True
prv.bezlat = node.lat - prv.lat
prv.bezlon = node.lon - prv.lon
prv.bz2lat = -prv.bezlat
prv.bz2lon = -prv.bezlon
prv.split = False
nodes.pop(j)
else:
# previous node wasn't a bezier - make it into an ugly half-split node
prv.bezier = True
prv.bezlat = node.lat - prv.lat
prv.bezlon = node.lon - prv.lon
prv.bz2lat = 0
prv.bz2lon = 0
prv.split = True
nodes.pop(j)
else:
j += 1
return newnodes