-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpghydro_tools.py
2532 lines (1710 loc) · 92.1 KB
/
pghydro_tools.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 -*-
"""
/***************************************************************************
PghydroTools
A QGIS plugin
This plugin create the pghydro schema and runs all the process to consist a drainage network
-------------------
begin : 2020-01-31
git sha : $Format:%H$
copyright : (C) 2020 by Alexandre de Amorim Teixeira
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from __future__ import absolute_import
from builtins import str
from builtins import object
from qgis.PyQt.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, QFile, QFileInfo
from qgis.PyQt.QtWidgets import QDialog, QFormLayout, QAction, QFileDialog, QMessageBox, QApplication
from qgis.PyQt.QtGui import QIcon
# Initialize Qt resources from file resources.py
from . import resources
# Import the code for the dialog
from .pghydro_tools_dialog import PghydroToolsDialog
import os.path
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import time
from time import strftime
import codecs
import os, subprocess
import osgeo
from osgeo import gdal
from osgeo import ogr
from qgis.gui import QgsFieldComboBox, QgsMapLayerComboBox
from qgis.core import QgsDataSourceUri, QgsVectorLayer, QgsMapLayerProxyModel
class PghydroTools(object):
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'PghydroTools_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = PghydroToolsDialog()
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Pghydro Tools')
self.toolbar = self.iface.addToolBar(u'PghydroTools')
self.toolbar.setObjectName(u'PghydroTools')
self.dlg.pushButton_create_database.clicked.connect(self.create_database)
self.dlg.pushButton_connect_database.clicked.connect(self.connect_database)
self.dlg.pushButton_import_drainage_line.clicked.connect(self.import_drainage_line)
self.dlg.pushButton_import_drainage_area.clicked.connect(self.import_drainage_area)
self.dlg.pushButton_ExplodeDrainageLine.clicked.connect(self.ExplodeDrainageLine)
self.dlg.pushButton_MakeDrainageLineSimple.clicked.connect(self.MakeDrainageLineSimple)
self.dlg.pushButton_MakeDrainageLineValid.clicked.connect(self.MakeDrainageLineValid)
self.dlg.pushButton_Check_DrainageLineGeometryConsistencies.clicked.connect(self.Check_DrainageLineGeometryConsistencies)
self.dlg.pushButton_Check_DrainageLineTopologyConsistencies_1.clicked.connect(self.Check_DrainageLineTopologyConsistencies_1)
self.dlg.pushButton_Check_DrainageLineTopologyConsistencies_2.clicked.connect(self.Check_DrainageLineTopologyConsistencies_2)
self.dlg.pushButton_DeleteDrainageLineWithinDrainageLine.clicked.connect(self.DeleteDrainageLineWithinDrainageLine)
self.dlg.pushButton_BreakDrainageLines.clicked.connect(self.BreakDrainageLines)
self.dlg.pushButton_UnionDrainageLineValence2.clicked.connect(self.UnionDrainageLineValence2)
self.dlg.pushButton_Execute_Network_Topology.clicked.connect(self.Execute_Network_Topology)
self.dlg.pushButton_UpdateShorelineEndingPoint.clicked.connect(self.UpdateShorelineEndingPoint)
self.dlg.pushButton_UpdateShorelineStartingPoint.clicked.connect(self.UpdateShorelineStartingPoint)
self.dlg.pushButton_Check_Execute_Flow_Direction.clicked.connect(self.Execute_Flow_Direction)
self.dlg.pushButton_ExplodeDrainageArea.clicked.connect(self.ExplodeDrainageArea)
self.dlg.pushButton_MakeDrainageAreaSimple.clicked.connect(self.MakeDrainageAreaSimple)
self.dlg.pushButton_MakeDrainageAreaValid.clicked.connect(self.MakeDrainageAreaValid)
self.dlg.pushButton_Check_DrainageAreaGeometryConsistencies.clicked.connect(self.Check_DrainageAreaGeometryConsistencies)
self.dlg.pushButton_RemoveDrainageAreaOverlap.clicked.connect(self.RemoveDrainageAreaOverlap)
self.dlg.pushButton_DeleteDrainageAreaWithinDrainageArea.clicked.connect(self.DeleteDrainageAreaWithinDrainageArea)
self.dlg.pushButton_Check_DrainageAreaTopologyConsistencies.clicked.connect(self.Check_DrainageAreaTopologyConsistencies)
self.dlg.pushButton_Union_DrainageAreaNoDrainageLine.clicked.connect(self.Union_DrainageAreaNoDrainageLine)
self.dlg.pushButton_Check_DrainageAreaDrainageLineConsistencies.clicked.connect(self.Check_DrainageAreaDrainageLineConsistencies)
self.dlg.pushButton_Principal_Procedure.clicked.connect(self.Principal_Procedure)
self.dlg.pushButton_UpdateExportTables.clicked.connect(self.UpdateExportTables)
self.dlg.pushButton_Start_Systematize_Hydronym.clicked.connect(self.Start_Systematize_Hydronym)
self.dlg.pushButton_Systematize_Hydronym.clicked.connect(self.Systematize_Hydronym)
self.dlg.pushButton_Check_ConfluenceHydronym.clicked.connect(self.Check_ConfluenceHydronym)
self.dlg.pushButton_Update_OriginalHydronym.clicked.connect(self.Update_OriginalHydronym)
self.dlg.pushButton_Stop_Systematize_Hydronym.clicked.connect(self.Stop_Systematize_Hydronym)
self.dlg.pushButton_create_role.clicked.connect(self.Create_Role)
self.dlg.pushButton_check_role.clicked.connect(self.Check_Role)
self.dlg.pushButton_enable_role.clicked.connect(self.Enable_Role)
self.dlg.pushButton_disable_role.clicked.connect(self.Disable_Role)
self.dlg.pushButton_drop_role.clicked.connect(self.Drop_Role)
self.dlg.pushButton_turn_on_audit.clicked.connect(self.Turn_ON_Audit)
self.dlg.pushButton_turn_off_audit.clicked.connect(self.Turn_OFF_Audit)
self.dlg.pushButton_reset_drainage_line_audit.clicked.connect(self.Reset_Drainage_Line_Audit)
self.dlg.pushButton_reset_drainage_area_audit.clicked.connect(self.Reset_Drainage_Area_Audit)
self.dlg.input_drainage_line_table_MapLayerComboBox.currentIndexChanged.connect(self.input_drainage_line_table_attribute_name_select)
def tr(self, 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('PghydroTools', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
#add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_menu:
self.iface.addPluginToDatabaseMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/PghydroTools/icon.png'
self.add_action(
icon_path,
text=self.tr(u'PgHydro Tools'),
callback=self.run,
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginDatabaseMenu(
self.tr(u'&Pghydro Tools'),
action)
#self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
###Database Editing
def create_database(self):
host = self.dlg.lineEdit_host.text()
port = self.dlg.lineEdit_port.text()
dbname = self.dlg.lineEdit_base.text()
user = self.dlg.lineEdit_user.text()
password = self.dlg.lineEdit_password.text()
postgres = 'postgres'
connection_str_postgres = 'host={0} port={1} dbname={2} user={3} password={4}'.format(host, port, postgres, user, password)
self.print_console_message("Creating Spatial Database and Pghydro Extension. Please, Wait...")
try:
conn = psycopg2.connect(connection_str_postgres)
conn.autocommit = True
cur = conn.cursor()
createdatabase = """
CREATE DATABASE """+dbname+""";
"""
cur.execute(createdatabase)
self.print_console_message("Database Created With Success!\n")
cur.close()
conn.close()
create_spatial_database = """
CREATE EXTENSION postgis;
"""
create_pghydro = """
CREATE EXTENSION pghydro;
"""
create_pgh_consistency = """
CREATE EXTENSION pgh_consistency;
"""
create_pgh_output = """
CREATE EXTENSION pgh_output;
"""
self.execute_sql(create_spatial_database)
self.print_console_message("Spatial Database Successfully Created!\n")
self.execute_sql(create_pghydro)
self.print_console_message("PgHydro Extension Successfully Created!\n")
self.execute_sql(create_pgh_consistency)
self.print_console_message("PgHydro Consistency Extension Successfully Created!\n")
self.execute_sql(create_pgh_output)
self.print_console_message("PgHydro Output Extension Successfully Created!\n")
self.print_console_message("Spatial Database and Pghydro Extensions Successfully Created!\n")
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def connect_database(self):
host = self.dlg.lineEdit_host.text()
port = self.dlg.lineEdit_port.text()
dbname = self.dlg.lineEdit_base.text()
schema = self.dlg.lineEdit_schema.text()
user = self.dlg.lineEdit_user.text()
password = self.dlg.lineEdit_password.text()
connection_str = 'host={0} port={1} dbname={2} user={3} password={4}'.format(host, port, dbname, user, password)
self.print_console_message('Connecting to Database. Please, wait...')
try:
conn = psycopg2.connect(connection_str)
conn.close()
self.print_console_message('Database Successfully Connected!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def execute_sql(self, sql):
host = self.dlg.lineEdit_host.text()
port = self.dlg.lineEdit_port.text()
dbname = self.dlg.lineEdit_base.text()
schema = self.dlg.lineEdit_schema.text()
user = self.dlg.lineEdit_user.text()
password = self.dlg.lineEdit_password.text()
connection_str = 'host={0} port={1} dbname={2} user={3} password={4}'.format(host, port, dbname, user, password)
try:
conn = None
conn = psycopg2.connect(connection_str)
conn.autocommit = True
cur = conn.cursor()
cur.execute(sql)
cur.close()
conn.close()
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def return_sql(self, sql):
host = self.dlg.lineEdit_host.text()
port = self.dlg.lineEdit_port.text()
dbname = self.dlg.lineEdit_base.text()
schema = self.dlg.lineEdit_schema.text()
user = self.dlg.lineEdit_user.text()
password = self.dlg.lineEdit_password.text()
connection_str = 'host={0} port={1} dbname={2} user={3} password={4}'.format(host, port, dbname, user, password)
try:
conn = None
conn = psycopg2.connect(connection_str)
conn.autocommit = True
cur = conn.cursor()
cur.execute(sql)
result = str(cur.fetchone()[0])
cur.close()
conn.close()
return result
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def print_console_message(self, message):
self.dlg.console.append(time.strftime("\n%d.%m.%Y"+" - "+"%H"+":"+"%M"+":"+"%S"))
self.dlg.console.append(message)
self.dlg.console.repaint()
###Input Drainage Line
def import_drainage_line(self):
try:
layers = self.iface.mapCanvas().layers()
selectedLayerIndex = self.dlg.input_drainage_line_table_MapLayerComboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
input_drainage_line_table_schema = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).schema()
input_drainage_line_table = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).table()
input_drainage_line_table_attribute_name = self.dlg.input_drainage_line_table_attribute_name_MapLayerComboBox.currentText()
input_drainage_line_table_attribute_geom = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).geometryColumn ()
self.print_console_message('Importing Drainage Lines. Please, wait...\n')
self.dlg.console.append('SCHEMA: '+input_drainage_line_table_schema)
self.dlg.console.append('GEOMETRY TABLE: '+input_drainage_line_table)
self.dlg.console.append('RIVER NAME COLUMN: '+input_drainage_line_table_attribute_name)
self.dlg.console.append('GEOMETRY COLUMN: '+input_drainage_line_table_attribute_geom)
sql= """
SELECT pghydro.pghfn_input_data_drainage_line('"""+input_drainage_line_table_schema+"""','"""+input_drainage_line_table+"""','"""+input_drainage_line_table_attribute_geom+"""','"""+input_drainage_line_table_attribute_name+"""');
"""
self.execute_sql(sql)
self.Vacuum_Database()
self.print_console_message('Drainage Lines Successfully Imported!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
###Input Drainage Area
def import_drainage_area(self):
try:
self.print_console_message('Importing Drainage Areas. Please, wait...\n')
layers = self.iface.mapCanvas().layers()
selectedLayerIndex = self.dlg.input_drainage_area_table_MapLayerComboBox.currentIndex()
selectedLayer = layers[selectedLayerIndex]
input_drainage_area_table_schema = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).schema()
input_drainage_area_table = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).table()
input_drainage_area_table_attribute_geom = QgsDataSourceUri(selectedLayer.dataProvider().dataSourceUri()).geometryColumn ()
self.print_console_message('Updating Drainage Areas. Please, wait...\n')
self.dlg.console.append('SCHEME: '+input_drainage_area_table_schema)
self.dlg.console.append('GEOMETRY TABLE: '+input_drainage_area_table)
self.dlg.console.append('GEOMETRY COLUMN: '+input_drainage_area_table_attribute_geom)
self.dlg.console.repaint()
sql = """
SELECT pghydro.pghfn_input_data_drainage_area('"""+input_drainage_area_table_schema+"""','"""+input_drainage_area_table+"""','"""+input_drainage_area_table_attribute_geom+"""');
"""
self.execute_sql(sql)
self.Vacuum_Database()
self.print_console_message('Drainage Areas Successfully Imported!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
#####Consistency Drainage Line
def Check_DrainageLineIsNotSingle(self):
try:
self.print_console_message("Checking Non-Single Geometries. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelineisnotsingle;
"""
result = self.return_sql(sql)
self.dlg.console.append("Non-Single Geometries: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineIsNotSingle.setText(result)
self.dlg.lineEdit_Check_DrainageLineIsNotSingle.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_ExplodeDrainageLine.setEnabled(True)
else:
self.dlg.pushButton_ExplodeDrainageLine.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def ExplodeDrainageLine(self):
try:
self.print_console_message("Exploding Non-Single Geometries. Please, wait...")
sql = """
SELECT pgh_consistency.pghfn_explodedrainageline();
"""
self.execute_sql(sql)
self.dlg.lineEdit_Check_DrainageLineIsNotSingle.setText('')
self.dlg.lineEdit_Check_DrainageLineIsNotSingle.repaint()
self.dlg.pushButton_ExplodeDrainageLine.setEnabled(False)
self.print_console_message('Geometries Successfully Exploded!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineIsNotSimple(self):
try:
self.print_console_message("Checking Non-Simple Geometries. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelineisnotsimple;
"""
result = self.return_sql(sql)
self.dlg.console.append("Non-Simple Geometries: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineIsNotSimple.setText(result)
self.dlg.lineEdit_Check_DrainageLineIsNotSimple.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_MakeDrainageLineSimple.setEnabled(True)
else:
self.dlg.pushButton_MakeDrainageLineSimple.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def MakeDrainageLineSimple(self):
try:
self.print_console_message("Simplifying Non-Simple Geometries. Please, wait...")
sql = """
SELECT pgh_consistency.pghfn_makedrainagelinesimple();
"""
self.execute_sql(sql)
self.dlg.lineEdit_Check_DrainageLineIsNotSimple.setText('')
self.dlg.lineEdit_Check_DrainageLineIsNotSimple.repaint()
self.dlg.pushButton_MakeDrainageLineSimple.setEnabled(False)
self.print_console_message('Non-Simple Geometries Successfully Simpliflyed!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineIsNotValid(self):
try:
self.print_console_message("Checking Invalid Geometries. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelineisnotvalid;
"""
result = self.return_sql(sql)
self.dlg.console.append("Invalid Geometries: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineIsNotValid.setText(result)
self.dlg.lineEdit_Check_DrainageLineIsNotValid.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_MakeDrainageLineValid.setEnabled(True)
else:
self.dlg.pushButton_MakeDrainageLineValid.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def MakeDrainageLineValid(self):
try:
self.print_console_message("Validating Invalid Geometries. Please, wait...")
sql = """
SELECT pgh_consistency.pghfn_makedrainagelinevalid();
"""
self.execute_sql(sql)
self.dlg.lineEdit_Check_DrainageLineIsNotValid.setText('')
self.dlg.lineEdit_Check_DrainageLineIsNotValid.repaint()
self.dlg.pushButton_MakeDrainageLineValid.setEnabled(False)
self.print_console_message('Geometries Successfully Validated!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineGeometryConsistencies(self):
DrainageLinePrecision = self.dlg.lineEdit_DrainageLinePrecision.text()
DrainageLineOffset = self.dlg.lineEdit_DrainageLineOffset.text()
try:
self.print_console_message('Checking Geometric Consistency. Please, wait...\n')
sql1 = """
DROP INDEX IF EXISTS pghydro.drn_gm_idx;
ALTER TABLE pghydro.pghft_drainage_line DROP CONSTRAINT IF EXISTS drn_pk_pkey;
"""
sql2 = """
SELECT pgh_consistency.pghfn_MakeSnapToGridDrainageLine("""+DrainageLinePrecision+""");
SELECT pgh_consistency.pghfn_removereapetedpointsdrainageline();
SELECT pgh_consistency.pghfn_DeleteDrainageLineGeometryEmpty();
"""
sql3 = """
SELECT setval(('pghydro.drn_pk_seq'::text)::regclass, """+DrainageLineOffset+""", false);
UPDATE pghydro.pghft_drainage_line
SET drn_pk = NEXTVAL('pghydro.drn_pk_seq');
CREATE INDEX drn_gm_idx ON pghydro.pghft_drainage_line USING GIST(drn_gm);
ALTER TABLE pghydro.pghft_drainage_line ADD CONSTRAINT drn_pk_pkey PRIMARY KEY (drn_pk);
"""
sql4 = """
SELECT pgh_consistency.pghfn_UpdateDrainageLineConsistencyGeometryTables();
"""
self.Turn_OFF_Audit()
self.Vacuum_Database()
self.execute_sql(sql1)
self.execute_sql(sql2)
self.execute_sql(sql3)
self.execute_sql(sql4)
self.Check_DrainageLineIsNotSingle()
self.Check_DrainageLineIsNotSimple()
self.Check_DrainageLineIsNotValid()
self.Vacuum_Database()
self.print_console_message('Geometric Consistency Successfully Checked!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineWithinDrainageLine(self):
try:
self.print_console_message("Checking Geometry WITHIN Geometry. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelinewithindrainageline;
"""
result = self.return_sql(sql)
self.dlg.console.append("Geometry WITHIN Geometry: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineWithinDrainageLine.setText(result)
self.dlg.lineEdit_Check_DrainageLineWithinDrainageLine.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_DeleteDrainageLineWithinDrainageLine.setEnabled(True)
else:
self.dlg.pushButton_DeleteDrainageLineWithinDrainageLine.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def DeleteDrainageLineWithinDrainageLine(self):
try:
self.print_console_message("Deleting Geometry WITHIN Geometry. Please, wait...")
sql = """
SELECT pgh_consistency.pghfn_deletedrainagelinewithindrainageline();
"""
self.execute_sql(sql)
self.dlg.lineEdit_Check_DrainageLineWithinDrainageLine.setText('')
self.dlg.lineEdit_Check_DrainageLineWithinDrainageLine.repaint()
self.dlg.pushButton_DeleteDrainageLineWithinDrainageLine.setEnabled(False)
self.print_console_message('Geometries Successfully Deleted!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineOverlapDrainageLine(self):
try:
self.print_console_message("Checking Geometry OVERLAP Geometry. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelineoverlapdrainageline;
"""
result = self.return_sql(sql)
self.dlg.console.append("Geometry OVERLAP Geometry: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineOverlapDrainageLine.setText(result)
self.dlg.lineEdit_Check_DrainageLineOverlapDrainageLine.repaint()
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineLoops(self):
try:
self.print_console_message("Checking LOOPS. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelineloops;
"""
result = self.return_sql(sql)
self.dlg.console.append("Geometries with LOOPS: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineLoops.setText(result)
self.dlg.lineEdit_Check_DrainageLineLoops.repaint()
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineTopologyConsistencies_1(self):
try:
self.print_console_message('Checking Topological Consistency Part I. Please, wait...\n')
sql = """
SELECT pgh_consistency.pghfn_UpdateDrainageLineConsistencyTopologyTables_1();
"""
self.Turn_OFF_Audit()
self.execute_sql(sql)
self.Check_DrainageLineWithinDrainageLine()
self.Check_DrainageLineOverlapDrainageLine()
self.Check_DrainageLineLoops()
self.Vacuum_Database()
self.print_console_message('Topological Consistency Part I Successfully Checked!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineCrossDrainageLine(self):
try:
self.print_console_message("Checking Geometry CROSS Geometry. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelinecrossdrainageline;
"""
result = self.return_sql(sql)
self.dlg.console.append("Geometry CROSS Geometry: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineCrossDrainageLine.setText(result)
self.dlg.lineEdit_Check_DrainageLineCrossDrainageLine.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_BreakDrainageLines.setEnabled(True)
else:
self.dlg.pushButton_BreakDrainageLines.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineTouchDrainageLine(self):
try:
self.print_console_message("Checking Geometry TOUCH Geometry. Please, wait...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_drainagelinetouchdrainageline;
"""
result = self.return_sql(sql)
self.dlg.console.append("Geometry TOUCH Geometry: ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_DrainageLineTouchDrainageLine.setText(result)
self.dlg.lineEdit_Check_DrainageLineTouchDrainageLine.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_BreakDrainageLines.setEnabled(True)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_DrainageLineTopologyConsistencies_2(self):
try:
self.print_console_message('Checking Topological Consistency Part II. Please wait...\n')
sql = """
SELECT pgh_consistency.pghfn_UpdateDrainageLineConsistencyTopologyTables_2();
"""
self.Turn_OFF_Audit()
self.execute_sql(sql)
self.Check_DrainageLineCrossDrainageLine()
self.Check_DrainageLineTouchDrainageLine()
self.Vacuum_Database()
self.print_console_message('Topological Consistency Part II Successfully Checked!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def BreakDrainageLines(self):
DrainageLinePrecision = self.dlg.lineEdit_DrainageLinePrecision.text()
try:
self.print_console_message("Breaking Geometries. Please, wait...")
sql1 = """
SELECT pgh_consistency.pghfn_CreateDrainageLineVertexIntersections("""+DrainageLinePrecision+""");
"""
sql2 = """
SELECT pgh_consistency.pghfn_BreakDrainageLine();
"""
self.execute_sql(sql1)
self.execute_sql(sql2)
self.print_console_message('Geometries Successfully Broken!\n')
self.dlg.lineEdit_Check_DrainageLineCrossDrainageLine.setText('')
self.dlg.lineEdit_Check_DrainageLineCrossDrainageLine.repaint()
self.dlg.lineEdit_Check_DrainageLineTouchDrainageLine.setText('')
self.dlg.lineEdit_Check_DrainageLineTouchDrainageLine.repaint()
self.dlg.pushButton_BreakDrainageLines.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_PointValenceValue2(self):
try:
self.print_console_message("Checking Pseudo-Nodes (Valence = 2)...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_pointvalencevalue2;
"""
result = self.return_sql(sql)
self.dlg.console.append("Pseudo-Nodes (Valence = 2): ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_PointValenceValue2.setText(result)
self.dlg.lineEdit_Check_PointValenceValue2.repaint()
if int('0' if result =='' else result) > 0:
self.dlg.pushButton_UnionDrainageLineValence2.setEnabled(True)
else:
self.dlg.pushButton_UnionDrainageLineValence2.setEnabled(False)
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def UnionDrainageLineValence2(self):
try:
self.print_console_message('Uniting Drainage Lines. Please, wait...\n')
sql = """
SELECT pgh_consistency.pghfn_uniondrainagelinevalence2();
"""
self.execute_sql(sql)
self.dlg.lineEdit_Check_PointValenceValue2.setText('')
self.dlg.lineEdit_Check_PointValenceValue2.repaint()
self.dlg.pushButton_UnionDrainageLineValence2.setEnabled(False)
self.print_console_message('Drainage Lines Successfully United!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Check_PointValenceValue4(self):
try:
self.print_console_message("Checking Multiple Confluences (Valence = 4)...")
sql = """
SELECT count(id)
FROM pgh_consistency.pghft_pointvalencevalue4;
"""
result = self.return_sql(sql)
self.dlg.console.append("Multiple Confluences (Valence = 4): ")
self.dlg.console.append(result)
self.dlg.console.repaint()
self.dlg.lineEdit_Check_PointValenceValue4.setText(result)
self.dlg.lineEdit_Check_PointValenceValue4.repaint()
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def Execute_Network_Topology(self):
DrainagePointOffset = self.dlg.lineEdit_DrainagePointOffset.text()
try:
self.print_console_message('Creating Drainage Line Network. Please, wait...\n')
sql1 = """
DROP INDEX IF EXISTS pghydro.drp_gm_idx;
DROP INDEX IF EXISTS pghydro.drn_gm_idx;
ALTER TABLE pghydro.pghft_drainage_line DROP CONSTRAINT IF EXISTS drn_pk_pkey;
"""
sql2 = """
SELECT pghydro.pghfn_assign_vertex_id("""+DrainagePointOffset+""");
"""
sql3 = """
SELECT pghydro.pghfn_CalculateValence();
"""
sql4 = """
DROP INDEX IF EXISTS pghydro.drn_gm_idx;
CREATE INDEX drn_gm_idx ON pghydro.pghft_drainage_line USING GIST(drn_gm);
DROP INDEX IF EXISTS pghydro.drp_gm_idx;
CREATE INDEX drp_gm_idx ON pghydro.pghft_drainage_point USING GIST(drp_gm);
ALTER TABLE pghydro.pghft_drainage_line ADD CONSTRAINT drn_pk_pkey PRIMARY KEY (drn_pk);
"""
sql5 = """
SELECT pgh_consistency.pghfn_updatedrainagelinenetworkconsistencytables();
"""
self.Turn_OFF_Audit()
self.execute_sql(sql1)
self.execute_sql(sql2)
self.execute_sql(sql3)
self.execute_sql(sql4)
self.execute_sql(sql5)
self.Check_PointValenceValue2()
self.Check_PointValenceValue4()
self.Vacuum_Database()
self.print_console_message('Drainage Line Network Successfully Created!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def UpdateShorelineEndingPoint(self):
UpdateShorelineEndingPoint = self.dlg.lineEdit_UpdateShorelineEndingPoint.text()
try:
self.print_console_message('Identifying "End Node". Please, wait...\n')
sql = """
SELECT pghydro.pghfn_UpdateShorelineEndingPoint("""+UpdateShorelineEndingPoint+""");
"""
self.execute_sql(sql)
self.print_console_message('"End Node" Successfully Identified!\n')
except:
self.print_console_message('ERROR\nCheck Database Input Parameters!')
def UpdateShorelineStartingPoint(self):
UpdateShorelineStartingPoint = self.dlg.lineEdit_UpdateShorelineStartingPoint.text()
try: