-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools_qgis.py
1570 lines (1217 loc) · 51.4 KB
/
tools_qgis.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
"""
This file is part of Giswater 3
The 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 3 of the License,
or (at your option) any later version.
"""
# -*- coding: utf-8 -*-
import configparser
import re
from functools import partial
from typing import Optional
import console
import os.path
import shlex
import sys
from random import randrange
from qgis.PyQt.QtCore import Qt, QTimer, QSettings
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtWidgets import QDockWidget, QApplication, QPushButton, QDialog, QVBoxLayout, QTextEdit, QDialogButtonBox
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsPointLocator, \
QgsSnappingUtils, QgsTolerance, QgsPointXY, QgsFeatureRequest, QgsRectangle, QgsSymbol, \
QgsLineSymbol, QgsRendererCategory, QgsCategorizedSymbolRenderer, QgsGeometry, QgsCoordinateReferenceSystem, \
QgsCoordinateTransform, QgsVectorLayer, QgsExpression, QgsFillSymbol, QgsMapToPixel, QgsWkbTypes, QgsLayerTree
from qgis.utils import iface, plugin_paths, available_plugins, active_plugins
from . import tools_log, tools_qt, tools_os, tools_db
from . import lib_vars
# List of user parameters (optionals)
user_parameters = {'log_sql': None, 'show_message_durations': None, 'aux_context': 'ui_message'}
# Define message level constants
MESSAGE_LEVEL_INFO = 0 # Blue
MESSAGE_LEVEL_WARNING = 1 # Yellow
MESSAGE_LEVEL_CRITICAL = 2 # Red
MESSAGE_LEVEL_SUCCESS = 3 # Green
# Define message duration constants
DEFAULT_MESSAGE_DURATION = 10
MINIMUM_WARNING_DURATION = 10
def get_feature_by_expr(layer, expr_filter):
# Check filter and existence of fields
expr = QgsExpression(expr_filter)
if expr.hasParserError():
message = f"{expr.parserErrorString()}: {expr_filter}"
show_warning(message)
return
it = layer.getFeatures(QgsFeatureRequest(expr))
# Iterate over features
for feature in it:
return feature
return False
def show_message(text, message_level=MESSAGE_LEVEL_WARNING, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", parameter=None, title="", logger_file=True,
dialog=iface, sqlcontext=None):
"""
Show message to the user with selected message level
:param text: The text to be shown (String)
:param message_level: Message level constant
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param parameter: A text to show after the message (String)
:param title: The title of the message (String)
:param logger_file: Whether it should log the message in a file or not (bool)
"""
global user_parameters
# Get optional parameter 'show_message_durations'
dev_duration = user_parameters.get('show_message_durations')
# If is set, use this value
if dev_duration not in (None, "None"):
if message_level in (MESSAGE_LEVEL_WARNING, MESSAGE_LEVEL_CRITICAL) and int(dev_duration) < MINIMUM_WARNING_DURATION:
duration = DEFAULT_MESSAGE_DURATION
else:
duration = int(dev_duration)
msg = None
if text:
msg = tools_qt.tr(text, context_name, user_parameters['aux_context'])
if parameter:
msg += f": {parameter}"
# Show message
try:
if message_level in (MESSAGE_LEVEL_WARNING, MESSAGE_LEVEL_CRITICAL) and sqlcontext is not None:
# show message with button with the sqlcontext
show_message_function(msg, lambda: show_sqlcontext_dialog(sqlcontext, msg, title, 500, 300), "Show more", message_level, duration, context_name, logger_file, dialog)
else:
dialog.messageBar().pushMessage(title, msg, message_level, duration)
except Exception as e: # This is because "messageBar().pushMessage" is only available for QMainWindow, not QDialog.
print("Exception show_message: ", e)
iface.messageBar().pushMessage(title, msg, message_level, duration)
# Check if logger to file
if lib_vars.logger and logger_file:
lib_vars.logger.info(text)
def show_message_link(text, url, btn_text="Open", message_level=MESSAGE_LEVEL_INFO, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", logger_file=True,
dialog=iface):
"""
Show message to the user with selected message level and a button to open the url
:param text: The text to be shown (String)
:param url: The url that will be opened by the button. It will also show after the message (String)
:param btn_text: The text of the button (String)
:param message_level: {INFO = 0(blue), WARNING = 1(yellow), CRITICAL = 2(red), SUCCESS = 3(green)}
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param logger_file: Whether it should log the message in a file or not (bool)
"""
global user_parameters
# Get optional parameter 'show_message_durations'
dev_duration = user_parameters.get('show_message_durations')
# If is set, use this value
if dev_duration not in (None, "None"):
if message_level in (1, 2) and int(dev_duration) < 10:
duration = 10
else:
duration = int(dev_duration)
msg = None
if text:
msg = tools_qt.tr(text, context_name, user_parameters['aux_context'])
# Create the message with the button
widget = iface.messageBar().createMessage(f"{msg}", f"{url}")
button = QPushButton(widget)
button.setText(f"{btn_text}")
button.pressed.connect(partial(tools_os.open_file, url))
widget.layout().addWidget(button)
# Show the message
dialog.messageBar().pushWidget(widget, message_level, duration)
# Check if logger to file
if lib_vars.logger and logger_file:
lib_vars.logger.info(text)
def show_message_function(text, function, btn_text="Open", message_level=MESSAGE_LEVEL_INFO, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", logger_file=True,
dialog=iface):
"""
Show message to the user with selected message level and a button to open the url
:param text: The text to be shown (String)
:param function: The function (can be a ``partial()`` object) to execute.
:param btn_text: The text of the button (String)
:param message_level: Message level constant
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param logger_file: Whether it should log the message in a file or not (bool)
"""
global user_parameters
# Get optional parameter 'show_message_durations'
dev_duration = user_parameters.get('show_message_durations')
# If is set, use this value
if dev_duration not in (None, "None") and duration > 0:
if message_level in (MESSAGE_LEVEL_WARNING, MESSAGE_LEVEL_CRITICAL) and int(dev_duration) < MINIMUM_WARNING_DURATION:
duration = DEFAULT_MESSAGE_DURATION
else:
duration = int(dev_duration)
msg = None
if text:
msg = tools_qt.tr(text, context_name, user_parameters['aux_context'])
# Create the message with the button
widget = iface.messageBar().createMessage(f"{msg}")
button = QPushButton(widget)
button.setText(f"{btn_text}")
button.pressed.connect(function)
widget.layout().addWidget(button)
# Show the message
dialog.messageBar().pushWidget(widget, message_level, duration)
# Check if logger to file
if lib_vars.logger and logger_file:
lib_vars.logger.info(text)
def show_info(text, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", parameter=None, logger_file=True, title="", dialog=iface):
"""
Show information message to the user
:param text: The text to be shown (String)
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param parameter: A text to show after the message (String)
:param logger_file: Whether it should log the message in a file or not (bool)
:param title: The title of the message (String) """
show_message(text, MESSAGE_LEVEL_INFO, duration, context_name, parameter, title, logger_file, dialog=dialog)
def show_warning(text, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", parameter=None, logger_file=True, title="", dialog=iface):
"""
Show warning message to the user
:param text: The text to be shown (String)
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param parameter: A text to show after the message (String)
:param logger_file: Whether it should log the message in a file or not (bool)
:param title: The title of the message (String) """
show_message(text, MESSAGE_LEVEL_WARNING, duration, context_name, parameter, title, logger_file, dialog=dialog)
def show_critical(text, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", parameter=None, logger_file=True, title="", dialog=iface):
"""
Show critical message to the user
:param text: The text to be shown (String)
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param parameter: A text to show after the message (String)
:param logger_file: Whether it should log the message in a file or not (bool)
:param title: The title of the message (String) """
show_message(text, MESSAGE_LEVEL_CRITICAL, duration, context_name, parameter, title, logger_file, dialog=dialog)
def show_success(text, duration=DEFAULT_MESSAGE_DURATION, context_name="giswater", parameter=None, logger_file=True, title="", dialog=iface):
"""
Show success message to the user
:param text: The text to be shown (String)
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param parameter: A text to show after the message (String)
:param logger_file: Whether it should log the message in a file or not (bool)
:param title: The title of the message (String) """
show_message(text, MESSAGE_LEVEL_SUCCESS, duration, context_name, parameter, title, logger_file, dialog=dialog)
def show_sqlcontext_dialog(sqlcontext: str, msg: str, title: str, min_width: int = 400, min_height: int = 200):
"""
Displays a dialog with the SQL context in a more detailed, error-specific format,
allowing the user to copy the error message.
:param sqlcontext: The SQL context to display (String)
:param msg: The message to display above the sqlcontext (String)
:param title: The title of the dialog window (String)
:param min_width: The minimum width of the dialog (int)
:param min_height: The minimum height of the dialog (int)
"""
dialog = QDialog()
dialog.setWindowTitle(title or "SQL Context")
dialog.setMinimumWidth(min_width)
dialog.setMinimumHeight(min_height)
layout = QVBoxLayout()
# Construct the full message
if msg and sqlcontext:
full_message = f"{msg}\n\nSQL context:\n{sqlcontext}"
elif msg:
full_message = msg
elif sqlcontext:
full_message = f"SQL context:\n{sqlcontext}"
else:
full_message = "No SQL context available."
# Add the message text area to allow copying
text_area = QTextEdit()
text_area.setPlainText(full_message)
text_area.setReadOnly(True)
layout.addWidget(text_area)
# Add standard close button at the bottom
button_box = QDialogButtonBox(QDialogButtonBox.Close)
button_box.rejected.connect(dialog.reject)
layout.addWidget(button_box)
dialog.setLayout(layout)
# Manage stay on top
flags = Qt.WindowStaysOnTopHint
dialog.setWindowFlags(flags)
dialog.exec_()
def get_visible_layers(as_str_list=False, as_list=False):
"""
Return string as {...} or [...] or list with name of table in DB of all visible layer in TOC
False, False --> return str like {"name1", "name2", "..."}
True, False --> return str like ["name1", "name2", "..."]
xxxx, True --> return list like ['name1', 'name2', '...']
"""
layers_name = []
visible_layer = '{'
if as_str_list:
visible_layer = '['
layers = get_project_layers()
for layer in layers:
if not check_query_layer(layer):
continue
if is_layer_visible(layer):
table_name = get_layer_source_table_name(layer)
if not check_query_layer(layer):
continue
layers_name.append(table_name)
visible_layer += f'"{table_name}", '
visible_layer = visible_layer[:-2]
if as_list:
return layers_name
if as_str_list:
visible_layer += ']'
else:
visible_layer += '}'
return visible_layer
def get_plugin_metadata(parameter, default_value, plugin_dir=None):
""" Get @parameter from metadata.txt file """
if not plugin_dir:
plugin_dir = os.path.dirname(__file__)
plugin_dir = plugin_dir.rstrip(f'{os.sep}lib')
# Check if metadata file exists
metadata_file = os.path.join(plugin_dir, 'metadata.txt')
if not os.path.exists(metadata_file):
message = f"Metadata file not found: {metadata_file}"
iface.messageBar().pushMessage("", message, 1, 20)
return default_value
value = None
try:
metadata = configparser.ConfigParser(comment_prefixes=["#", ";"], allow_no_value=True, strict=False)
metadata.read(metadata_file)
value = metadata.get('general', parameter)
except configparser.NoOptionError:
message = f"Parameter not found: {parameter}"
iface.messageBar().pushMessage("", message, 1, 20)
value = default_value
finally:
return value
def get_plugin_version():
""" Get plugin version from metadata.txt file """
# Check if metadata file exists
plugin_version = None
message = None
metadata_file = os.path.join(lib_vars.plugin_dir, 'metadata.txt')
if not os.path.exists(metadata_file):
message = f"Metadata file not found: {metadata_file}"
return plugin_version, message
metadata = configparser.ConfigParser(comment_prefixes=";", allow_no_value=True, strict=False)
metadata.read(metadata_file)
plugin_version = metadata.get('general', 'version')
if plugin_version is None:
message = "Plugin version not found"
return plugin_version, message
def get_major_version(plugin_dir=None, default_version='3.5'):
""" Get plugin higher version from metadata.txt file """
major_version = get_plugin_metadata('version', default_version, plugin_dir)[0:3]
return major_version
def get_build_version(plugin_dir, default_version='35001'):
""" Get plugin build version from metadata.txt file """
build_version = get_plugin_metadata('version', default_version, plugin_dir).replace(".", "")
return build_version
def find_plugin_path(folder_name: str) -> Optional[str]:
"""
Find the full path of a plugin folder by checking possible paths.
:param folder_name: The folder name of the plugin.
:return: The full path to the plugin folder if found, None otherwise.
"""
for path in plugin_paths:
potential_path = os.path.join(path, folder_name)
if os.path.exists(potential_path):
return potential_path
return None
def is_plugin_available(plugin_name: str) -> bool:
"""
Check if a QGIS plugin is available by matching its metadata name.
:param plugin_name: The 'name' parameter from the plugin's metadata.
:return: True if the plugin is available, False otherwise.
"""
for folder_name in available_plugins:
plugin_dir = find_plugin_path(folder_name)
if plugin_dir:
metadata_name = get_plugin_metadata('name', default_value=None, plugin_dir=plugin_dir)
if metadata_name == plugin_name:
return True
return False
def is_plugin_active(plugin_name: str) -> bool:
"""
Check if a QGIS plugin is active by matching its metadata name.
:param plugin_name: The 'name' parameter from the plugin's metadata.
:return: True if the plugin is active, False otherwise.
"""
for folder_name in active_plugins:
plugin_dir = find_plugin_path(folder_name)
if plugin_dir:
metadata_name = get_plugin_metadata('name', default_value=None, plugin_dir=plugin_dir)
if metadata_name == plugin_name:
return True
return False
def get_plugin_folder(plugin_name: str) -> Optional[str]:
"""
Get the full path of a plugin folder by matching its metadata name.
:param plugin_name: The 'name' parameter from the plugin's metadata.
:return: The folder name of the plugin if found, None otherwise.
"""
for folder_name in available_plugins:
plugin_dir = find_plugin_path(folder_name)
if plugin_dir:
metadata_name = get_plugin_metadata('name', default_value=None, plugin_dir=plugin_dir)
if metadata_name == plugin_name:
return folder_name
return None
def enable_python_console():
""" Enable Python console and Log Messages panel """
# Manage Python console
python_console = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')
if python_console:
python_console.setVisible(True)
else:
console.show_console()
# Manage Log Messages panel
message_log = iface.mainWindow().findChild(QDockWidget, 'MessageLog')
if message_log:
message_log.setVisible(True)
def get_project_variable(var_name):
""" Get project variable """
value = None
try:
value = QgsExpressionContextUtils.projectScope(QgsProject.instance()).variable(var_name)
except Exception:
pass
finally:
return value
def set_project_variable(var_name, value):
""" Set project variable """
try:
custom_vars = QgsProject.instance().customVariables()
custom_vars[var_name] = value
QgsProject.instance().setCustomVariables(custom_vars)
except Exception:
pass
finally:
return
def get_project_layers():
""" Return layers in the same order as listed in TOC """
layers = [layer.layer() for layer in QgsProject.instance().layerTreeRoot().findLayers()]
return layers
def find_toc_group(root, group, case_sensitive=False):
""" Find a group of layers in the ToC """
for grp in root.findGroups():
group1 = grp.name()
group2 = group
if not case_sensitive:
group1 = group1.lower()
group2 = group2.lower()
if group1 == group2:
return grp
return None
def get_layer_source(layer):
""" Get database connection paramaters of @layer """
# Initialize variables
layer_source = {'db': None, 'schema': None, 'table': None, 'service': None, 'host': None, 'port': None,
'user': None, 'password': None, 'sslmode': None}
if layer is None:
return layer_source
if layer.providerType() != 'postgres':
return layer_source
# Get dbname, host, port, user and password
uri = layer.dataProvider().dataSourceUri()
# split with quoted substrings preservation
splt = shlex.split(uri)
list_uri = []
for v in splt:
if '=' in v:
elem_uri = tuple(v.split('='))
if len(elem_uri) == 2:
list_uri.append(elem_uri)
splt_dct = dict(list_uri)
if 'service' in splt_dct:
splt_dct['service'] = splt_dct['service']
if 'dbname' in splt_dct:
splt_dct['db'] = splt_dct['dbname']
if 'table' in splt_dct:
splt_dct['schema'], splt_dct['table'] = splt_dct['table'].split('.')
for key in layer_source.keys():
layer_source[key] = splt_dct.get(key)
return layer_source
def get_layer_source_table_name(layer):
""" Get table or view name of selected layer """
if layer is None:
return None
provider = layer.providerType()
if provider == 'postgres':
uri = layer.dataProvider().dataSourceUri().lower()
pos_ini = uri.find('table=')
total = len(uri)
pos_end_schema = uri.rfind('.')
pos_fi = uri.find('" ')
if uri.find('pg:') != -1:
uri_table = uri[pos_ini + 6:total]
elif pos_ini != -1 and pos_fi != -1:
uri_table = uri[pos_end_schema + 2:pos_fi]
else:
uri_table = uri[pos_end_schema + 2:total - 1]
elif provider == 'ogr' and layer.source().split('|')[0].endswith('.gpkg'):
uri_table = ""
parts = layer.source().split('|') # Split by the pipe character '|'
for part in parts:
if part.startswith('layername='):
uri_table = part.split('=')[1]
break
else:
uri_table = None
return uri_table
def get_layer_schema(layer):
""" Get table or view schema_name of selected layer """
if layer is None:
return None
if layer.providerType() != 'postgres':
return None
table_schema = None
uri = layer.dataProvider().dataSourceUri().lower()
pos_ini = uri.find('table=')
pos_end_schema = uri.rfind('.')
pos_fi = uri.find('" ')
if pos_ini != -1 and pos_fi != -1:
table_schema = uri[pos_ini + 7:pos_end_schema - 1]
return table_schema
def get_primary_key(layer=None):
""" Get primary key of selected layer """
uri_pk = None
if layer is None:
layer = iface.activeLayer()
if layer is None:
return uri_pk
uri = layer.dataProvider().dataSourceUri().lower()
pos_ini = uri.find('key=')
pos_end = uri.rfind('srid=')
if pos_ini != -1:
uri_pk = uri[pos_ini + 5:pos_end - 2]
return uri_pk
def get_layer_by_tablename(tablename, show_warning_=False, log_info=False, schema_name=None):
""" Iterate over all layers and get the one with selected @tablename """
# Check if we have any layer loaded
layers = get_project_layers()
if len(layers) == 0:
return None
# Iterate over all layers
layer = None
if schema_name is None:
if 'main_schema' in lib_vars.project_vars:
schema_name = lib_vars.project_vars['main_schema']
else:
tools_log.log_warning("Key not found", parameter='main_schema')
for cur_layer in layers:
uri_table = get_layer_source_table_name(cur_layer)
table_schema = get_layer_schema(cur_layer)
if (uri_table is not None and uri_table == tablename) and schema_name in ('', None, table_schema):
layer = cur_layer
break
if layer is None and show_warning_:
show_warning("Layer not found", parameter=tablename)
if layer is None and log_info:
tools_log.log_info("Layer not found", parameter=tablename)
return layer
def add_layer_to_toc(layer, group=None, sub_group=None, create_groups=False, sub_sub_group=None):
""" If the function receives a group name, check if it exists or not and put the layer in this group
:param layer: (QgsVectorLayer)
:param group: Name of the group that will be created in the toc (string)
"""
if group is None:
QgsProject.instance().addMapLayer(layer)
return
QgsProject.instance().addMapLayer(layer, False)
root: QgsLayerTree = QgsProject.instance().layerTreeRoot()
if root is None:
msg = "QgsLayerTree not found for project."
tools_log.log_error(msg)
return
first_group = find_toc_group(root, group)
if create_groups:
if not first_group:
first_group = root.insertGroup(0, group)
if first_group is None:
msg = f"Group '{group}' not found in layer tree."
tools_log.log_error(msg)
return
if not find_toc_group(first_group, sub_group):
second_group = first_group.insertGroup(0, sub_group)
if second_group is None:
msg = "Couldn't add group."
tools_log.log_error(msg)
return
if sub_sub_group and not find_toc_group(second_group, sub_sub_group):
second_group.insertGroup(0, sub_sub_group)
if first_group and sub_group:
second_group = find_toc_group(first_group, sub_group)
if second_group:
third_group = find_toc_group(second_group, sub_sub_group)
if third_group:
third_group.insertLayer(0, layer)
iface.setActiveLayer(layer)
return
second_group.insertLayer(0, layer)
iface.setActiveLayer(layer)
return
first_group.insertLayer(0, layer)
iface.setActiveLayer(layer)
return
root = QgsProject.instance().layerTreeRoot()
my_group = root.findGroup("GW Layers")
if my_group is None:
my_group = root.insertGroup(0, "GW Layers")
my_group.insertLayer(0, layer)
def add_layer_from_query(query: str, layer_name: str = "QueryLayer",
key_column: Optional[str] = None, geom_column: Optional[str] = "the_geom",
group: Optional[str] = None):
""" Creates a QVectorLayer and adds it to the project """
# Define your PostgreSQL connection parameters
uri = tools_db.get_uri()
# Modify the query to include a unique identifier if key_column is not provided
if key_column is None:
querytext = f"(SELECT row_number() over () AS _uid_, * FROM ({query}) AS query_table)"
key_column = "_uid_"
else:
querytext = f"({query})"
# Set the SQL query and the geometry column (initially without geom_column)
uri.setDataSource("", f"({query})", "", "", key_column)
# Create a provisional layer
provisional_layer = QgsVectorLayer(uri.uri(False), f"{layer_name}", "postgres")
# Check if the provisional layer is valid
if not provisional_layer.isValid():
tools_log.log_error("Layer failed to load!", parameter=querytext)
return
# Check if the geometry column exists in the provisional layer
fields = provisional_layer.fields()
if geom_column in fields.names():
# Update uri to include the geometry column
uri.setDataSource("", querytext, geom_column, "", key_column)
# Create the layer
layer = QgsVectorLayer(uri.uri(False), f"{layer_name}", "postgres")
# Check if the layer is valid
if not layer.isValid():
tools_log.log_error("Layer failed to load!", parameter=querytext)
return
# Add the layer to the project
add_layer_to_toc(layer, group)
def manage_snapping_layer(layername, snapping_type=0, tolerance=15.0):
""" Manage snapping of @layername """
layer = get_layer_by_tablename(layername)
if not layer:
return
if snapping_type == 0:
snapping_type = QgsPointLocator.Vertex
elif snapping_type == 1:
snapping_type = QgsPointLocator.Edge
elif snapping_type == 2:
snapping_type = QgsPointLocator.All
QgsSnappingUtils.LayerConfig(layer, snapping_type, tolerance, QgsTolerance.Pixels)
def select_features_by_ids(feature_type, expr, layers=None):
""" Select features of layers of group @feature_type applying @expr """
if layers is None:
return
if feature_type not in layers:
return
# Build a list of feature id's and select them
for layer in layers[feature_type]:
if expr is None:
layer.removeSelection()
else:
it = layer.getFeatures(QgsFeatureRequest(expr))
id_list = [i.id() for i in it]
if len(id_list) > 0:
layer.selectByIds(id_list)
else:
layer.removeSelection()
def get_points_from_geometry(layer, feature):
""" Get the start point and end point of the feature """
list_points = None
geom = feature.geometry()
if layer.geometryType() == 0:
points = geom.asPoint()
list_points = f'"x1":{points.x()}, "y1":{points.y()}'
elif layer.geometryType() in (1, 2):
points = geom.asPolyline()
init_point = points[0]
last_point = points[-1]
list_points = f'"x1":{init_point.x()}, "y1":{init_point.y()}'
list_points += f', "x2":{last_point.x()}, "y2":{last_point.y()}'
else:
tools_log.log_info("NO FEATURE TYPE DEFINED")
return list_points
def disconnect_snapping(action_pan=True, emit_point=None, vertex_marker=None):
""" Select 'Pan' as current map tool and disconnect snapping """
try:
iface.mapCanvas().xyCoordinates.disconnect()
except TypeError as e:
tools_log.log_info(f"{type(e).__name__} --> {e}")
if emit_point is not None:
try:
emit_point.canvasClicked.disconnect()
except TypeError as e:
tools_log.log_info(f"{type(e).__name__} --> {e}")
if vertex_marker is not None:
try:
vertex_marker.hide()
except AttributeError as e:
tools_log.log_info(f"{type(e).__name__} --> {e}")
if action_pan:
iface.actionPan().trigger()
def refresh_map_canvas(_restore_cursor=False):
""" Refresh all layers present in map canvas """
iface.mapCanvas().refreshAllLayers()
for layer_refresh in iface.mapCanvas().layers():
layer_refresh.triggerRepaint()
if _restore_cursor:
restore_cursor()
def force_refresh_map_canvas():
""" Refresh all layers & map canvas """
refresh_map_canvas() # First refresh all the layers
iface.mapCanvas().refresh() # Then refresh the map view itself
def set_cursor_wait():
""" Change cursor to 'WaitCursor' """
while get_override_cursor() is not None:
restore_cursor()
QApplication.setOverrideCursor(Qt.WaitCursor)
def get_override_cursor():
return QApplication.overrideCursor()
def restore_cursor():
""" Restore to previous cursors """
while get_override_cursor() is not None:
QApplication.restoreOverrideCursor()
def disconnect_signal_selection_changed():
""" Disconnect signal selectionChanged """
try:
iface.mapCanvas().selectionChanged.disconnect()
except Exception:
pass
finally:
iface.actionPan().trigger()
def select_features_by_expr(layer, expr):
""" Select features of @layer applying @expr """
if not layer:
return
if expr is None:
layer.removeSelection()
else:
it = layer.getFeatures(QgsFeatureRequest(expr))
# Build a list of feature id's from the previous result and select them
id_list = [i.id() for i in it]
if len(id_list) > 0:
layer.selectByIds(id_list)
else:
layer.removeSelection()
def get_max_rectangle_from_coords(list_coord):
"""
Returns the minimum rectangle(x1, y1, x2, y2) of a series of coordinates
:param list_coord: list of coords in format ['x1 y1', 'x2 y2',....,'x99 y99']
"""
coords = list_coord.group(1)
polygon = coords.split(',')
x_vals = []
y_vals = []
for p in polygon:
x, y = re.findall(r'-?\d+(?:\.\d+)?', p)
x_vals.append(float(x))
y_vals.append(float(y))
min_x = min(x_vals)
max_x = max(x_vals)
min_y = min(y_vals)
max_y = max(y_vals)
return max_x, max_y, min_x, min_y
def zoom_to_rectangle(x1, y1, x2, y2, margin=5, change_crs=True):
""" Generate an extension on the canvas according to the received coordinates """
rect = QgsRectangle(float(x1) + margin, float(y1) + margin, float(x2) - margin, float(y2) - margin)
if str(lib_vars.data_epsg) == '2052' and str(lib_vars.project_epsg) == '102566' and change_crs:
rect = QgsRectangle(float(float(x1) + margin) * -1,
(float(y1) + margin) * -1,
(float(x2) - margin) * -1,
(float(y2) - margin) * -1)
elif str(lib_vars.data_epsg) != str(lib_vars.project_epsg) and change_crs:
data_epsg = QgsCoordinateReferenceSystem(str(lib_vars.data_epsg))
project_epsg = QgsCoordinateReferenceSystem(str(lib_vars.project_epsg))
tform = QgsCoordinateTransform(data_epsg, project_epsg, QgsProject.instance())
rect = tform.transform(rect)
iface.mapCanvas().setExtent(rect)
iface.mapCanvas().refresh()
def get_composers_list():
""" Returns the list of project composer """
layour_manager = QgsProject.instance().layoutManager().layouts()
active_composers = [layout for layout in layour_manager]
return active_composers
def get_composer_index(name):
""" Returns the index of the selected composer name"""
index = 0
composers = get_composers_list()
for comp_view in composers:
composer_name = comp_view.name()
if composer_name == name:
break
index += 1
return index
def get_geometry_vertex(list_coord=None):
"""
Return list of QgsPoints taken from geometry
:param list_coord: list of coors in format ['x1 y1', 'x2 y2',....,'x99 y99']
"""
coords = list_coord.group(1)
polygon = coords.split(',')
points = []
for i in range(0, len(polygon)):
x, y = polygon[i].split(' ')
point = QgsPointXY(float(x), float(y))
points.append(point)
return points
def reset_rubber_band(rubber_band):
""" Reset QgsRubberBand """
rubber_band.reset()
def restore_user_layer(layer_name, user_current_layer=None):
""" Set active layer, preferably @user_current_layer else @layer_name """
if user_current_layer:
iface.setActiveLayer(user_current_layer)
else:
layer = get_layer_by_tablename(layer_name)
if layer: