-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicworker.py
692 lines (601 loc) · 27.1 KB
/
icworker.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# -*- coding: utf-8 -*-
from PyQt5.QtCore import (QSettings, QTranslator, qVersion, QCoreApplication,
QObject, QThread, pyqtRemoveInputHook, pyqtSignal,
QVariant)
from PyQt5.QtGui import QIcon, QDoubleValidator, QColor
from PyQt5.QtWidgets import QAction
from qgis.core import *
from qgis.gui import *
import os.path
import processing
from osgeo import ogr, osr
from math import isnan
import time
MESSAGE_CATEGORY = 'InterfaceCatchment'
class ICWorker(QgsTask):
"""This shows how to subclass QgsTask"""
# layerPrint = pyqtSignal('QgsMapLayerType', str, str, dict)
layerPrint = pyqtSignal(QgsVectorLayer, dict)
# logSignal = pyqtSignal(str)
def __init__(self, parent, description):
super().__init__(description, QgsTask.CanCancel)
self.exception = None
self.parent = parent
def log(self, message: str, level=Qgis.Info):
QgsMessageLog.logMessage(message, MESSAGE_CATEGORY, level=level)
def run(self):
"""Here you implement your heavy lifting.
Should periodically test for isCanceled() to gracefully
abort.
This method MUST return True or False.
Raising exceptions will crash QGIS, so we handle them
internally and raise them in self.finished
"""
self.icWorkerContext = QgsProcessingContext()
# read the GUI parameters at run time
blocks_layer = self.parent.dlg.mMapLayerComboBox.currentLayer()
starting_point_layer = self.parent.dlg.mMapLayerComboBox_2.currentLayer()
walking_distance = self.parent.dlg.mQgsDoubleSpinBox.value()
deadend_solution = self.parent.dlg.checkBox.checkState()
buffering_distance = self.parent.dlg.mQgsDoubleSpinBox_2.value()/2
x_coordinate = float(self.parent.dlg.lineEdit.text())
y_coordinate = float(self.parent.dlg.lineEdit_2.text())
project_crs = self.parent.canvas.mapSettings().destinationCrs().authid()
self.log('Started task "%s"' %self.description())
# abort parameter for stopping function in loop execution
# debug parameter for enabling or disabling the debugging with vscode
__debug = False
starttime = time.time()
# Preliminary fix the blocks layer
blocks_layer = processing.run(
'qgis:fixgeometries',
{
'INPUT': blocks_layer,
'OUTPUT': 'memory:',
}
)['OUTPUT']
# REPROJECT both blocks and sp layers
blocks_layer = processing.run(
'qgis:reprojectlayer',
{
'INPUT': blocks_layer,
'TARGET_CRS': project_crs,
'OUTPUT': 'memory:',
}
)['OUTPUT']
if starting_point_layer:
starting_point_layer = processing.run(
'qgis:reprojectlayer',
{
'INPUT': starting_point_layer,
'TARGET_CRS': project_crs,
'OUTPUT': 'memory:',
}
)['OUTPUT']
#############################################################################################
# HANDLING THE STARTING POINT COORDINATES
# Create a new layer that will hold the starting point
starting_point_layer = QgsVectorLayer('Point?crs='+project_crs,
'IC_starting_point' , "memory")
# get the data provider of the starting point layer
provider = starting_point_layer.dataProvider()
with edit(starting_point_layer):
# create the geometry of the starting point
starting_pointXY = QgsPointXY(x_coordinate, y_coordinate)
point_geom = QgsGeometry.fromPointXY(starting_pointXY)
# Create a new feature with the geometry of the
# block and the id, perimeter and area attributes
feature = QgsFeature()
feature.setGeometry(point_geom)
feature.setId(1)
provider.addFeatures( [feature] )
starting_point_layer.updateFields()
# This is where I make a buffer around the starting point, the size of
# a given walking distance
walking_buffer_layer = processing.run(
'qgis:buffer',
{
'INPUT' : starting_point_layer,
'END_CAP_STYLE' : 0,
'OUTPUT' : 'memory:buffer',
'SEGMENTS' : 360,
'MITER_LIMIT' : 2,
'DISTANCE' : walking_distance,
'JOIN_STYLE' : 0,
'DISSOLVE' : False
}
)['OUTPUT']
# this is where I need to select from the blocks layer only those features which intersect with the buffer created above
blocks_layer = processing.run(
'qgis:extractbylocation',
{
'INPUT' : blocks_layer,
'PREDICATE' : [0],
'INTERSECT' : walking_buffer_layer,
'OUTPUT' : 'memory:',
}
)['OUTPUT']
if self.isCanceled():
return False
###################################################################
####### HANDLING THE BLOCKS LAYER
# check if the blocks layer == lines
if blocks_layer.geometryType() == 1:
blocks_layer = processing.run(
'qgis:linestopolygons',
{
'INPUT': blocks_layer,
'OUTPUT': 'memory:',
}
)['OUTPUT']
if self.isCanceled():
return False
# fix the polygons by using the fix geometries processing
# algorithm
fixedgeometries = processing.run(
'qgis:fixgeometries',
{
'INPUT': blocks_layer,
'OUTPUT': 'memory:',
}
)['OUTPUT']
if self.isCanceled():
return False
# dissolve the created polygons in order to make touching polygons
# into one block
pathdissolve = processing.run(
'qgis:dissolve',
{
'INPUT': fixedgeometries,
'OUTPUT': 'memory:',
}
)['OUTPUT']
if self.isCanceled():
return False
# convert dissolve result from multipart to singleparts
blocks_layer = processing.run(
'qgis:multiparttosingleparts',
{
'INPUT': pathdissolve,
'OUTPUT': 'memory:',
}
)['OUTPUT']
if self.isCanceled():
return False
# see if the deadend solution has been selected when the plugin was run
# if true, run the whole buffering solution for removing deadends from
# the results
if deadend_solution:
# first I need to buffer out the blocks by the given distance
# amount
buffer_out_layer = processing.run(
'qgis:buffer',
{
'INPUT' : blocks_layer,
'END_CAP_STYLE' : 1,
'OUTPUT' : 'memory:',
'SEGMENTS' : 5,
'MITER_LIMIT' : 2,
'DISTANCE' : buffering_distance,
'JOIN_STYLE' : 1,
'DISSOLVE' : False
}
)['OUTPUT']
if self.isCanceled():
return False
# then I need to buffer back in by the same distance amount
buffer_in_layer = processing.run(
'qgis:buffer',
{
'INPUT' : buffer_out_layer,
'END_CAP_STYLE' : 1,
'OUTPUT' : 'memory:',
'SEGMENTS' : 5,
'MITER_LIMIT' : 2,
'DISTANCE' : -buffering_distance,
'JOIN_STYLE' : 1,
'DISSOLVE' : False
}
)['OUTPUT']
# finally, fill in any wholes that are left in the blocks
blocks_layer = processing.run(
'qgis:deleteholes',
{
'INPUT' : buffer_in_layer,
'MIN_AREA' : 0,
'OUTPUT' : 'memory:',
}
)['OUTPUT']
# Create a new layer that will hold all the blocks
final_blocks_layer = QgsVectorLayer('Polygon?crs='+project_crs, 'IC_blocks' , "memory")
# get the data provider of the blocks layer
provider = final_blocks_layer.dataProvider()
i = 1
with edit(final_blocks_layer):
for block in blocks_layer.getFeatures():
block_geom = block.geometry()
# Create a new feature with the geometry of the
# block and the id, perimeter and area attributes
feature = QgsFeature()
feature.setGeometry(block_geom)
feature.setId(i)
provider.addFeatures( [feature] )
i += 1
final_blocks_layer.updateFields()
if self.isCanceled():
return False
##########################################
# This is where I extract the boundary of the blocks layer in order to
# work out the vertices
blocks_boundary_layer = processing.run(
'qgis:boundary',
{
'INPUT' : blocks_layer,
'OUTPUT' : 'memory:boundary',
}
)['OUTPUT']
if self.isCanceled():
return False
# This is where I clip the boundary layer with the walking distance
# buffer
clipped_boundary_layer = processing.run(
'qgis:clip',
{
'INPUT' : blocks_boundary_layer,
'OVERLAY' : walking_buffer_layer,
'OUTPUT' : 'memory:clip1',
}
)['OUTPUT']
if self.isCanceled():
return False
clipped_boundary_layer = processing.run(
'qgis:advancedpythonfieldcalculator',
{
'INPUT' : clipped_boundary_layer,
'OUTPUT' : 'memory:clip2',
'FIELD_LENGTH' : 10,
'GLOBAL' : '',
'FIELD_TYPE' : 0,
'FIELD_NAME' : 'ic_boundary_id',
'FORMULA' : 'value = $id',
'FIELD_PRECISION' : 1,
}
)['OUTPUT']
if self.isCanceled():
return False
# This is where I dissolve the blocks layer
dissolved_blocks_layer = processing.run(
'qgis:dissolve',
{
'INPUT' : blocks_layer,
'OUTPUT' : 'memory:dissolve',
}
)['OUTPUT']
############################################################################
######## This is where I will test if the points are visible from the starting
# Create a new layer that will hold all the lines representing the
# walkable portions of the boundaries
lines_layer = QgsVectorLayer('Polygon?crs='+project_crs,
'lines' , "memory")
# get the data provider of the lines layer
lines_provider = lines_layer.dataProvider()
lines_provider.addAttributes(
[
QgsField('boundary_id', QVariant.Int),
]
)
lines_layer.updateFields()
lines_boundary_id = lines_provider.fieldNameIndex('boundary_id')
# Create a new layer that will hold all the resulting points
new_vertices_layer = QgsVectorLayer('Point?crs='+project_crs,
'IC_vertices' , "memory")
# get the data provider of the new vertices layer
provider = new_vertices_layer.dataProvider()
provider.addAttributes(
[
QgsField('iteration', QVariant.Int),
QgsField('prev_id', QVariant.Int),
QgsField('distance', QVariant.Double),
QgsField('boundary_id', QVariant.Int),
]
)
# Tell vector layer to update fields in order to get the new layers
# from data provider
new_vertices_layer.updateFields()
distance_id = provider.fieldNameIndex('distance')
iteration_id = provider.fieldNameIndex('iteration')
prev_id_id = provider.fieldNameIndex('prev_id')
boundary_id_id = provider.fieldNameIndex('boundary_id')
fields = new_vertices_layer.fields()
iteration = 1
# Get the feature where all the blocks are dissolved into a single
# feature
for f in dissolved_blocks_layer.getFeatures():
# I have to buffer in the blocks by a small amount because of
# the floating point error on the newly created points
blocks_geom = f.geometry().buffer(-0.05, 360)
# First I need to take in the layer with existing points
for starting_point in starting_point_layer.getFeatures():
# take the geometry of the starting point
sp_geom = starting_point.geometry()
sp_aspoint = sp_geom.asPoint()
for boundary in clipped_boundary_layer.getFeatures():
boundary_geom = boundary.geometry()
boundary_id = boundary['ic_boundary_id']
boundary_geom.convertToMultiType()
intersected_boundaries = boundary_geom.asMultiPolyline()
writepoints_list = []
boundary_points = [sp_aspoint]
for boundary_line in intersected_boundaries:
for p_geom in boundary_line:
# create a line between the starting point and the vertice
# point
if self.isCanceled():
return False
line = QgsLineString(
[p_geom.x(), sp_aspoint.x()],
[p_geom.y(), sp_aspoint.y()]
)
line_geom = QgsGeometry(line)
if not line_geom.crosses(blocks_geom):
distance = walking_distance - line_geom.length()
pointarea = QgsRectangle(p_geom.x() - 0.005,p_geom.y() - 0.005,
p_geom.x() + 0.005,p_geom.y() + 0.005)
writepoint = True
for point in new_vertices_layer.getFeatures(
QgsFeatureRequest().setFilterRect(pointarea)):
if distance > point['distance']:
provider.deleteFeatures( [point.id()] )
new_vertices_layer.updateFields()
else:
writepoint = False
if writepoint:
feature = QgsFeature(fields)
feature.setGeometry(QgsGeometry(QgsPoint(p_geom)))
feature[distance_id] = distance
feature[iteration_id] = iteration
feature[boundary_id_id] = boundary_id
provider.addFeatures( [feature] )
new_vertices_layer.updateFields()
boundary_points.append(p_geom)
newlines = []
for j, p1_geom in enumerate(boundary_points):
for k, p2_geom in enumerate(boundary_points[j+1:]):
if self.isCanceled():
return False
line = QgsGeometry(
QgsLineString(
[p1_geom.x(), p2_geom.x()],
[p1_geom.y(), p2_geom.y()]
)
)
if line.within(boundary_geom.buffer(0.1, 5)):
# newlines.append(line.buffer(0.01, 5, 2, 2, 2))
newlines.append(line.buffer(0.01, 5))
if newlines:
feature = QgsFeature(lines_layer.fields())
walkable_line_geometry = QgsGeometry().unaryUnion(newlines)
boundary_geom = boundary_geom.difference(walkable_line_geometry)
feature.setGeometry(walkable_line_geometry)
feature.setId(boundary_id)
feature[lines_boundary_id] = boundary_id
lines_provider.addFeatures( [feature] )
lines_layer.updateFields()
iteration += 1
if __debug:
self.log('itaration = %s' %iteration)
while any( new_vertices_layer.getFeatures( '"iteration" = %s AND "distance" > 0.001' % (iteration-1))):
for boundary in clipped_boundary_layer.getFeatures():
if self.isCanceled():
return False
boundary_geom = boundary.geometry()
boundary_id = boundary['ic_boundary_id']
writepoints_list = []
# newlines = []
newlines = QgsGeometry()
existing_line_features = [l for l in lines_layer.getFeatures('"boundary_id"=%s' %boundary_id)]
if len(existing_line_features) == 1:
existing_line = existing_line_features[0]
existing_lines = existing_line.geometry()
# boundary_geom = boundary_geom.difference(existing_lines)
else:
existing_lines = QgsGeometry()
for sp in new_vertices_layer.getFeatures(
'"iteration" = %s AND "distance" > 0.001' % (iteration-1)
):
sp_boundary_id = sp['boundary_id']
sp_distance = sp['distance']
sp_geom = sp.geometry()
sp_buffer = sp_geom.buffer(sp_distance, 180)
sp_aspoint = sp_geom.asPoint()
if boundary_geom.intersects(sp_buffer):
intersected_boundaries = boundary_geom.intersection(sp_buffer)
intersected_boundaries.convertToMultiType()
boundary_points = [sp_aspoint]
for boundary_line in intersected_boundaries.asMultiPolyline():
for p_geom in boundary_line:
# create a line between the starting point and the vertice
# point
if self.isCanceled():
return False
line = QgsLineString(
[p_geom.x(), sp_aspoint.x()],
[p_geom.y(), sp_aspoint.y()]
)
line_geom = QgsGeometry(line)
if not line_geom.crosses(blocks_geom):
distance = sp_distance - line_geom.length()
pointarea = QgsRectangle(p_geom.x() - 0.005,p_geom.y() - 0.005,
p_geom.x() + 0.005,p_geom.y() + 0.005)
writepoint = True
for point in new_vertices_layer.getFeatures(
QgsFeatureRequest().setFilterRect(pointarea)):
if distance > point['distance']:
provider.deleteFeatures( [point.id()] )
new_vertices_layer.updateFields()
else:
writepoint = False
if writepoint:
feature = QgsFeature(fields)
feature.setGeometry(QgsGeometry(QgsPoint(p_geom)))
feature[distance_id] = distance
feature[iteration_id] = iteration
feature[prev_id_id] = sp.id()
feature[boundary_id_id] = boundary_id
provider.addFeatures( [feature] )
new_vertices_layer.updateFields()
boundary_points.append(p_geom)
for j, p1_geom in enumerate(boundary_points):
for k, p2_geom in enumerate(boundary_points[j+1:]):
if self.isCanceled():
return False
line = QgsGeometry(
QgsLineString(
[p1_geom.x(), p2_geom.x()],
[p1_geom.y(), p2_geom.y()]
)
)
if line.within(boundary_geom.buffer(0.1, 5)):
# newlines.append(line.buffer(0.01, 5, 2, 2, 2))
# newlines = QgsGeometry().unaryUnion([newlines, line.buffer(0.01, 5, 2, 2, 2)])
newlines = QgsGeometry().unaryUnion([newlines, line.buffer(0.01, 5)])
walkable_line_geometry = QgsGeometry().unaryUnion( [newlines, existing_lines] )
if newlines:
if not existing_lines.isEmpty():
with edit(lines_layer):
lines_layer.changeGeometry(
existing_line.id(),
walkable_line_geometry
)
else:
feature = QgsFeature(lines_layer.fields())
feature.setGeometry(walkable_line_geometry)
feature.setId(boundary_id)
feature[lines_boundary_id] = boundary_id
lines_provider.addFeatures( [feature] )
iteration += 1
if __debug:
self.log('iteration = %s' %iteration)
new_vertices_layer.updateFields()
lines_layer.updateFields()
# Fix any invalid geometries made with the lines layer
lines_layer = processing.run(
'qgis:fixgeometries',
{
'INPUT': lines_layer,
'OUTPUT': 'memory:',
}
)['OUTPUT']
# This is where I clip the boundary layer with the walking distance
# buffer
walkable_lines_layer = processing.run(
'qgis:clip',
{
'INPUT' : clipped_boundary_layer,
'OVERLAY' : lines_layer,
'OUTPUT' : 'memory: IC_reachable',
}
)['OUTPUT']
# first calculate the lengths of all lines representing IC result
walkable_lines_layer = processing.run(
'qgis:fieldcalculator',
{
'INPUT' : walkable_lines_layer,
'OUTPUT' : 'memory:IC_reachable',
'FIELD_LENGTH' : 20,
'GLOBAL' : '',
'FIELD_TYPE' : 0,
'FIELD_NAME' : 'length',
'FORMULA' : '$length',
'FIELD_PRECISION' : 3,
}
)['OUTPUT']
# check the calculated lengths for "nan" values and replace them
layer_provider = walkable_lines_layer.dataProvider()
for ic_feature in walkable_lines_layer.getFeatures():
length = ic_feature['length']
if isnan(length):
# replace any nan lengths with calculated ones
fid = ic_feature.id()
idx = walkable_lines_layer.fields().names().index('length')
calculated_length = ic_feature.geometry().length()
attr_value={idx:calculated_length}
layer_provider.changeAttributeValues({fid : attr_value})
# reproject start_point_layer with processing context
self.starting_point_layer = processing.runAndLoadResults(
'qgis:reprojectlayer',
{
'INPUT': starting_point_layer,
'TARGET_CRS': project_crs,
'OUTPUT': 'memory:',
},
context=self.icWorkerContext
)['OUTPUT']
walkable_lines_layer = processing.run(
'qgis:fieldcalculator',
{
'INPUT' : walkable_lines_layer,
'OUTPUT' : 'memory:IC_reachable',
'FIELD_LENGTH' : 20,
'GLOBAL' : '',
'FIELD_TYPE' : 1,
'FIELD_NAME' : 'IC',
'FORMULA' : 'sum("length")',
'FIELD_PRECISION' : 1,
},
)['OUTPUT']
IC = round(next(walkable_lines_layer.getFeatures())['IC'])
endtime = time.time()
self.duration = endtime-starttime
# self.log('total running time: %s s' %(endtime-starttime))
walkable_lines_layer.setName('IC_' + str(IC))
self.walkable_lines_layer = walkable_lines_layer
self.starting_point_layer = starting_point_layer
return True
def finished(self, result):
"""
This function is automatically called when the task has
completed (successfully or not).
You implement finished() to do whatever follow-up stuff
should happen after the task is complete.
finished is always called from the main thread, so it's safe
to do GUI operations and raise Python exceptions here.
result is the return value from self.run.
"""
if result:
self.log(
'Task "{name}" completed in {duration} s'.format(
name=self.description(),
duration=self.duration
),
Qgis.Success)
# emit signals to add the resulting layers to the map
self.layerPrint.emit(
self.walkable_lines_layer,
{'color' : 'red', 'width' : None}
)
self.layerPrint.emit(
self.starting_point_layer,
{'color' : 'red', 'width' : None}
)
else:
if self.exception is None:
self.log(
'Task "{name}" not successful but without '\
'exception (probably the task was manually '\
'canceled by the user)'.format(
name=self.description()),
Qgis.Warning)
else:
self.log(
'Task "{name}" Exception: {exception}'.format(
name=self.description(),
exception=self.exception),
Qgis.Critical)
raise self.exception
def cancel(self):
self.log(
'Task "{name}" was canceled'.format(
name=self.description()),
Qgis.Info)
super().cancel()