-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilinejoin.py
268 lines (218 loc) · 6.53 KB
/
multilinejoin.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
from qgis.core import *
import qgis.utils
import processing
from qgis.gui import QgsMessageBar
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Linepart:
_id = None
_points = []
_before = None
_after = None
_status = 0 # 0 = include and possibly connect, 1 = include as-is, never connect, 2 = dont include
def __init__(self, original_index, points):
assert(len(points) > 1)
self._id = original_index
self._points = points
if self._points[0] == self._points[-1]:
# one-part loop detected. this _can_ be ambiguous - a loop can never be connected to another line since
# which end should be connected?
self._status = 1
def maybe_connect_with(self, other):
assert(other is not self)
if self._status == 2 or other._status == 2:
return
p=self._points
op=other.points_start_to_end()
if p[-1] == op[0]:
if self._after or other._before:
# this is ambiguous
self._status = 2
other._status = 2
return
if self._status == 1 or other._status == 1:
self._status = 2
other._status = 2
return
self._after = other
other._before = self
if p[0] == op[-1]:
if self._before or other._after:
# this is ambiguous
self._status = 2
other._status = 2
return
if self._status == 1 or other._status == 1:
self._status = 2
other._status = 2
return
self._before = other
other._after = self
if p[-1] == op[-1]:
if self._after or other._after:
# this is ambiguous
self._status = 2
other._status = 2
return
if self._status == 1 or other._status == 1:
self._status = 2
other._status = 2
return
self._after = other
other._after = self
if p[0] == op[0]:
if self._before or other._before:
# this is ambiguous
self._status = 2
other._status = 2
return
if self._status == 1 or other._status == 1:
self._status = 2
other._status = 2
return
self._before = other
other._before = self
def traverse_from(self, frm):
if self._before == frm:
p=self.points_start_to_end()
return (self.shared_point_at_start(), p, self.shared_point_at_end(), self._after)
elif self._after == frm:
p=self.points_start_to_end()
p.reverse()
return (self.shared_point_at_end(), p, self.shared_point_at_start(), self._before)
else:
return ([], None)
def points_start_to_end(self):
px1 = 0
if self._before: px1 += 1
px2 = len(self._points)
if self._after: px2 -= 1
return self._points[px1:px2]
def shared_point_at_start(self):
if self._before:
return self._points[0]
else:
return None
def shared_point_at_end(self):
if self._after:
return self._points[-1]
else:
return None
def at_start(self):
return self._before
def at_end(self):
return self._after
def disabled(self):
return self._status == 2
def prnt(self):
print "linepart",self,"------------------"
print " id: ",self._id
print " status: ",self._status
print " before: ",self._before
print " after: ",self._after
print " n points: ",len(self._points)
print " first point: ",self._points[0]
print " last point: ",self._points[0]
print "----------------------------------"
class MultilinejoinBatch:
def __init__(self, iface):
self.iface=iface
def initGui(self):
self.action = QAction("Multiline Join", self.iface.mainWindow())
self.action.setObjectName("multiline-join")
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
self.iface.addPluginToVectorMenu("&Multiline Join", self.action)
def unload(self):
self.iface.removePluginVectorMenu("&Multiline Join", self.action)
def run(self):
layer=self.iface.activeLayer()
if layer and layer.dataProvider().capabilities() & QgsVectorDataProvider.ChangeGeometries:
parts_before_total = 0
parts_after_total = 0
parts_included_total = 0
done_anything=False
for feature in processing.features(layer):
geom = feature.geometry()
if geom.type() == QGis.Line and geom.isMultipart():
parts = geom.asMultiPolyline()
lineparts = []
for px in range(0, len(parts)):
if len(parts[px]) > 1:
lp = Linepart(px, parts[px])
lineparts.append(lp)
for px in range(0, len(lineparts)):
for px2 in range(px + 1, len(lineparts)):
lineparts[px].maybe_connect_with(lineparts[px2])
seen = {}
included = {}
new_parts = []
for pa in lineparts:
if pa in seen:
continue
if pa.disabled():
continue
seen[pa]=True
points = []
abort_part=False
pa_last=pa
pa_curr=pa.at_start()
while pa_curr:
if pa_curr == pa:
# multi-part loop detected. multi-part loops are ambiguous since
# where should the start/endpoint be?
#
# all parts in the loop are now marked
# as seen since we are back were we
# started and we will just abort.
abort_part=True
break
seen[pa_curr]=True
shpt1, p, shpt2, pa_next =pa_curr.traverse_from(pa_last)
p.reverse()
points[0:0] = p + [shpt1]
included[pa_curr]=True
pa_last=pa_curr
pa_curr=pa_next
if abort_part:
continue
points.extend(pa.points_start_to_end())
included[pa]=True
pa_last=pa
pa_curr=pa.at_end()
while pa_curr:
seen[pa_curr]=True
shpt1, p, shpt2, pa_next=pa_curr.traverse_from(pa_last)
points.extend([shpt1] + p)
included[pa_curr]=True
pa_last=pa_curr
pa_curr=pa_next
new_parts.append(points)
# print "feature", feature, "(",feature.id(),")-----"
# print "parts before:", len(parts)
# print "parts after:", len(new_parts)
# np=0
# for p in parts:
# np+=len(p)
# print "number of points before:", np
# np=0
# for p in new_parts:
# np+=len(p)
# print "number of points after:", np
# print "-----"
if len(parts) != len(new_parts):
if not done_anything:
done_anything=True
layer.beginEditCommand("Multiline Join")
g = QgsGeometry.fromMultiPolyline(new_parts)
layer.changeGeometry(feature.id(), g)
# layer.addFeatures([f])
parts_before_total += len(parts)
parts_after_total += len(new_parts)
parts_included_total += len(included)
if done_anything:
# print "actually did modify something"
layer.endEditCommand()
layer.triggerRepaint()
self.iface.messageBar().pushMessage("Multiline Join",
str(parts_included_total) + "/" + str(parts_before_total) + \
" parts -> " + str(parts_after_total) + " parts", level=QgsMessageBar.INFO)