-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstream_utilities.py
1281 lines (1009 loc) · 41.1 KB
/
stream_utilities.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""**Utilities class for extract feature from stream.**
.. tip::
Detailed multi-paragraph description...
"""
from builtins import str
from builtins import range
__author__ = 'Tim Sutton <[email protected]>'
__revision__ = '$Format:%H$'
__date__ = '17/04/2014'
__license__ = "GPL"
__copyright__ = ''
from math import sqrt
from qgis.PyQt.QtCore import QVariant, QCoreApplication
from qgis.core import (
Qgis,
QgsFeature,
QgsFeatureRequest,
QgsField,
QgsGeometry,
QgsMapLayer,
QgsPoint,
QgsPointXY,
QgsRectangle,
QgsSpatialIndex,
QgsVectorLayer,
QgsWkbTypes,
)
def tr(message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('@default', message)
def list_to_str(the_list, sep=','):
"""Convert a list to str. If empty, return empty string.
:param the_list: A list.
:type the_list: list
:param sep: Separator for each element in the result.
:type sep: str
:returns: String represent the_list.
:rtype: str
"""
if len(the_list) > 0:
return sep.join([str(x) for x in the_list])
else:
return ''
def str_to_list(the_str, sep=',', the_type=None):
"""Convert the_str to list.
:param the_str: String represents a list.
:type the_str: str
:param sep: Separator for each element in the_str.
:type sep: str
:param the_type: Type of the element.
:type the_type: type
:returns: List from the_str.
:rtype: list
"""
if len(the_str) == 0:
return []
the_list = the_str.split(sep)
if the_type is None:
return the_list
else:
try:
return [the_type(x) for x in the_list]
except TypeError:
raise TypeError('%s is not valid type' % the_type)
def add_layer_attribute(layer, attribute_name, qvariant):
"""Add new attribute called attribute_name to layer.
:param layer: A Vector layer.
:type layer: QgsVectorLayer
:param attribute_name: The name of the new attribute.
:type attribute_name: str
:param qvariant: Attribute type of the new attribute.
:type qvariant: QVariant
"""
id_index = layer.fields().indexFromName(attribute_name)
if id_index == -1:
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.addAttributes([QgsField(attribute_name, qvariant)])
layer.updateFields()
layer.commitChanges()
def extract_nodes(layer):
"""Return a list of tuple that represent line_id, first_point, last_point.
This method will extract node from vector line layer. We only extract the
line_id, first_point of the line, and last_point of the line.
:param layer: A vector line layer.
:type layer: QgsVectorLayer
:returns: list of tuple. The tuple contains line_id, first_point of the
line, and last_point of the line.
:rtype: list
"""
nodes = []
lines = layer.getFeatures()
for feature in lines:
geom = feature.geometry()
# for handling feature with None geometry
if geom is None:
continue
points = geom.asPolyline()
if len(points) < 1:
continue
line_id = feature.id()
first_point = points[0]
last_point = points[-1]
nodes.append((line_id, first_point, last_point))
return nodes
def create_nodes_layer(authority_id='EPSG:4326', nodes=None, name=None):
"""Return QgsVectorLayer (point) that contains nodes.
This method also create attribute for the layer as follow:
id - line_id - node_type
id : the id of the node, generated
line_id : the id of the line. It should be existed in the nodes
node_type : upstream (first point) or downstream (last point).
:param authority_id: Coordinate reference system authid (as represented in
QgsCoordinateReferenceSystem.authid() for the nodes layer that will
be created. Defaults to 'EPSG:4326'.
:type authority_id: str
:param nodes: A list of nodes. Represent as line_id, first_point,
and last_point in a tuple.
:type nodes: list, None
:param name: The name of the layer. If None, set to Nodes.
:type name: str
:returns: A vector point layer that contains nodes as attributes.
:rtype: QgsVectorLayer
"""
if name is None:
name = tr('Stream features')
layer = QgsVectorLayer(
'Point?crs=%s&index=yes' % authority_id, name, 'memory')
data_provider = layer.dataProvider()
# Start edit layer
layer.startEditing()
# Add fields
data_provider.addAttributes([
QgsField('id', QVariant.Int),
QgsField('line_id', QVariant.Int),
QgsField('node_type', QVariant.String)
])
layer.commitChanges()
# For creating node_id
node_id = 0
# Add features
features = []
for node in nodes:
line_id = node[0]
first_point = node[1]
last_point = node[2]
# Add upstream node
feature = QgsFeature()
# noinspection PyArgumentList
feature.setGeometry(QgsGeometry.fromPointXY(first_point))
feature.setAttributes([node_id, line_id, 'upstream'])
features.append(feature)
node_id += 1
# Add upstream node
feature = QgsFeature()
# noinspection PyArgumentList
feature.setGeometry(QgsGeometry.fromPointXY(last_point))
feature.setAttributes([node_id, line_id, 'downstream'])
features.append(feature)
node_id += 1
data_provider.addFeatures(features)
# Commit changes
layer.commitChanges()
return layer
def get_nearby_nodes(layer, node, threshold):
"""Return all nodes that has distance less than threshold from node_id.
The list will be divided into two groups, upstream nodes and downstream
nodes.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
:param node: The point/node.
:type node: QgsFeature
:param threshold: Distance threshold.
:type threshold: float
:returns: Tuple of list of nodes. (upstream_nodes, downstream_nodes).
:rtype: tuple
"""
id_index = layer.fields().indexFromName('id')
node_attributes = node.attributes()
node_id = node_attributes[id_index]
id_index = layer.fields().indexFromName('id')
node_type_index = layer.fields().indexFromName('node_type')
center_node_point = node.geometry().asPoint()
rectangle = QgsRectangle(
center_node_point.x() - threshold,
center_node_point.y() - threshold,
center_node_point.x() + threshold,
center_node_point.y() + threshold)
# iterate through all nodes
upstream_nodes = []
downstream_nodes = []
request = QgsFeatureRequest()
request.setFilterRect(rectangle)
for feature in layer.getFeatures(request):
attributes = feature.attributes()
if feature[id_index] == node_id:
continue
if attributes[node_type_index] == 'upstream':
upstream_nodes.append(attributes[id_index])
if attributes[node_type_index] == 'downstream':
downstream_nodes.append(attributes[id_index])
return upstream_nodes, downstream_nodes
def add_associated_nodes(layer, threshold, callback=None):
"""Add node_list and node_count attribute to every node in layer.
node_list is list of node that is near from a node. There are two node
list; upstream_node_list and downstream_node_list. There are also two
node_count; upstream_node_count and downstream_node_count.
node_count will have the same value as the number of the element in
node_list. But, will be add by one according to the type of the node.
This method will add new attributes (upstream_node_list,
upstream_node_count, downstream_node_list, downstream_node_count) to the
layer and populate those attributes with the right value.
it will use get_nearby_nodes function to populate them.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
:param threshold: Distance threshold.
:type threshold: float
:param callback: A function to all to indicate progress. The function
should accept params 'current' (int) and 'maximum' (int). Defaults to
None.
:type callback: function
"""
nodes = layer.getFeatures()
node_type_index = layer.fields().indexFromName('node_type')
# add attributes
data_provider = layer.dataProvider()
add_layer_attribute(layer, 'up_nodes', QVariant.String)
add_layer_attribute(layer, 'down_nodes', QVariant.String)
add_layer_attribute(layer, 'up_num', QVariant.Int)
add_layer_attribute(layer, 'down_num', QVariant.Int)
up_nodes_index = layer.fields().indexFromName('up_nodes')
down_nodes_index = layer.fields().indexFromName('down_nodes')
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
layer.startEditing()
node_count = layer.featureCount()
counter = 1
dictionary_changes = {}
for node in nodes:
if callback is not None:
if counter % 100 == 0:
callback(current=counter, maximum=node_count)
counter += 1
node_fid = int(node.id())
node_attributes = node.attributes()
# node_id = node_attributes[id_index]
node_type = node_attributes[node_type_index]
upstream_nodes, downstream_nodes = get_nearby_nodes(
layer, node, threshold)
upstream_count = len(upstream_nodes)
downstream_count = len(downstream_nodes)
if node_type == 'upstream':
upstream_count += 1
if node_type == 'downstream':
downstream_count += 1
attributes = {
up_nodes_index: list_to_str(upstream_nodes),
down_nodes_index: list_to_str(downstream_nodes),
up_num_index: upstream_count,
down_num_index: downstream_count
}
dictionary_changes[node_fid] = attributes
data_provider.changeAttributeValues(dictionary_changes)
if callback:
callback(current=node_count, maximum=node_count)
layer.commitChanges()
def check_associated_attributes(layer):
"""Check whether there have been associated attributes or not.
Associated attributed : up_nodes, down_nodes, up_num, down_num
:param layer: A vector point layer.
:type layer: QgsVectorLayer
:returns: True if so, else False.
:rtype: bool
"""
up_nodes_index = layer.fields().indexFromName('up_nodes')
down_nodes_index = layer.fields().indexFromName('down_nodes')
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
if -1 in [up_nodes_index, down_nodes_index, up_num_index, down_num_index]:
return False
else:
return True
def identify_wells(layer):
"""Mark nodes from the layer if it is a well.
A node is identified as a well if the number of upstream nodes == 1 and
the number downstream node 0.
And add attribute `well` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'well', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
well_index = layer.fields().indexFromName('well')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if up_num == 1 and down_num == 0:
well_value = 1
else:
well_value = 0
attributes = {well_index: well_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_sinks(layer):
"""Mark nodes from the layer if it is a sink.
A node is identified as a sink if the number of upstream nodes == 0 and
the number downstream node > 0.
And add attribute `sink` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'sink', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
sink_index = layer.fields().indexFromName('sink')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if up_num == 0 and down_num > 0:
sink_value = 1
else:
sink_value = 0
attributes = {sink_index: sink_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_watersheds(layer):
"""Mark nodes from the layer if it is a watershed.
A node is identified as a watershed if the number of upstream nodes > 1 and
the number downstream node = 0.
And add attribute `watershed` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'watershed', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
watershed_index = layer.fields().indexFromName('watershed')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if up_num > 1 and down_num == 0:
watershed_value = 1
else:
watershed_value = 0
attributes = {watershed_index: watershed_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_unclear_bifurcations(layer):
"""Mark nodes from the layer if it is a unclear bifurcations.
A node is identified as a unclear_bifurcations if the number of upstream
nodes > 1 and the number downstream node > 1 and both of them must have
the sam number.
And add attribute `unclear_bi` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'unclear_bi', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
unclear_bifurcation_index = layer.fields().indexFromName('unclear_bi')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if 1 < up_num == down_num > 1:
unclear_bifurcation_value = 1
else:
unclear_bifurcation_value = 0
attributes = {unclear_bifurcation_index: unclear_bifurcation_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_branches(layer):
"""Mark nodes from the layer if it is a branch.
A node is identified as a branch if 1 <= the number of upstream nodes <
the number of downstream nodes.
And add attribute `branch` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'branch', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
branch_index = layer.fields().indexFromName('branch')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if 1 <= down_num < up_num:
branch_value = 1
else:
branch_value = 0
attributes = {branch_index: branch_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_confluences(layer):
"""Mark nodes from the layer if it is a confluence.
A node is identified as a confluence if 1 <= the number of downstream
nodes < the number of upstream nodes.
And add attribute `confluence` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'confluence', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
confluence_index = layer.fields().indexFromName('confluence')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if 1 <= up_num < down_num:
confluence_value = 1
else:
confluence_value = 0
attributes = {confluence_index: confluence_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def identify_pseudo_nodes(layer):
"""Mark nodes from the layer if it is a pseudo_node.
A node is identified as a pseudo_node if the number of upstream nodes == 1
and the number downstream node == 1.
And add attribute `pseudo_node` for marking.
:param layer: A vector point layer.
:type layer: QgsVectorLayer
"""
if not check_associated_attributes(layer):
raise Exception('You should add associated node first')
add_layer_attribute(layer, 'pseudo', QVariant.Int)
nodes = layer.getFeatures()
up_num_index = layer.fields().indexFromName('up_num')
down_num_index = layer.fields().indexFromName('down_num')
pseudo_node_index = layer.fields().indexFromName('pseudo')
dictionary_attributes = {}
for node in nodes:
node_fid = node.id()
node_attributes = node.attributes()
up_num = node_attributes[up_num_index]
down_num = node_attributes[down_num_index]
if up_num == 1 and down_num == 1:
pseudo_node_value = 1
else:
pseudo_node_value = 0
attributes = {pseudo_node_index: pseudo_node_value}
dictionary_attributes[node_fid] = attributes
data_provider = layer.dataProvider()
layer.startEditing()
data_provider.changeAttributeValues(dictionary_attributes)
layer.commitChanges()
def between(a, b, c):
"""True if c is between a and b."""
if a < b < c:
return True
if c < b < a:
return True
return False
def point_in_line(point, line):
"""True if a point is in a line that has only two vertices."""
x_point = point[0]
y_point = point[1]
x1_line = line[0][0]
y1_line = line[0][1]
x2_line = line[1][0]
y2_line = line[1][1]
return (between(x1_line, x_point, x2_line)
and between(y1_line, y_point, y2_line))
def get_spatial_index(data_provider):
"""Create spatial index from a data provider."""
qgs_feature = QgsFeature()
index = QgsSpatialIndex()
qgs_features = data_provider.getFeatures()
while qgs_features.nextFeature(qgs_feature):
index.insertFeature(qgs_feature)
return index
def identify_intersections(layer):
"""Return all self intersection points of a line.
:param layer: A vector line to be identified.
:type layer: QgsVectorLayer
:returns: List of QgsPoint that represent the intersection point.
:rtype: list
"""
intersections = []
data_provider = layer.dataProvider()
spatial_index = get_spatial_index(data_provider)
feature_2 = QgsFeature()
features = layer.getFeatures()
for feature in features:
geometry = feature.geometry()
vertices = geometry.asPolyline()
intersect_lines = spatial_index.intersects(geometry.boundingBox())
for intersect_line in intersect_lines:
line_id = int(intersect_line)
data_provider.getFeatures(
QgsFeatureRequest().setFilterFid(line_id)).nextFeature(
feature_2)
geometry_2 = feature_2.geometry()
if geometry.intersects(geometry_2):
temp_geom = geometry.intersection(geometry_2)
if temp_geom.type() == QgsWkbTypes.PointGeometry:
temp_list = []
if temp_geom.isMultipart():
temp_list = temp_geom.asMultiPoint()
else:
temp_list.append(temp_geom.asPoint())
if len(vertices) > 1:
if vertices[0] in temp_list:
temp_list.remove(vertices[0])
if vertices[-1] in temp_list:
temp_list.remove(vertices[-1])
intersections.extend(temp_list)
# Note(ismailsunni): if I converted directly from list to set, there is a
# problem. The elements in the set are not unique in qgis 2.0.
# Yes, this is strange.
result = []
for intersection in intersections:
if intersection not in result:
result.append(intersection)
return result
# noinspection PyArgumentList,PyCallByClass,PyTypeChecker
def identify_self_intersections(line):
"""Return all self intersection points of a line.
Adapted from:
http://qgis.osgeo.org/api/qgsgeometryvalidator_8cpp_source.html#l00371
:param line: A line to be identified.
:type line: QgsFeature
:returns: List of QgsPoint that represent the intersection point.
:rtype: list
"""
self_intersections = []
geometry = line.geometry()
vertices = geometry.asPolyline()
if len(vertices) <= 2:
return self_intersections
for i in range(len(vertices) - 2):
v = (vertices[i + 1].x() - vertices[i].x(),
vertices[i + 1].y() - vertices[i].y())
for j in range(i + 2, len(vertices) - 1):
w = (vertices[j + 1].x() - vertices[j].x(),
vertices[j + 1].y() - vertices[j].y())
d = v[1] * w[0] - v[0] * w[1]
if d == 0:
# Continue to the next part of line
continue
dx = vertices[j].x() - vertices[i].x()
dy = vertices[j].y() - vertices[i].y()
k = (dy * w[0] - dx * w[1]) / float(d)
intersection = vertices[i][0] + v[0] * k, vertices[i][1] + v[1] * k
if not point_in_line(intersection, vertices[i:i + 2]):
continue
if not point_in_line(intersection, vertices[j:j + 2]):
continue
self_intersections.append(
QgsPoint(intersection[0], intersection[1]))
return self_intersections
def identify_segment_center(line):
"""Return a QgsPoint of linear segment center of the line.
:param line: A line to be identified.
:type line: QgsFeature
:returns: A linear segment center.
:rtype: QgsPoint
"""
geometry = line.geometry()
vertices = geometry.asPolyline()
vertex_count = len(vertices)
if vertex_count < 1:
return None
part_lengths = []
for i in range(vertex_count - 1):
length = vertices[i].sqrDist(vertices[i + 1])
part_lengths.append(sqrt(length))
segment_count = len(part_lengths)
line_length = sum(part_lengths)
half_length = 0.5 * line_length
current_length = 0
i = 0
add_length = 0
while current_length <= half_length and i < segment_count:
add_length = part_lengths[i]
current_length += add_length
i += 1
current_length -= add_length
delta_length = half_length - current_length
i -= 1
if add_length > 0:
ratio = float(delta_length) / float(add_length)
center_x = vertices[i].x()
center_x += ratio * (vertices[i + 1].x() - vertices[i].x())
center_y = vertices[i].y()
center_y += ratio * (vertices[i + 1].y() - vertices[i].y())
else:
try:
center_x = vertices[i].x()
center_y = vertices[i].y()
except IndexError:
return None
return QgsPoint(center_x, center_y)
def identify_segment_centers(layer):
"""Return a list of QgsPoint of linear segment centers in layer.
:param layer: A vector line layer to be identified.
:type layer: QgsVectorLayer
:returns: A list of linear segment center.
:rtype: list
"""
segment_centers = []
data_provider = layer.dataProvider()
features = data_provider.getFeatures()
for feature in features:
center = identify_segment_center(feature)
if center is not None:
segment_centers.append(center)
return segment_centers
def identify_self_intersections_layer(layer):
"""Return all self intersection points of a vector line layer.
:param layer: A vector line layer to be identified.
:type layer: QgsVectorLayer
:returns: List of QgsPoint that represent the intersection point.
:rtype: list
"""
self_intersections_layer = []
data_provider = layer.dataProvider()
features = data_provider.getFeatures()
for feature in features:
self_intersections = identify_self_intersections(feature)
if self_intersections:
self_intersections_layer.extend(self_intersections)
return self_intersections_layer
def create_intermediate_layer(input_layer, threshold=0, callback=None):
"""Helper function to create intermediate layer.
Intermediate layer is a temporary layer that is used for helping the tool
to identify the nodes.
:param input_layer: A vector line layer.
:type input_layer: QgsVectorLayer
:param threshold: Distance threshold for node snapping. Defaults to 1.
:type threshold: float
:param callback: A function to all to indicate progress. The function
should accept params 'current' (int) and 'maximum' (int). Defaults to
None.
:type callback: function
:returns: Intermediate layer.
:rtype: QgsVectorLayer
"""
# Creating intermediate layer
authority_id = input_layer.crs().authid()
nodes = extract_nodes(layer=input_layer)
nodes_layer_name = tr('Intermediate layer')
# noinspection PyTypeChecker
intermediate_layer = create_nodes_layer(
authority_id=authority_id, nodes=nodes, name=nodes_layer_name)
add_associated_nodes(intermediate_layer, threshold, callback)
# Create a collection of function references that we will call in turn
# Note the actually run order is non deterministic so each function
# should be independent.
rules = {
tr('Finding wells...'): identify_wells,
tr('Finding sinks...'): identify_sinks,
tr('Finding branches...'): identify_branches,
tr('Finding confluences...'): identify_confluences,
tr('Finding pseudo nodes...'): identify_pseudo_nodes,
tr('Finding watersheds...'): identify_watersheds,
tr('Finding unclear bifurcations...'): identify_unclear_bifurcations}
rule_count = len(rules)
index = 1
for message, rule in list(rules.items()):
callback(current=index, maximum=rule_count, message=message)
rule(intermediate_layer)
index += 1
return intermediate_layer
def create_new_features(
intermediate_layer,
self_intersections,
intersections,
segment_centers):
"""Create list of features ready to add to final layer.
This function will extract node from intermediate_layer, then add points
from self_intersections, intersections, and segment_centers to complete
the list of features.
:param intermediate_layer: An intermediate layer.
:type intermediate_layer: QgsVectorLayer
:param self_intersections: List of self_intersection points.
:type self_intersections: list
:param intersections: List of intersection points.
:type intersections: list
:param segment_centers: List of segment_center points.
:type segment_centers: list
:returns: List of QgsFeature
:rtype: list
"""
new_features = []
id_index = intermediate_layer.fields().indexFromName('id')
upstream_index = intermediate_layer.fields().indexFromName('up_nodes')
downstream_index = intermediate_layer.fields().indexFromName('down_nodes')
well_index = intermediate_layer.fields().indexFromName('well')
sink_index = intermediate_layer.fields().indexFromName('sink')
branch_index = intermediate_layer.fields().indexFromName('branch')
confluence_index = intermediate_layer.fields().indexFromName('confluence')
pseudo_node_index = intermediate_layer.fields().indexFromName('pseudo')
watershed_index = intermediate_layer.fields().indexFromName('watershed')
unclear_bifurcation_index = intermediate_layer.fields().indexFromName('unclear_bi')
feature_indexes = [
well_index,
sink_index,
branch_index,
confluence_index,
pseudo_node_index,
watershed_index,
unclear_bifurcation_index]
feature_names = [
tr('Well'),
tr('Sink'),
tr('Branch'),
tr('Confluence'),
tr('Pseudo node'),
tr('Watershed'),
tr('Unclear Bifurcation')]
self_intersection_name = tr('Self Intersection')
segment_center_name = tr('Segment Center')
intersection_name = tr('Intersection')
intermediate_data_provider = intermediate_layer.dataProvider()
nodes = intermediate_data_provider.getFeatures()
new_node_id = 1
expired_node_id = set()
for node in nodes:
# get data from intermediate layers
node_attribute = node.attributes()
node_id = node_attribute[id_index]
if str(node_id) in expired_node_id:
# Continue to the next node if its nearby nodes has been already
# computed