/
road_network_cleaner_tool.py
373 lines (304 loc) · 14.3 KB
/
road_network_cleaner_tool.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
"""
/***************************************************************************
RoadNetworkCleaner
A QGIS plugin
This plugin clean a road centre line map.
-------------------
begin : 2016-11-10
git sha : $Format:%H$
copyright : (C) 2016 by Space SyntaxLtd
email : i.kolovou@spacesyntax.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import Qt, QThread, QSettings
from qgis.core import *
from qgis.gui import *
from qgis.utils import *
#import db_manager.db_plugins.postgis.connector as con
import traceback
# Initialize Qt resources from file resources.py
import resources
# the dialog modules
from road_network_cleaner_dialog import RoadNetworkCleanerDialog
# additional modules
from sGraph.break_tools import * # better give these a name to make it explicit to which module the methods belong
from sGraph.merge_tools import *
from sGraph.utilityFunctions import *
# Import the debug library - required for the cleaning class in separate thread
# set is_debug to False in release version
is_debug = False
try:
import pydevd
has_pydevd = True
except ImportError, e:
has_pydevd = False
is_debug = False
class NetworkCleanerTool(QObject):
# initialise class with self and iface
def __init__(self, iface):
QObject.__init__(self)
self.iface=iface
self.legend = self.iface.legendInterface()
# load the dialog from the run method otherwise the objects gets created multiple times
self.dlg = None
# some globals
self.cleaning = None
self.thread = None
def loadGUI(self):
# create the dialog objects
self.dlg = RoadNetworkCleanerDialog(self.getQGISDbs())
# setup GUI signals
self.dlg.closingPlugin.connect(self.unloadGUI)
self.dlg.cleanButton.clicked.connect(self.startCleaning)
self.dlg.cancelButton.clicked.connect(self.killCleaning)
# add layers to dialog
self.updateLayers()
# setup legend interface signals
self.legend.itemAdded.connect(self.updateLayers)
self.legend.itemRemoved.connect(self.updateLayers)
self.settings = None
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
def unloadGUI(self):
if self.dlg:
self.dlg.closingPlugin.disconnect(self.unloadGUI)
self.dlg.cleanButton.clicked.disconnect(self.startCleaning)
self.dlg.cancelButton.clicked.disconnect(self.killCleaning)
self.settings = None
try:
self.legend.itemAdded.disconnect(self.updateLayers)
self.legend.itemRemoved.disconnect(self.updateLayers)
except TypeError:
pass
self.dlg = None
def getQGISDbs(self):
"""Return all PostGIS connection settings stored in QGIS
:return: connection dict() with name and other settings
"""
con_settings = []
settings = QSettings()
settings.beginGroup('/PostgreSQL/connections')
for item in settings.childGroups():
con = dict()
con['name'] = unicode(item)
con['host'] = unicode(settings.value(u'%s/host' % unicode(item)))
con['port'] = unicode(settings.value(u'%s/port' % unicode(item)))
con['database'] = unicode(settings.value(u'%s/database' % unicode(item)))
con['username'] = unicode(settings.value(u'%s/username' % unicode(item)))
con['password'] = unicode(settings.value(u'%s/password' % unicode(item)))
con_settings.append(con)
settings.endGroup()
dbs = {}
if len(con_settings) > 0:
for conn in con_settings:
dbs[conn['name']]= conn
return dbs
def getActiveLayers(self):
layers_list = []
for layer in self.iface.legendInterface().layers():
if layer.isValid() and layer.type() == QgsMapLayer.VectorLayer:
if layer.hasGeometryType() and (layer.geometryType() == 1):
layers_list.append(layer.name())
return layers_list
def updateLayers(self):
layers = self.getActiveLayers()
self.dlg.popActiveLayers(layers)
# SOURCE: Network Segmenter https://github.com/OpenDigitalWorks/NetworkSegmenter
# SOURCE: https://snorfalorpagus.net/blog/2013/12/07/multithreading-in-qgis-python-plugins/
def giveMessage(self, message, level):
# Gives warning according to message
self.iface.messageBar().pushMessage("Road network cleaner: ", "%s" % (message), level, duration=5)
def cleaningError(self, e, exception_string):
# Gives error according to message
QgsMessageLog.logMessage('Cleaning thread raised an exception: %s' % exception_string, level=QgsMessageLog.CRITICAL)
self.dlg.close()
def startCleaning(self):
self.dlg.cleaningProgress.reset()
self.settings = self.dlg.get_settings()
if self.settings['output_type'] == 'postgis':
db_settings = self.dlg.get_dbsettings()
self.settings.update(db_settings)
if self.settings['input']:
cleaning = self.clean(self.settings, self.iface)
# start the cleaning in a new thread
thread = QThread()
cleaning.moveToThread(thread)
cleaning.finished.connect(self.cleaningFinished)
cleaning.error.connect(self.cleaningError)
cleaning.warning.connect(self.giveMessage)
cleaning.cl_progress.connect(self.dlg.cleaningProgress.setValue)
thread.started.connect(cleaning.run)
# thread.finished.connect(self.cleaningFinished)
self.thread = thread
self.cleaning = cleaning
self.thread.start()
if is_debug: print 'started'
else:
self.giveMessage('Missing user input!', QgsMessageBar.INFO)
return
def cleaningFinished(self, ret):
if is_debug: print 'trying to finish'
# get cleaning settings
layer_name = self.settings['input']
path = self.settings['output']
output_type = self.settings['output_type']
# get settings from layer
layer = getLayerByName(layer_name)
crs = layer.dataProvider().crs()
encoding = layer.dataProvider().encoding()
geom_type = layer.dataProvider().geometryType()
# create the cleaning results layers
try:
# create clean layer
if output_type == 'shp':
final = to_shp(path, ret[0][0], ret[0][1], crs, 'cleaned', encoding, geom_type)
elif output_type == 'memory':
final = to_shp(None, ret[0][0], ret[0][1], crs, path, encoding, geom_type)
else:
final = to_dblayer(self.settings['dbname'], self.settings['user'], self.settings['host'],
self.settings['port'], self.settings['password'], self.settings['schema'],
self.settings['table_name'], ret[0][1], ret[0][0], crs)
if final:
QgsMapLayerRegistry.instance().addMapLayer(final)
final.updateExtents()
# create errors layer
if self.settings['errors']:
errors = to_shp(None, ret[1][0], ret[1][1], crs, 'errors', encoding, geom_type)
if errors:
QgsMapLayerRegistry.instance().addMapLayer(errors)
errors.updateExtents()
# create unlinks layer
if self.settings['unlinks']:
unlinks = to_shp(None, ret[2][0], ret[2][1], crs, 'unlinks', encoding, 0)
if unlinks:
QgsMapLayerRegistry.instance().addMapLayer(unlinks)
unlinks.updateExtents()
self.iface.mapCanvas().refresh()
self.giveMessage('Process ended successfully!', QgsMessageBar.INFO)
except Exception, e:
# notify the user that sth went wrong
self.cleaning.error.emit(e, traceback.format_exc())
self.giveMessage('Something went wrong! See the message log for more information', QgsMessageBar.CRITICAL)
# clean up the worker and thread
#self.cleaning.deleteLater()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
if is_debug: print 'thread running ', self.thread.isRunning()
if is_debug: print 'has finished ', self.thread.isFinished()
self.thread = None
self.cleaning = None
if self.dlg:
self.dlg.cleaningProgress.reset()
self.dlg.close()
def killCleaning(self):
if is_debug: print 'trying to cancel'
# add emit signal to breakTool or mergeTool only to stop the loop
if self.cleaning:
try:
dummy = self.cleaning.br
del dummy
self.cleaning.br.killed = True
except AttributeError:
pass
try:
dummy = self.cleaning.mrg
del dummy
self.cleaning.mrg.killed = True
except AttributeError:
pass
# Disconnect signals
self.cleaning.finished.disconnect(self.cleaningFinished)
self.cleaning.error.disconnect(self.cleaningError)
self.cleaning.warning.disconnect(self.giveMessage)
self.cleaning.cl_progress.disconnect(self.dlg.cleaningProgress.setValue)
# Clean up thread and analysis
self.cleaning.kill()
self.cleaning.deleteLater()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
self.cleaning = None
self.dlg.cleaningProgress.reset()
self.dlg.close()
else:
self.dlg.close()
# SOURCE: https://snorfalorpagus.net/blog/2013/12/07/multithreading-in-qgis-python-plugins/
class clean(QObject):
# Setup signals
finished = pyqtSignal(object)
error = pyqtSignal(Exception, basestring)
cl_progress = pyqtSignal(float)
warning = pyqtSignal(str)
cl_killed = pyqtSignal(bool)
def __init__(self, settings, iface):
QObject.__init__(self)
self.settings = settings
self.iface = iface
self.total =0
def add_step(self,step):
self.total += step
return self.total
def run(self):
if has_pydevd and is_debug:
pydevd.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True, suspend=False)
ret = None
if self.settings:
try:
# cleaning settings
layer_name = self.settings['input']
tolerance = self.settings['tolerance']
# project settings
layer = getLayerByName(layer_name)
self.cl_progress.emit(2)
self.br = breakTool(layer, tolerance, None, self.settings['errors'], self.settings['unlinks'])
if self.cl_killed is True or self.br.killed is True: return
self.br.add_edges()
if self.cl_killed is True or self.br.killed is True: return
self.cl_progress.emit(5)
self.total = 5
step = 40/ self.br.feat_count
self.br.progress.connect(lambda incr=self.add_step(step): self.cl_progress.emit(incr))
broken_features = self.br.break_features()
if self.cl_killed is True or self.br.killed is True: return
self.cl_progress.emit(45)
self.mrg = mergeTool(broken_features, None, True)
# TODO test
try:
step = 40/ len(self.mrg.con_1)
self.mrg.progress.connect(lambda incr=self.add_step(step): self.cl_progress.emit(incr))
except ZeroDivisionError:
pass
merged_features = self.mrg.merge()
if self.cl_killed is True or self.mrg.killed is True: return
fields = self.br.layer_fields
# prepare other output data
((errors_list, errors_fields), (unlinks_list, unlinks_fields)) = ((None, None), (None, None))
if self.settings['errors']:
self.br.updateErrors(self.mrg.errors_features)
errors_list = [[k, [[k], [v[0]]], v[1]] for k, v in self.br.errors_features.items()]
errors_fields = [QgsField('id_input', QVariant.Int), QgsField('errors', QVariant.String)]
if self.settings['unlinks']:
unlinks_list = self.br.unlinked_features
unlinks_fields = [QgsField('id', QVariant.Int), QgsField('line_id1', QVariant.Int), QgsField('line_id2', QVariant.Int), QgsField('x', QVariant.Double), QgsField('y', QVariant.Double)]
if is_debug: print "survived!"
self.cl_progress.emit(100)
# return cleaned data, errors and unlinks
ret = ((merged_features, fields), (errors_list, errors_fields), (unlinks_list, unlinks_fields))
except Exception, e:
# forward the exception upstream
self.error.emit(e, traceback.format_exc())
self.finished.emit(ret)
def kill(self):
self.cl_killed = True