-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
generic_functions.py
executable file
·1394 lines (877 loc) · 42.7 KB
/
generic_functions.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 -*-
from PyQt5.QtCore import QVariant
# from qgis.PyQt.QtCore import QVariant
from qgis import processing
from processing.tools import dataobjects
from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer, QgsProject, edit, QgsGeometry, QgsProperty, QgsField, QgsFeature, QgsRasterLayer, QgsSpatialIndex, QgsFeatureRequest, QgsGeometryUtils, QgsVector, QgsCoordinateTransform, QgsMultiPoint, QgsPoint, QgsPointXY, QgsProperty, QgsApplication, Qgis #, QgsRendererCategory, QgsSymbol, QgsLineSymbol, QgsCategorizedSymbolRenderer
# from qgis.core import Qgis
from processing.gui.AlgorithmExecutor import execute_in_place
import os, json #, random
from math import isclose,pi
crs_4326 = QgsCoordinateReferenceSystem("EPSG:4326")
def create_dir_ifnotexists(folderpath):
if not os.path.exists(folderpath):
if not folderpath == '':
os.makedirs(folderpath)
def generate_buffer(inputlayer,distance,segments=5,dissolve=True,cap_style='FLAT',join_style='ROUND',outputlayer='TEMPORARY_OUTPUT'):
'''
interfacing qgis processing operation
one can specify variable length with an QGIS expression like the defalt value in 'distance' parameter
someting like: '( "width" /2)+1.5'
'''
parameter_dict = {'INPUT': inputlayer, 'DISTANCE': distance,'OUTPUT': outputlayer,'DISSOLVE':dissolve,'SEGMENTS':segments}
if type(distance) == str:
parameter_dict['DISTANCE'] = QgsProperty.fromExpression(distance)
cap_styles = {"FLAT":1,"ROUND":0,'SQUARE':2}
if cap_style.upper() in cap_styles:
parameter_dict['END_CAP_STYLE'] = cap_styles[cap_style.upper()]
join_styles = {'ROUND':0,'MITER':1,'BEVEL':2}
if join_style.upper() in join_styles:
parameter_dict['JOIN_STYLE'] = join_styles[join_style.upper()]
return processing.run('native:buffer',parameter_dict)['OUTPUT']
def remove_duplicate_geometries(inputlayer,outputlayer):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer}
return processing.run('native:deleteduplicategeometries',parameter_dict)['OUTPUT']
def remove_duplicate_vertices(inputlayer,tolerance):
parameter_dict = {'INPUT': inputlayer,'TOLERANCE':tolerance,'OUTPUT':'TEMPORARY_OUTPUT'}
return processing.run('native:removeduplicatevertices',parameter_dict)['OUTPUT']
def split_lines_by_max_len(inputlayer,len_val_or_expression,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer,'LENGTH':len_val_or_expression, 'OUTPUT': outputlayer}
if type(len_val_or_expression) == str:
parameter_dict['LENGTH'] = QgsProperty.fromExpression(len_val_or_expression)
return processing.run('native:splitlinesbylength',parameter_dict)['OUTPUT']
def vec_layers_intersection(inputlayer,overlay_layer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer,'OVERLAY':overlay_layer, 'OUTPUT': outputlayer}
return processing.run('qgis:intersection',parameter_dict)['OUTPUT']
def compute_difference_layer(inputlayer,overlaylayer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer,'OVERLAY':overlaylayer, 'OUTPUT': outputlayer}
return processing.run('qgis:difference',parameter_dict)['OUTPUT']
# def difference_inplace(inputlayer,overlaylayer):
# # with edit(inputlayer):
# inputlayer.startEditing()
# parameter_dict = {'INPUT': inputlayer,'OVERLAY':overlaylayer}
# registry = QgsApplication.instance().processingRegistry()
# alg = registry.algorithmById("qgis:difference")
# execute_in_place(alg, parameter_dict)
# inputlayer.commitChanges()
def convert_multipart_to_singleparts(inputlayer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer}
return processing.run('native:multiparttosingleparts',parameter_dict)['OUTPUT']
def mergelayers(inputlayerlist,dest_crs,outputlayer='TEMPORARY_OUTPUT'):
'''
Will only work for layers of the same geometry type
'''
parameter_dict = {'LAYERS': inputlayerlist,'CRS':dest_crs, 'OUTPUT': outputlayer}
return processing.run('native:mergevectorlayers',parameter_dict)['OUTPUT']
# # # def check_distances_layers(layer_many_features,layer_one_feature,idx=0):
def dissolve_tosinglegeom(inputlayer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer}
return processing.run('native:dissolve',parameter_dict)['OUTPUT']
def merge_touching_lines(inputlayer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer}
return processing.run('native:mergelines',parameter_dict)['OUTPUT']
def polygonize_lines(inputlines,outputlayer='TEMPORARY_OUTPUT',keepfields=True):
parameter_dict = {'INPUT': inputlines, 'OUTPUT': outputlayer}
return processing.run('native:polygonize',parameter_dict)['OUTPUT']
def convex_hulls(inputlayer,outputlayer='TEMPORARY_OUTPUT',keepfields=True):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer,'KEEP_FIELDS':keepfields}
return processing.run('native:convexhull',parameter_dict)['OUTPUT']
def snap_layers(inputlayer,snap_layer,behavior_code=1,tolerance=0.1,outputlayer='TEMPORARY_OUTPUT',dontcheckinvalid=False):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer,'REFERENCE_LAYER':snap_layer,'TOLERANCE':tolerance,'BEHAVIOR':behavior_code}
if not dontcheckinvalid:
return processing.run('native:snapgeometries',parameter_dict)['OUTPUT']
else:
# thx: https://gis.stackexchange.com/a/307618
context = dataobjects.createContext()
context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
return processing.run("qgis:snapgeometries", parameter_dict, context=context)['OUTPUT']
def extract_lines_from_polygons(input_polygons,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': input_polygons, 'OUTPUT': outputlayer}
return processing.run('native:polygonstolines',parameter_dict)['OUTPUT']
def extract_with_spatial_relation(input_layer,compared_layer,predicate:list=[5],outputlayer='TEMPORARY_OUTPUT',dontcheckinvalid=True):
"""
Generic spatial relationship extractor
Predicates are:
YOU MUST PASS A LIST, you can use more than one predicate
0 — intersect; 1 — contain;2 — disjoint; 3 — equal; 4 — touch; 5 — overlap; 6 — are within; 7 — cross
"""
parameter_dict = {'INPUT': input_layer,'PREDICATE':predicate,'INTERSECT':compared_layer, 'OUTPUT': outputlayer}
if dontcheckinvalid:
# thx: https://gis.stackexchange.com/a/307618
context = dataobjects.createContext()
context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
return processing.run("qgis:extractbylocation", parameter_dict, context=context)['OUTPUT']
else:
return processing.run('qgis:extractbylocation',parameter_dict)['OUTPUT']
def collected_geoms_layer(inputlayer,outputlayer='TEMPORARY_OUTPUT'):
'''
interface to
https://docs.qgis.org/3.22/en/docs/user_manual/processing_algs/qgis/vectorgeometry.html#qgiscollect
'''
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer}
return processing.run('native:collect',parameter_dict)['OUTPUT']
def gen_centroids_layer(inputlayer,outputlayer='TEMPORARY_OUTPUT',for_allparts=False):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer,'ALL_PARTS':for_allparts}
return processing.run('native:centroids',parameter_dict)['OUTPUT']
def gen_voronoi_polygons_layer(inputlayer,outputlayer='TEMPORARY_OUTPUT',buffer_perc=300):
parameter_dict = {'INPUT': inputlayer, 'OUTPUT': outputlayer,'BUFFER':buffer_perc}
return processing.run('qgis:voronoipolygons',parameter_dict)['OUTPUT']
def get_intersections(inputlayer,intersect_layer,outputlayer):
parameter_dict = {'INPUT': inputlayer, 'INTERSECT': intersect_layer, 'OUTPUT': outputlayer}
return processing.run('qgis:lineintersections',parameter_dict)['OUTPUT']
def cliplayer_v2(inputlayer,overlay_lyr,outputlayer='TEMPORARY_OUTPUT'):
"""
the first one was intended for datafiles, not memeory layers
"""
parameter_dict = {'INPUT': inputlayer, 'OVERLAY': overlay_lyr, 'OUTPUT': outputlayer}
return processing.run('qgis:clip',parameter_dict)['OUTPUT']
def reproject_layer(inputlayer,destination_crs='EPSG:4326',output_mode='memory:Reprojected'):
parameter_dict = {'INPUT': inputlayer, 'TARGET_CRS': destination_crs,'OUTPUT': output_mode}
return processing.run('native:reprojectlayer', parameter_dict)['OUTPUT']
def split_lines(inputlayer,splitterlayer,outputlayer='TEMPORARY_OUTPUT'):
parameter_dict = {'INPUT': inputlayer, 'LINES': splitterlayer, 'OUTPUT': outputlayer}
return processing.run('qgis:splitwithlines',parameter_dict)['OUTPUT']
def cliplayer(inlayerpath,cliplayerpath,outputpath):
'''
clip a layer
all inputs are paths!!!
will be generated clipped layer as a file in outputpath
source: https://opensourceoptions.com/blog/pyqgis-clip-vector-layers/ (thx!!)
'''
#run the clip tool
processing.run("native:clip", {'INPUT':inlayerpath,'OVERLAY':cliplayerpath,'OUTPUT':outputpath})
def remove_biggest_polygon(inputlayer,record_area=False,area_fieldname='area'):
areas = []
ids = []
# if one extracts only boundaries, one can still use the area value
if record_area:
create_new_layerfield(inputlayer,area_fieldname)
area_idx = inputlayer.fields().indexOf(area_fieldname)
with edit(inputlayer):
for feature in inputlayer.getFeatures():
area_val = feature.geometry().area()
areas.append(area_val)
ids.append(feature.id())
if record_area:
inputlayer.changeAttributeValue(feature.id(),area_idx,area_val)
max_area_idx = areas.index(max(areas))
inputlayer.deleteFeature(ids[max_area_idx])
def single_geom_polygonize(inputgeom):
return QgsGeometry.polygonize([inputgeom]).asGeometryCollection()[0]
def create_area_field(inputlayer,area_fieldname):
area_idx = create_new_layerfield(inputlayer,area_fieldname)
with edit(inputlayer):
if inputlayer.geometryType() == 2:
for feature in inputlayer.getFeatures():
area_val = feature.geometry().area()
inputlayer.changeAttributeValue(feature.id(),area_idx,area_val)
elif inputlayer.geometryType() == 1:
for feature in inputlayer.getFeatures():
area_val = single_geom_polygonize(feature.geometry()).area()
inputlayer.changeAttributeValue(feature.id(),area_idx,area_val)
# for points or not-spatial will leave all NULL
return area_idx
def create_perimeter_field(inputlayer,perimeter_fieldname):
perimeter_idx = create_new_layerfield(inputlayer,perimeter_fieldname)
with edit(inputlayer):
if inputlayer.geometryType() == 1:
for feature in inputlayer.getFeatures():
perim_val = feature.geometry().length()
inputlayer.changeAttributeValue(feature.id(),perimeter_idx,perim_val)
# TBD for polygons
# elif inputlayer.geometryType() == 2:
# for feature in inputlayer.getFeatures():
# perim_val =
# inputlayer.changeAttributeValue(feature.id(),perimeter_idx,perim_val)
# for points or not-spatial will leave all NULL
return perimeter_idx
def path_from_layer(inputlayer,splitcharacter='|',splitposition=0):
return inputlayer.dataProvider().dataSourceUri().split(splitcharacter)[splitposition]
def custom_local_projection(lgt_0,lat_0=0,mode='TM',return_wkt=False):
as_wkt = f"""PROJCRS["unknown",
BASEGEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]],
ID["EPSG",6326]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8901]]],
CONVERSION["unknown",
METHOD["Transverse Mercator",
ID["EPSG",9807]],
PARAMETER["Latitude of natural origin",{lat_0},
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8801]],
PARAMETER["Longitude of natural origin",{lgt_0},
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["Scale factor at natural origin",1,
SCALEUNIT["unity",1],
ID["EPSG",8805]],
PARAMETER["False easting",0,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",0,
LENGTHUNIT["metre",1],
ID["EPSG",8807]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]
"""
# TODO: if mode != 'TM' and lat_0 != 0:
# define as_wkt as a stereographic projection
custom_crs = QgsCoordinateReferenceSystem()
custom_crs.createFromWkt(as_wkt)
if return_wkt:
return as_wkt
else:
return custom_crs
def reproject_layer_localTM(inputlayer,outputpath,layername,lgt_0,lat_0=0):
'''
pass None to "outputpath" in order to use an only memory layer
'''
# https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#reproject-layer
operation = f'+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=tmerc +lat_0={lat_0} +lon_0={lgt_0} +k=1 +x_0=0 +y_0=0 +ellps=WGS84'
parameter_dict = { 'INPUT' : inputlayer, 'OPERATION' : operation, 'OUTPUT' : outputpath }
if not outputpath:
if layername:
parameter_dict['OUTPUT'] = f'memory:{layername}'
else:
parameter_dict['OUTPUT'] = 'TEMPORARY_OUTPUT'
# option 1: creating from wkt
# proj_wkt = custom_local_projection(lgt_0,return_wkt=True)
# parameter_dict['TARGET_CRS'] = QgsCoordinateReferenceSystem(proj_wkt)
# option 2: as a crs object, directly
new_crs = custom_local_projection(lgt_0,lat_0=lat_0)
parameter_dict['TARGET_CRS'] = new_crs
if not outputpath:
ret_lyr = processing.run('native:reprojectlayer', parameter_dict)['OUTPUT']
else:
processing.run('native:reprojectlayer', parameter_dict)
ret_lyr = QgsVectorLayer(outputpath,layername,'ogr')
# fixing no set layer crs:
ret_lyr.setCrs(new_crs)
return ret_lyr, new_crs
# def retrieve_att(layer,att_id,row_id):
# iterr = layer.getFeatures()
# attrs = []
# for feature in iterr:
# attrs.append(feature.attributes())
# return attrs[row_id][att_id]
# def retrieve_attrs(layer):
# iterr = layer.getFeatures()
# attrs = []
# for feature in iterr:
# attrs.append(feature.attributes())
# return attrs
# def column(matrixList, i):
# return [row[i] for row in matrixList]
def get_first_feature_or_geom(inputlayer,return_geom=False):
first_feature = QgsFeature()
# filling first feature:
inputlayer.getFeatures().nextFeature(first_feature)
if return_geom:
return first_feature.geometry()
else:
return first_feature
def check_empty_layer(inputlayer):
feat_count = 0
for feature in inputlayer.getFeatures():
feat_count += 1
if feat_count > 1:
break
return (feat_count == 0)
def get_column_names(inputlayer):
return inputlayer.fields().names()
def create_new_layerfield(inputlayer,fieldname,datatype=QVariant.Double):
'''
create a new field for the layer, and also return the index of the new field
'''
with edit(inputlayer):
new_field = QgsField(fieldname,datatype)
inputlayer.dataProvider().addAttributes([new_field])
inputlayer.updateFields()
return inputlayer.fields().indexOf(fieldname)
def create_filled_newlayerfield(inputlayer,fieldname,fieldvalue,datatype):
# creating field
field_index = create_new_layerfield(inputlayer,fieldname,datatype)
# then filling:
with edit(inputlayer):
if isinstance(fieldvalue,dict):
dkey = next(iter(fieldvalue))
# print(dkey,fieldvalue[dkey])
# by now only length is implemented
if dkey == 'geometry':
if fieldvalue[dkey] == "length":
for feature in inputlayer.getFeatures():
inputlayer.changeAttributeValue(feature.id(),field_index,feature.geometry().length())
if dkey == 'attr_by_id':
inner_dict = fieldvalue[dkey]
for feature in inputlayer.getFeatures():
if feature.id() in inner_dict:
inputlayer.changeAttributeValue(feature.id(),field_index,inner_dict[feature.id()])
else:
for feature in inputlayer.getFeatures():
inputlayer.changeAttributeValue(feature.id(),field_index,fieldvalue)
def create_fill_id_field(inputlayer,fieldname = 'id_on_layer'):
created_field_index = create_new_layerfield(inputlayer,fieldname,QVariant.Int)
with edit(inputlayer):
for feature in inputlayer.getFeatures():
inputlayer.changeAttributeValue(feature.id(),created_field_index,feature.id())
def remove_layerfields(inputlayer,fieldlist):
# to assert if the field name really exists in 'fieldlist'
layer_fields = get_column_names(inputlayer)
with edit(inputlayer):
for field_name in fieldlist:
if field_name in layer_fields:
f_idx = inputlayer.fields().indexOf(field_name)
inputlayer.deleteAttribute(f_idx)
def remove_all_layerfields(inputlayer):
remove_layerfields(inputlayer,get_column_names(inputlayer))
def get_layercolumn_byname(inputlayer,columname):
input_table = get_layer_att_table(inputlayer)
column_id = inputlayer.fields().lookupField(columname)
return [sublist[column_id] for sublist in input_table]
def get_layer_att_table(inputlayer):
att_lists = []
for feature in inputlayer.getFeatures():
att_lists.append(feature.attributes())
return att_lists
def wipe_folder_files(inputfolderpath):
for filename in os.listdir(inputfolderpath):
filepath = os.path.join(inputfolderpath,filename)
os.remove(filepath)
# def remove_layer(layername):
# # thx https://gis.stackexchange.com/a/310590/49900
# # but deprecated
# layerinstance = QgsProject.instance()
# lyref = layerinstance.mapLayersByName(layername)
# if lyref:
# layerinstance.removeMapLayer(lyref[0].id())
def remove_layerlist(listoflayer_alias):
# thx https://gis.stackexchange.com/a/310590/49900
project_instance = QgsProject.instance()
for layerfullname in project_instance.mapLayers():
if any(alias in layerfullname for alias in listoflayer_alias):
project_instance.removeMapLayer(layerfullname)
### DEPRECATED ###
# # def remove_unconnected_lines(inputlayer):
# # #thx: https://gis.stackexchange.com/a/316058/49900
# # with edit(inputlayer):
# # for i,feature_A in enumerate(inputlayer.getFeatures()):
# # is_disjointed = True
# # # disjointed_features = 0
# # for j,feature_B in enumerate(inputlayer.getFeatures()):
# # if not i == j:
# # if not feature_A.geometry().disjoint(feature_B.geometry()):
# # # print('not disjointed!!',i,j)
# # is_disjointed=False
# # if is_disjointed:
# # # print(is_disjointed)
# # inputlayer.deleteFeature(feature_A.id())
# # # # # # it works inplace =D ()
# # # # # # return inputlayer
def qgs_point_geom_from_line_at(inputlinefeature,index=0):
return QgsGeometry.fromPointXY(inputlinefeature.geometry().asPolyline()[index])
def remove_lines_from_no_block(inputlayer,layer_to_check_culdesac=None):
'''
remove lines in wich one of its ends
are not connected to any other segment
the "layer_to_check_culdesac" is a whole layer (dissolved) that should be checked for 'within' condition
'''
# TODO: check if will work with multilinestrings
check_for_culdesacs = False
if layer_to_check_culdesac:
check_for_culdesacs = True
checker_geom = get_first_feature_or_geom(layer_to_check_culdesac,True)
feature_ids_to_be_removed = []
index = QgsSpatialIndex(inputlayer.getFeatures())
for i,feature_A in enumerate(inputlayer.getFeatures()):
P0 = qgs_point_geom_from_line_at(feature_A) # first point
PF = qgs_point_geom_from_line_at(feature_A,-1) # last point
P0_count = 0
PF_count = 0
# THE OPTIMIZATION PATROL:
# for j,feature_B in enumerate(inputlayer.getFeatures()):
# # if not i == j:
# if P0.intersects(feature_B.geometry()):
# P0_count += 1
# if PF.intersects(feature_B.geometry()):
# PF_count += 1
intersect_ids = index.intersects(feature_A.geometry().boundingBox())
for id in intersect_ids:
if id != feature_A.id():
if P0.intersects(inputlayer.getFeature(id).geometry()):
P0_count += 1
if PF.intersects(inputlayer.getFeature(id).geometry()):
PF_count += 1
# print(P0_count,PF_count)
if any(count == 0 for count in [P0_count,PF_count]):
# after checking, only add if its not a "culdesac"
if check_for_culdesacs:
if not feature_A.geometry().within(checker_geom):
feature_ids_to_be_removed.append(feature_A.id())
# if no "checker geometry", then just add directly
else:
feature_ids_to_be_removed.append(feature_A.id())
with edit(inputlayer):
for feature_id in feature_ids_to_be_removed:
inputlayer.deleteFeature(feature_id)
def remove_features_byattr(inputlayer,attrname,attrvalue):
column_values = get_layercolumn_byname(inputlayer,attrname)
with edit(inputlayer):
for i,feature in enumerate(inputlayer.getFeatures()):
if column_values[i] == attrvalue:
inputlayer.deleteFeature(feature.id())
def add_tms_layer(qms_string,layername):
# mostly for user add basemaps buttons
QgsProject.instance().addMapLayer(QgsRasterLayer(qms_string,layername, 'wms'))
def distance_geom_another_layer(inputgeom,inputlayer,as_list=False,to_sort=False,input_spatial_index=None,max_dist=100,nn_feat_num=5):
ret_dict = {}
'''
one passes a geometry and a layer anf get dict/list of distances
'''
feat_request = QgsFeatureRequest()
if input_spatial_index:
# thx, pt 2 https://gis.stackexchange.com/a/59185/49900
nearest_ids = input_spatial_index.nearestNeighbor(inputgeom,nn_feat_num,max_dist)
feat_request.setFilterFids(nearest_ids)
for feature in inputlayer.getFeatures(feat_request):
ret_dict[feature.id()] = inputgeom.distance(feature.geometry())
if as_list:
if to_sort:
return sorted(list(ret_dict.values()))
else:
return list(ret_dict.values())
else:
return ret_dict
def gen_layer_spatial_index(inputlayer,use_fullgeom_flag=True):
'''
return a spatial index filled with all of the layer's features
'''
# thx: https://gis.stackexchange.com/a/59185/49900 (part 1)
feat_iterator = inputlayer.dataProvider().getFeatures()
if use_fullgeom_flag:
# thx: https://gis.stackexchange.com/a/374282/49900
ret_spatial_index = QgsSpatialIndex(feat_iterator,flags=QgsSpatialIndex.FlagStoreFeatureGeometries)
else:
ret_spatial_index = QgsSpatialIndex(feat_iterator)
# temp_feat = QgsFeature() # just to store temporarily
# # filling:
# while feat_iterator.nextFeature(temp_feat): # so ellegant
# ret_spatial_index.insertFeature(temp_feat)
return ret_spatial_index
# def distances_anotherlyr_unsingNN(inputPTgeom,inputspatialindex,inputlayer,max_dist=100,nn_feat_num=5):
def get_major_dif_signed(inputval,inputdict,tol=0.5,print_diffs=False):
'''
in spite of finding a simple way to obtain the 'orthogonal' distance and ID of the orthonogal feature
'''
diffs = {} # []
inputval = float(inputval)
for key in inputdict:
# always avoid to compare floats equally
desired_value = float(inputdict[key])
if not isclose(inputval,desired_value,abs_tol=tol):
diffs[key] = desired_value-inputval #.append(inputdict[key]-inputval)
else:
refused_key = key
if print_diffs:
print(diffs)
if diffs:
if len(diffs) > 1:
key_maxdif = max(diffs,key=diffs.get)
return inputval+diffs[key_maxdif],key_maxdif
else:
# dict with only one key:
only_key = next(iter(diffs)) # thx: https://stackoverflow.com/a/46042617/4436950
return inputval+diffs[only_key],only_key
else:
return inputval,refused_key
def geom_to_feature(inputgeom,attrs_list=None):
# remember that inplace methods generally have a return that isn't the object itself #lessons
ret_feat = QgsFeature()
ret_feat.setGeometry(inputgeom)
if attrs_list:
ret_feat.setAttributes(attrs_list)
return ret_feat
def layer_from_featlist(featlist,layername=None,geomtype="Point",attrs_dict=None,output_type = 'memory',CRS=None):
'''
creating a layer from a list of features (not geometries)
geomtype must be one of:
[“point”, “linestring”, “polygon”, “multipoint”,”multilinestring”,”multipolygon”]
'''
lname = 'temp'
if layername:
lname = layername
ret_layer = QgsVectorLayer(geomtype, lname, output_type)
with edit(ret_layer):
if attrs_dict:
attrs_list = []
for key in attrs_dict:
attrs_list.append(QgsField(key,attrs_dict[key]))
ret_layer.dataProvider().addAttributes(attrs_list)
ret_layer.updateFields()
for feature in featlist:
ret_layer.dataProvider().addFeature(feature)
ret_layer.updateExtents()
if CRS:
ret_layer.setCrs(CRS)
return ret_layer
def items_minor_than_inlist(value,inpulist):
# thx: https://stackoverflow.com/a/10543316/4436950
return sum(entry < value for entry in inpulist)
def keep_only_contained_within(inputlayer,geomlayer):
geomtocheck = get_first_feature_or_geom(geomlayer,True)
with edit(inputlayer):
for feature in inputlayer.getFeatures():
if not feature.geometry().within(geomtocheck):
inputlayer.deleteFeature(feature.id())
def feature_from_fid(inputlayer,fid):
"""
could be a little less burocratic, but...
"""
# thx: https://gis.stackexchange.com/a/59185/49900
feat_iterator = inputlayer.getFeatures(QgsFeatureRequest().setFilterFid(fid))
ret_feat = QgsFeature()
feat_iterator.nextFeature(ret_feat)
return ret_feat
def points_intersecting_buffer_boundary(input_point,inputlayer,featlist=None,buffersize=1,segments=5):
'''
inputlayer must have geometries of Line type
'''
boundary = input_point.buffer(buffersize,segments).convertToType(Qgis.GeometryType(1)) # 1 is the value for "LineGeometry" in https://api.qgis.org/api/classQgis.html#a84964253bb44012b246c20790799c04d
# list storing the points that shall be returned:
ret_list = []
feat_request = QgsFeatureRequest()
if featlist:
feat_request.setFilterFids(featlist)
for feature in inputlayer.getFeatures(feat_request):
# ret_list.append(boundary.intersection(feature.geometry()))
ret_list.append(feature.geometry().intersection(boundary))
return ret_list
def qgsgeom_to_pointuple(inputgeom):
# TODO (if needed): consider Z or M
p = inputgeom.asPoint()
return p.x(),p.y()
def point_forms_minor_angle_w2(fixedpoint_A,centerpoint_B,pointlist,return_index=False,max_instead=False,print_angles=False):
'''
that is: the point that will forms the minor angle, compared to other angles spawn by the other points.
All angles are formed in conjunction with fixed points A and B
'''
if len(pointlist) == 0:
if return_index:
return 0
else:
return(pointlist[0])
else:
anglelist = []
pA = qgsgeom_to_pointuple(fixedpoint_A)
pB = qgsgeom_to_pointuple(centerpoint_B)
for point in pointlist:
try:
pC = qgsgeom_to_pointuple(point)
angle = QgsGeometryUtils.angleBetweenThreePoints(*pA,*pB,*pC) * (180/pi)
if angle > 180:
angle = 360 - angle
anglelist.append(angle)
except:
anglelist.append(360)
if print_angles:
print(anglelist)
if anglelist:
index = anglelist.index(min(anglelist))
else:
index = 0
if return_index:
return index
else:
return pointlist[index]
def vector_from_2_pts(point_A,point_B,desiredLen = None,normalized=False):
pA = qgsgeom_to_pointuple(point_A)
pB = qgsgeom_to_pointuple(point_B)
dx = pB[0] - pA[0]
dy = pB[1] - pA[1]
ret_vec = QgsVector(dx,dy)
if desiredLen:
return ret_vec.normalized() * desiredLen
else:
if normalized:
return ret_vec.normalized()
else:
return ret_vec
def check_sidewalk_intersection(intersectiongeom,referencepoint):
if not intersectiongeom.isEmpty():
if not intersectiongeom.isMultipart():
return True,intersectiongeom
else:
# if it returns Multipart geometry, it was because there are 2 points of intersection, so we chose the nearest to "referencepoint"
# print(intersectiongeom.asWkt())
# print(intersectiongeom.wkbType())
as_geomcollection = intersectiongeom.asGeometryCollection()
# print([item.type() for item in as_geomcollection])
if intersectiongeom.wkbType() == 4:
distances = [referencepoint.distance(point.asPoint()) for point in as_geomcollection]
return True,as_geomcollection[distances.index(min(distances))]
elif intersectiongeom.wkbType() == 5:
points = []
for line in as_geomcollection:
for point in line.asPolyline():
points.append(point)
distances = [referencepoint.distance(point) for point in points]
# print(points,'\n')
# print(distances,'\n')
return True,pointXY_to_geometry(points[distances.index(min(distances))])
elif intersectiongeom.wkbType() == 7:
points = []