-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker_class.py
427 lines (384 loc) · 18.4 KB
/
worker_class.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Lines Ranking
A QGIS plugin
-------------------
begin : 2020-07-07
copyright : (C) 2020 by Julia Borisova, Mikhail Sarafanov
email : [email protected], [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 qgis.gui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import time
import os, string
from qgis.core import *
from .preparation import *
from .graph_processing import overall_call
import processing
TMP_DIRECTORY_NAME = 'lines_ranking_tmp_directory'
class StartScript(QgsTask):
def __init__(self, desc, flags, selectedLayer, pt, cleanTresholdValue,
outLineEdit, QgsProject, fields_names):
QgsTask.__init__(self, desc, flags)
self.selectedLayer = selectedLayer
self.pt = pt
self.cleanTresholdValue = cleanTresholdValue
self.outLineEdit = outLineEdit
self.QgsProject = QgsProject
self.result = None
self.logText = ''
self.fields_names = fields_names
self.error_reason = ''
self.base_dir = os.path.abspath('.')
if 'C:' in self.base_dir:
# Generate temporary folder not in C disk
available_drives = ['%s:' % d for d in string.ascii_uppercase if
os.path.exists('%s:' % d)]
if len(available_drives) >= 2:
# There is an alternative:
available_drives = list(filter(lambda x: 'C' not in x, available_drives))
self.base_dir = os.path.join(available_drives[0], TMP_DIRECTORY_NAME)
self.create_base_directory_if_required()
self.attributes_file_path = os.path.join(self.base_dir, 'attributes_temp.csv')
self.points_file_path = os.path.join(self.base_dir, 'points_temp.csv')
self.original_file_path = os.path.join(self.base_dir, 'original_temp.csv')
# Set temporary files with intermediate results
# (needed in the case if input layer not in shp format)
self.tmp_after_fixing_geometries = os.path.join(self.base_dir,
'tmp_lines_ranking_after_fixing_geometries.gpkg')
self.tmp_after_vclean = os.path.join(self.base_dir, 'tmp_lines_ranking_after_vclean.gpkg')
self.remove_tmp_files()
def create_base_directory_if_required(self):
if os.path.exists(self.base_dir) is False:
os.mkdir(self.base_dir)
def remove_base_directory(self):
if len(os.listdir(self.base_dir)) < 1 and TMP_DIRECTORY_NAME in self.base_dir:
# Remove directory after finishing
os.rmdir(self.base_dir)
def remove_tmp_files(self):
if os.path.exists(self.tmp_after_fixing_geometries) is True:
os.remove(self.tmp_after_fixing_geometries)
if os.path.exists(self.tmp_after_vclean) is True:
os.remove(self.tmp_after_vclean)
def run(self):
try:
# By default - use intermediate layers
answer = self._run_ranking(use_intermediate_layers=True)
self.remove_tmp_files()
self.remove_base_directory()
return answer
except Exception as ex:
self.logText = 'Errors occurred. Re-run algorithm without saving intermediate layers'
try:
self.remove_tmp_files()
self.remove_base_directory()
except Exception as ex:
pass
answer = self._run_ranking(use_intermediate_layers=False)
return answer
def _fix_geometries_and_v_clean_with_temporary_files(self):
self.logText = 'Fixing geometries'
if os.path.exists(self.tmp_after_fixing_geometries) is True:
os.remove(self.tmp_after_fixing_geometries)
vl = fix_geometries(self.selectedLayer,
self.tmp_after_fixing_geometries)
if self.isCanceled():
return None, None
else:
if self.cleanTresholdValue.replace(' ', '') != '':
self.setProgress(5)
self.logText = 'Filling gaps (v.clean)'
try:
cleaned_layer_path = clean_gaps(vl, self.cleanTresholdValue,
self.tmp_after_vclean)
cleaned_layer = QgsVectorLayer(cleaned_layer_path,
'v_clean')
except Exception:
self.error_reason = 'grass'
self.logText = ''
return None, None
else:
vl = QgsVectorLayer(vl, 'fix')
pr = vl.dataProvider()
pr.addAttributes([QgsField("fid", QVariant.Int)])
vl.updateFields()
cleaned_layer = vl
buffer_layer = createCleanedBuffer(cleaned_layer)
buffer_layer.setProviderEncoding(u'UTF-8')
buffer_layer.dataProvider().setEncoding(u'UTF-8')
return cleaned_layer, buffer_layer
def _fix_geometries_and_v_clean_in_memory(self):
self.logText = 'Fixing geometries. Re-run'
vl = fix_geometries(self.selectedLayer, 'memory:fixed_geometry')
if self.isCanceled():
return None, None
else:
if self.cleanTresholdValue.replace(' ', '') != '':
self.setProgress(5)
self.logText = 'Filling gaps (v.clean). Re-run'
try:
cleaned_payer_path = clean_gaps(vl, self.cleanTresholdValue, 'TEMPORARY_OUTPUT')
cleaned_layer = QgsVectorLayer(cleaned_payer_path, 'v_clean')
except Exception:
self.error_reason = 'grass'
self.logText = ''
return None, None
else:
pr = vl.dataProvider()
pr.addAttributes([QgsField("fid", QVariant.Int)])
vl.updateFields()
cleaned_layer = vl
buffer_layer = createCleanedBuffer(cleaned_layer)
buffer_layer.setProviderEncoding(u'UTF-8')
buffer_layer.dataProvider().setEncoding(u'UTF-8')
return cleaned_layer, buffer_layer
def _run_ranking(self, use_intermediate_layers: bool = False):
self.create_base_directory_if_required()
response = False
cleaned_layer = None
try:
if use_intermediate_layers is True:
cleaned_layer, buffer_layer = self._fix_geometries_and_v_clean_with_temporary_files()
else:
# All layers will be processed in memory
cleaned_layer, buffer_layer = self._fix_geometries_and_v_clean_in_memory()
if cleaned_layer is None:
return False
if self.isCanceled():
return False
else:
self.setProgress(15)
self.logText = 'Clear attribute fields'
cleaned_layer.startEditing()
count = 0
del_list = []
for field in cleaned_layer.dataProvider().fields():
if field.name() != 'fid':
del_list.append(count)
count += 1
cleaned_layer.dataProvider().deleteAttributes(del_list)
cleaned_layer.updateFields()
cleaned_layer.commitChanges()
if self.isCanceled():
return False
else:
self.setProgress(20)
self.logText = 'Create lines segments'
clip_output = clip_line_to_segment(cleaned_layer)
clipped_vector_layer = clip_output[0]
if self.isCanceled():
return False
else:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "CSV"
options.fileEncoding = "utf-8"
QgsVectorFileWriter.writeAsVectorFormatV2(clip_output[1], self.attributes_file_path,
QgsCoordinateTransformContext(),
options)
if self.isCanceled():
return False
else:
clipped_vector_layer.startEditing()
self.setProgress(25)
self.logText = 'Creating "fid" attributes'
all_f = clipped_vector_layer.featureCount()
for feature in clipped_vector_layer.getFeatures():
progress = 25 + (int(feature.id())*10) / all_f
self.setProgress(progress)
if self.isCanceled():
break
else:
if feature.geometry().wkbType() == 0:
clipped_vector_layer.deleteFeature(feature.id())
else:
feature['fid'] = feature.id()
clipped_vector_layer.updateFeature(feature)
clipped_vector_layer.commitChanges()
self.setProgress(36)
self.logText = 'Calculating lines intersections'
if self.isCanceled():
return False
else:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "CSV"
options.fileEncoding = "utf-8"
QgsVectorFileWriter.writeAsVectorFormatV2(clipped_vector_layer,
self.original_file_path,
QgsCoordinateTransformContext(),
options)
if self.isCanceled():
return False
else:
intersect_point_layer = get_lines_intersections(clipped_vector_layer,
self.setProgress)
if self.isCanceled():
return False
else:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "CSV"
options.fileEncoding = "utf-8"
QgsVectorFileWriter.writeAsVectorFormatV2(intersect_point_layer,
self.points_file_path,
QgsCoordinateTransformContext(),
options)
if self.isCanceled():
return False
else:
self.setProgress(47)
self.logText = 'Get nearest segment id'
start_segment_id = get_nearest_segmentId(clipped_vector_layer,
self.pt, self.setProgress)
if self.isCanceled():
return False
else:
self.setProgress(58)
self.logText = 'Graph traversal ranking'
out_dataset = overall_call(self.original_file_path,
self.points_file_path,
start_segment_id,
self.attributes_file_path,
self.setProgress)
os.remove(self.points_file_path)
os.remove(self.original_file_path)
os.remove(self.attributes_file_path)
clipped_vector_layer.selectAll()
outLayer = processing.run("native:saveselectedfeatures",
{'INPUT': clipped_vector_layer, 'OUTPUT': 'memory:rank_output'})['OUTPUT']
if self.isCanceled():
return False
else:
self.setProgress(89)
self.logText = 'Creating attributes table'
pr = outLayer.dataProvider()
for field_name in self.fields_names:
pr.addAttributes([QgsField(field_name, QVariant.Int)])
outLayer.updateFields()
outLayer.startEditing()
all_f = outLayer.featureCount()
for feature in outLayer.getFeatures():
progress = 89 + (int(feature.id())*10)/all_f
self.setProgress(progress)
try:
if self.isCanceled():
break
else:
f_id = feature['fid']
rank = out_dataset[f_id][0]
value_shreve = out_dataset[f_id][1]
value_strahler = out_dataset[f_id][2]
distance = out_dataset[f_id][3]
try:
feature[self.fields_names[0]] = rank
except Exception:
pass
try:
feature[self.fields_names[1]] = value_shreve
except Exception:
pass
try:
feature[self.fields_names[2]] = value_strahler
except Exception:
pass
try:
feature[self.fields_names[3]] = distance
except Exception:
pass
outLayer.updateFeature(feature)
except Exception as e:
outLayer.deleteFeature(feature.id())
outLayer.commitChanges()
createSpatialIndex(outLayer)
outLayerWithAttr = joinAttributes(outLayer, buffer_layer, self.fields_names)
outLayerWithAttr.setProviderEncoding(u'UTF-8')
outLayerWithAttr.dataProvider().setEncoding(u'UTF-8')
if self.isCanceled():
return False
else:
if self.outLineEdit.replace(' ', '') != '':
if self.outLineEdit[-5:] != '.gpkg':
self.outLineEdit = self.outLineEdit + '.gpkg'
try:
QgsVectorFileWriter.writeAsVectorFormat(layer=outLayerWithAttr,
fileName=self.outLineEdit,
fileEncoding="utf-8",
driverName="GPKG")
self.result = QgsVectorLayer(self.outLineEdit, self.outLineEdit.split('/')[-1][:-5], "ogr")
except Exception as e:
pass
else:
self.result = outLayerWithAttr
if self.isCanceled():
if use_intermediate_layers is True:
del cleaned_layer
self.remove_tmp_files()
self.remove_base_directory()
return False
else:
if use_intermediate_layers is False:
del cleaned_layer
self.remove_tmp_files()
self.remove_base_directory()
return True
except Exception as ex:
del cleaned_layer
self.remove_tmp_files()
self.remove_base_directory()
raise ex
def cancel(self):
super().cancel()
def finished(self, result):
self.setProgress(0)
self.logText = ''
if result is False:
try:
os.remove(self.points_file_path)
os.remove(self.original_file_path)
os.remove(self.attributes_file_path)
except Exception:
pass
if self.error_reason == 'grass':
QMessageBox.critical(None, "Error", 'GRASS modules are not available. Activate GRASS or leave snapping threshold empty')
if result is True:
QMessageBox.information(None, "Success", 'Lines Ranking process is finished')
class Worker(QObject):
def __init__(self, qapp, selectedLayer, pt, cleanTresholdValue, outLineEdit, QgsProject, prBar, logTxtLine, fields_names):
self.qapp = qapp
self.selectedLayer = selectedLayer
self.pt = pt
self.cleanTresholdValue = cleanTresholdValue
self.outLineEdit = outLineEdit
self.QgsProject = QgsProject
self.result = None
self.progress = prBar
self.logTxtLine = logTxtLine
self.fields_names = fields_names
def setResult(self, r):
QgsProject.instance().addMapLayer(r)
self.progress = 0
def progressSet(self, progress, logText):
self.logTxtLine.setText(logText)
if isinstance(progress, float):
# Current value must be integer
progress = int(round(progress))
self.progress.setValue(progress)
def StartScriptTask(self):
self.task = StartScript('Lines Ranking processing', QgsTask.CanCancel,
self.selectedLayer, self.pt, self.cleanTresholdValue,
self.outLineEdit, self.QgsProject, self.fields_names)
self.task.progressChanged.connect(lambda: self.progressSet(self.task.progress(), self.task.logText))
self.task.taskCompleted.connect(lambda: self.setResult(self.task.result))
self.qapp.taskManager().addTask(self.task)
def cancelTask(self):
self.task.cancel()
QMessageBox.critical(None, "Terminating", 'Lines Ranking process was canceled')