-
Notifications
You must be signed in to change notification settings - Fork 1
/
arcswat.py.cs
709 lines (666 loc) · 31.6 KB
/
arcswat.py.cs
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
using System.Diagnostics;
using System.Collections.Generic;
using System;
using System.IO;
using Path = System.IO.Path;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using System.Threading.Tasks;
using ArcGIS.Core.Geometry;
using LinearUnit = ArcGIS.Core.Geometry.LinearUnit;
using ArcGIS.Core.Data;
using ArcGIS.Core.Internal.CIM;
using ArcGIS.Desktop.Layouts;
using ArcGIS.Core.Data.Raster;
using ArcGIS.Desktop.Core.Geoprocessing;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Shapes;
using ArcGIS.Desktop.Internal.Framework;
namespace ArcSWAT3
{
// ArcGIS Pro plugin to prepare geographic data for SWAT Editor.
public class ArcSWAT
{
public bool _demIsProcessed;
public GlobalVars _gv;
public MainForm _odlg;
public DelinForm delin;
public HRUs hrus;
public bool loadFailed;
public string plugin_dir;
public object translator;
public Visualise vis;
public string _SWATEDITORVERSION = Parameters._SWATEDITORVERSION;
public static string @__version__ = "1.0.0";
public ArcSWAT() {
//object locale;
// this import is a dependency on a Cython produuced .pyd file which will fail if the wrong architecture
// and so gives an immediate exit before the plugin is loaded
//# flag to show if init ran successfully
this.loadFailed = false;
// if not TYPE_CHECKING:
// try:
// from . import polygonizeInC2 # @UnusedImport @UnresolvedImport
// except Exception:
// Utils.loginfo("Failed to load Cython module: wrong architecture?")
// self.loadFailed = True
// return
// uncomment next line for debugging
// import pydevd; pydevd.settrace()
// initialize plugin directory
//# plugin directory
//this.plugin_dir = os.path.dirname(@__file__);
//// add to PYTHONPATH
//sys.path.append(this.plugin_dir);
//var settings = QSettings();
// initialize locale
// in testing with a dummy iface object this settings value can be None
//try {
// locale = settings.value("locale/userLocale")[0:2:];
//} catch (Exception) {
// locale = "en";
//}
//var localePath = os.path.join(this.plugin_dir, "i18n", "ArcSWAT_{}.qm".format(locale));
//// set default behaviour for loading files with no CRS to prompt - the safest option
//QSettings().setValue("Projections/defaultBehaviour", "prompt");
////# translator
//if (os.path.exists(localePath)) {
// this.translator = QTranslator();
// this.translator.load(localePath);
// if (qVersion() > "4.3.3") {
// QCoreApplication.installTranslator(this.translator);
// }
//}
this._gv = null;
// font = QFont("MS Shell Dlg 2", 8)
// Create the dialog (after translation) and keep reference
this._odlg = new MainForm(this);
//self._odlg.setWindowFlags(self._odlg.windowFlags() & ~Qt.WindowContextHelpButtonHint & Qt.WindowMinimizeButtonHint)
// TODO: this._odlg.Move(0, 0);
//=======================================================================
// font = self._odlg.font()
// fm = QFontMetrics(font)
// txt = "The quick brown fox jumps over the lazy dog."
// family = font.family()
// size = font.pointSize()
// Utils.information("Family: {2}. Point size: {3}.\nWidth of "{0}" is {1} pixels.".format(txt, fm.width(txt), family, size), False)
//=======================================================================
this._odlg.Text = String.Format("ArcSWAT3 {0}", ArcSWAT.@__version__);
// flag used in initialising delineation form
this._demIsProcessed = false;
//# deineation window
this.delin = null;
//# create hrus window
this.hrus = null;
//# visualise window
this.vis = null;
Utils.openLog();
// report QGIS version
Utils.loginfo(String.Format("ArcSWAT version: {0}", ArcSWAT.@__version__));
}
// def initGui(self) -> None:
// """Create ArcSWAT button in the toolbar."""
// if self.loadFailed:
// return
// ## Action that will start plugin configuration
// self.action = QAction(
// QIcon(":/ArcSWAT/SWAT32.png"),
// u"ArcSWAT", self._iface.mainWindow())
// # connect the action to the run method
// self.action.triggered.connect(self.run)
//
// # Add toolbar button and menu item
// self._iface.addToolBarIcon(self.action)
// self._iface.addPluginToMenu(u"&ArcSWAT", self.action)
//
// def unload(self) -> None:
// """Remove the ArcSWAT menu item and icon."""
// # allow for it not to have been loaded
// try:
// self._iface.removePluginMenu(u"&ArcSWAT", self.action)
// self._iface.removeToolBarIcon(self.action)
// except Exception:
// pass
// Run ArcSWAT.
public async Task run() {
// make sure we clear data from previous runs
this.delin = null;
this.hrus = null;
this.vis = null;
// initially only new/existing project buttons visible if project not set
this._odlg.initButtons();
this._odlg.Show();
if (MapView.Active is not null) {
await setupProject(false);
}
}
// Create a new project.
public async Task newProject() {
var title = Utils.trans("Choose parent directory for project");
string parentDir = "";
using (FolderBrowserDialog dialog = new FolderBrowserDialog()) {
dialog.Description = title;
dialog.UseDescriptionForTitle = true;
if (dialog.ShowDialog() == DialogResult.OK) {
parentDir = dialog.SelectedPath;
} else { return; }
}
title = Utils.trans("Pleae provide a project name");
string projName = Interaction.InputBox(title, "", "", 10, 20);
if (string.IsNullOrEmpty(projName)) { return; }
CreateProjectSettings settings = new CreateProjectSettings() {
LocationPath = parentDir,
Name = projName,
TemplateType = TemplateType.Map
};
await Project.CreateAsync(settings);
while (MapView.Active is null) {
await Task.Delay(1000);
}
this._odlg.initButtons();
await this.setupProject(false);
this._gv.writeMasterProgress(0, 0);
}
// Open an existing project.
public async Task existingProject() {
var title = Utils.trans("Choose project (*.aprx) file");
var filtr = Utils.trans("Project files (*.aprx)|*.aprx");
string projPath = "";
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Title = title;
dlg.Filter = filtr;
dlg.RestoreDirectory = false;
if (dlg.ShowDialog() == DialogResult.OK) {
projPath = dlg.FileName;
} else { return; }
}
if (!string.IsNullOrEmpty(projPath)) {
await Project.OpenAsync(projPath);
while (MapView.Active is null) {
await Task.Delay(1000);
}
this._odlg.initButtons();
this._odlg.setProject("Restarting project ...");
await this.setupProject(false);
}
}
// Set up the project.
public async Task setupProject(
bool isBatch,
bool isHUC = false,
bool isHAWQS = false,
string logFile = null,
bool fromGRASS = false,
string TNCDir = "") {
//this._odlg.mainBox.setVisible(true);
//this._odlg.mainBox.setEnabled(false);
//this._odlg.setCursor(Qt.WaitCursor);
//this._odlg.projPath.setText("Restarting project ...");
//Utils.information(string.Format("isHUC initially {0}", isHUC), isBatch);
// now have project so initiate global vars
// if we do this earlier we cannot for example find the project database
this._gv = new GlobalVars(isBatch, isHUC, isHAWQS, logFile, fromGRASS, TNCDir);
var title = this._gv.projName;
Proj proj = this._gv.proj;
bool found;
// for isHUC etc parameters take precedence if set true
if (isHUC) {
proj.writeEntryBool(title, "delin/isHUC", isHUC);
} else {
(isHUC, found) = proj.readBoolEntry("", "delin/isHUC", false);
}
// # same as isHUC for isHAWQS
if (isHAWQS) {
proj.writeEntryBool(title, "delin/isHAWQS", isHAWQS);
} else {
(isHAWQS, found) = proj.readBoolEntry("", "delin/isHAWQS", false);
}
// # same as isHUC for fromGRASS
if (fromGRASS) {
proj.writeEntryBool(title, "delin/fromGRASS", fromGRASS);
} else {
(fromGRASS, found) = proj.readBoolEntry("", "delin/fromGRASS", false);
}
//TODO
//if (!string.IsNullOrEmpty(TNCDir)) {
// proj.writeEntry(title, "delin/TNCDir", TNCDir);
//} else {
// (TNCDir, found) = proj.readEntry("", "delin/TNCDir", "");
//}
//this._gv.plugin_dir = this.plugin_dir;
//this._odlg.projPath.repaint();
this._odlg._gv = this._gv;
this._odlg.checkReports();
await setLegendGroups();
// enable edit button if converted from Arc
int choice;
(choice, found) = proj.readNumEntry(title, "delin/fromArc", -1);
if (found) {
if (choice >= 0) { // NB values from convertFromArc.py, 0 for full, 1 for existing, 2 for no gis.
this._odlg.allowEdit();
}
}
this._gv.useGridModel = proj.readBoolEntry(title, "delin/useGridModel", false).Item1;
if (this._gv.useGridModel)
this._gv.gridSize = proj.readNumEntry(title, "delin/gridSize", 1).Item1;
//Add ArcSWAT3 styles to project
string path = Path.Combine(this._gv.addinPath, "ArcSWAT3.stylx");
var styleItem = ItemFactory.Instance.Create(path) as IProjectItem;
await QueuedTask.Run(() => Project.Current.AddItem(styleItem));
//ARCSWAT3 custom style
this._gv.arcSWAT3Style =
Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcSWAT3");
if (await this.demProcessed(proj)) {
this._demIsProcessed = true;
this._odlg.allowCreateHRU();
var hrus = new HRUs(this._gv, this._odlg.reportsCombo, this);
//result = hrus.tryRun()
//if result == 1:
if (hrus.HRUsAreCreated()) {
this._odlg.showReports();
this._odlg.allowEdit();
}
}
var outputDb = Utils.join(this._gv.tablesOutDir, Parameters._OUTPUTDB);
if (this._gv.forTNC) {
outputDb = outputDb.Replace(".mdb", ".sqlite");
}
if (File.Exists(outputDb)) {
this._odlg.allowVisualise();
// TODO: this.loadVisualisationLayers();
}
this._odlg.setProject(this._gv.projDir);
//this._odlg.mainBox.setEnabled(true);
//this._odlg.setCursor(Qt.ArrowCursor);
}
// Run parameters form.
public void runParams() {
var @params = new Parameters(this._gv);
@params.run();
}
// Run the delineation dialog.
public void doDelineation() {
//Debug.Assert(this._gv is not null);
// avoid getting second window
if (this.delin is not null && this.delin.Enabled) {
this.delin.Close();
}
this.delin = new DelinForm(this._gv, this._demIsProcessed, this);
//Debug.Assert(this.delin is not null);
this.delin.Show();
}
public void postDelineation(bool result) {
if (result == true && this._gv.isDelinDone()) {
this._odlg.allowCreateHRU();
// remove old data so cannot be reused
var basinsdataTable = this._gv.isHUC || this._gv.isHAWQS ? "BASINSDATAHUC1" : "BASINSDATA1";
this._gv.db.clearTable(basinsdataTable);
// make sure HRUs starts from scratch
this.hrus = null;
} else {
this._demIsProcessed = false;
this._odlg.initButtons();
}
//this._odlg.BringToFront();
}
// Run the HRU creation dialog.
public void doCreateHRUs() {
//Debug.Assert(this._gv is not null);
// avoid getting second window
if (this.hrus is not null && this.hrus._dlg.Enabled) {
this.hrus._dlg.Close();
}
this.hrus = new HRUs(this._gv, this._odlg.reportsCombo, this);
//Debug.Assert(this.hrus is not null);
// non modal
this.hrus.run();
}
public void postCreateHRUs(bool result) {
//if (result == true && this._gv.isHRUsDone()) {
if (result) {
this._odlg.allowEdit();
}
}
//
// Return true if we can proceed with HRU creation.
//
// Return false if any required project setting is not found
// in the project file
// Return true if:
// Using existing watershed and watershed grid exists and
// is newer than dem
// or
// Not using existing watershed and filled dem exists and
// is no older than dem, and
// watershed shapefile exists and is no older than filled dem
//
public async Task<bool> demProcessed(Proj proj) {
var title = this._gv.projName;
//var root = proj.layerTreeRoot();
string demFile;
bool found;
(demFile, found) = proj.readEntry(title, "delin/DEM", "");
if (!found || string.IsNullOrEmpty(demFile) || !File.Exists(demFile)) {
Utils.loginfo("demProcessed failed: no DEM");
return false;
}
var demLayer = (await Utils.getLayerByFilename(demFile, FileTypes._DEM, this._gv, null, Utils._WATERSHED_GROUP_NAME)).Item1 as RasterLayer;
if (demLayer is null) {
Utils.loginfo("demProcessed failed: no DEM layer");
return false;
}
this._gv.demFile = demFile;
await QueuedTask.Run(() => {
// set extent to DEM as otherwise defaults to full globe
var demExtent = demLayer.QueryExtent();
var map = MapView.Active.Map;
map.SetCustomFullExtent(demExtent);
dynamic nodata = demLayer.GetRaster().GetNoDataValue();
var typ = nodata.GetType();
if (typ.IsArray) {
this._gv.elevationNoData = Convert.ToDouble(nodata[0]);
} else {
this._gv.elevationNoData = Convert.ToDouble(nodata);
}
});
var crsProject = await QueuedTask.Run(() => demLayer.GetSpatialReference());
var units = crsProject.Unit;
var factor = units.Name == "Meter" ? 1.0 : units.Name == "Foot" ? 0.3048 : 0.0;
if (factor == 0) {
Utils.loginfo(string.Format("demProcessed failed: units are {0}", units));
return false;
}
var XYSizes = await QueuedTask.Run<Tuple<double, double>>(() => {
return demLayer.GetRaster().GetMeanCellSize();
});
this._gv.cellArea = XYSizes.Item1 * XYSizes.Item2 * factor * factor;
// hillshade
//Delineation.addHillshade(demFile, root, demLayer, this._gv);
string outletFile;
(outletFile, found) = proj.readEntry(title, "delin/outlets", "");
FeatureLayer outletLayer = null;
if (found && !string.IsNullOrEmpty(outletFile) && File.Exists(outletFile)) {
var ft = this._gv.isHUC || this._gv.isHAWQS ? FileTypes._OUTLETSHUC : FileTypes._OUTLETS;
outletLayer = (await Utils.getLayerByFilename(outletFile, ft, this._gv, null, Utils._WATERSHED_GROUP_NAME)).Item1 as FeatureLayer;
if (outletLayer is null) {
Utils.loginfo("demProcessed failed: no outlet layer");
return false;
}
}
this._gv.outletFile = outletFile;
this._gv.existingWshed = proj.readBoolEntry(title, "delin/existingWshed", false).Item1;
this._gv.useGridModel = proj.readBoolEntry(title, "delin/useGridModel", false).Item1;
string streamFile;
(streamFile, found) = proj.readEntry(title, "delin/net", "");
if (!found || string.IsNullOrEmpty(streamFile) || !File.Exists(streamFile)) {
Utils.loginfo("demProcessed failed: no stream reaches shapefile");
return false;
}
this._gv.streamFile = streamFile;
string wshedFile;
(wshedFile, found) = proj.readEntry(title, "delin/wshed", "");
if (!found || string.IsNullOrEmpty(wshedFile) || !File.Exists(wshedFile)) {
Utils.loginfo("demProcessed failed: no subbasins shapefile");
return false;
}
var wshedTime = File.GetLastWriteTime(wshedFile);
var wshedLayer = (await Utils.getLayerByFilename(wshedFile, FileTypes._WATERSHED, this._gv, null, Utils._WATERSHED_GROUP_NAME)).Item1 as FeatureLayer;
if (wshedLayer is null) {
Utils.loginfo("demProcessed failed: no watershed layer");
return false;
};
this._gv.wshedFile = wshedFile;
string extraOutletFile;
(extraOutletFile, found) = proj.readEntry(title, "delin/extraOutlets", "");
FeatureLayer extraOutletLayer = null;
if (found && !string.IsNullOrEmpty(extraOutletFile) && File.Exists(extraOutletFile)) {
extraOutletLayer = (await Utils.getLayerByFilename(extraOutletFile, FileTypes._OUTLETS, this._gv, null, Utils._WATERSHED_GROUP_NAME)).Item1 as FeatureLayer;
if (extraOutletLayer is null) {
Utils.loginfo("demProcessed failed: no extra outlet layer");
return false;
}
}
this._gv.extraOutletFile = extraOutletFile;
var @base = Path.ChangeExtension(demFile, null);
this._gv.slopeFile = @base + "slp.tif";
// GRASS slope file should be based on original DEM
if (this._gv.fromGRASS && this._gv.slopeFile.EndsWith("_burnedslp.tif")) {
var unburnedslp = this._gv.slopeFile.Replace("_burnedslp.tif", "slp.tif");
if (File.Exists(unburnedslp)) {
this._gv.slopeFile = unburnedslp;
}
}
// if slope file was based on burned-in dem, replace with one produced without burning in
var noBurnSlopeFile = @base + "slope.tif";
if (Utils.isUpToDate(this._gv.slopeFile, noBurnSlopeFile)) {
this._gv.slopeFile = noBurnSlopeFile;
}
if (!File.Exists(this._gv.slopeFile)) {
Utils.loginfo("demProcessed failed: no slope raster");
return false;
}
this._gv.basinFile = @base + "w.tif";
//if (this._gv.useGridModel) {
// this._gv.isBig = wshedLayer.featureCount() > 100000 || this._gv.forTNC;
// Utils.loginfo("isBig is {0}".format(this._gv.isBig));
//}
if (this._gv.existingWshed) {
if (!this._gv.useGridModel) {
if (!File.Exists(this._gv.basinFile)) {
Utils.loginfo("demProcessed failed: no basins raster");
return false;
// following checks that basins raster created after shapefile, since this is what TauDEM does
// but for existing watershed we should not care how the maps were created
// so we removed this check
// winfo = QFileInfo(self._gv.basinFile)
// # cannot use last modified times because subbasin field in wshed file changed after wfile is created
// wCreateTime = winfo.created()
// wshedCreateTime = wshedInfo.created()
// if not wshedCreateTime <= wCreateTime:
// Utils.loginfo("demProcessed failed: wFile not up to date for existing watershed")
// return False
}
}
} else {
this._gv.pFile = @base + "p.tif";
if (!File.Exists(this._gv.pFile)) {
Utils.loginfo("demProcessed failed: no p raster");
return false;
}
if (!this._gv.fromGRASS) {
var felFile = @base + "fel.tif";
if (!File.Exists(felFile)) {
Utils.loginfo("demProcessed failed: no filled raster");
return false;
}
var demTime = File.GetLastWriteTime(demFile);
var felTime = File.GetLastWriteTime(felFile);
if (!(demTime <= felTime && felTime <= wshedTime)) {
Utils.loginfo("demProcessed failed: not up to date");
return false;
}
if (!this._gv.useGridModel) {
this._gv.distFile = @base + "dist.tif";
if (!File.Exists(this._gv.distFile)) {
Utils.loginfo("demProcessed failed: no distance to outlet raster");
return false;
}
}
}
}
if (!await this._gv.topo.setUp0(demLayer, streamFile, this._gv.verticalFactor)) {
return false;
}
var streamLayer = (await Utils.getLayerByFilename(streamFile, FileTypes._STREAMS, this._gv, null, Utils._WATERSHED_GROUP_NAME)).Item1 as FeatureLayer;
if (streamLayer is null) {
Utils.loginfo("demProcessed failed: no stream reaches layer");
return false;
}
var basinIndex = await this._gv.topo.getIndex(wshedLayer, Topology._POLYGONID);
if (basinIndex < 0) {
Utils.loginfo("demProcessed failed: no PolygonId field in washed file");
return false;
}
await QueuedTask.Run(() => {
using (RowCursor rowCursor = wshedLayer.Search(null)) {
while (rowCursor.MoveNext()) {
using (Row polygon = rowCursor.Current) {
var basin = Convert.ToInt32(polygon[basinIndex]);
var centroid = GeometryEngine.Instance.Centroid(((Feature)polygon).GetShape());
this._gv.topo.basinCentroids[basin] = new Coordinate2D(centroid.X, centroid.Y);
}
}
}
});
// this can go wrong if eg the streams and watershed files exist but are inconsistent
try {
if (!await this._gv.topo.setUp(demLayer, streamFile, wshedFile, outletFile, extraOutletFile, this._gv, this._gv.existingWshed, false, this._gv.useGridModel, false)) {
// type: ignore
Utils.loginfo("demProcessed failed: topo setup failed");
return false;
}
if (this._gv.topo.inletLinks.Count == 0) {
// no inlets, so no need to expand subbasins layer legend
var subbasinsLayer = Utils.getLayerByLegend(FileTypes.legend(FileTypes._SUBBASINS));
//Debug.Assert(subbasinsLayer is not null);
if (subbasinsLayer != null) {
await QueuedTask.Run(() => subbasinsLayer.SetExpanded(false));
}
}
}
catch (Exception ex) {
Utils.loginfo(string.Format("demProcessed failed: topo setup raised exception: {0}", ex.Message));
return false;
}
return this._gv.isDelinDone();
}
// Legend groups are used to keep legend in reasonable order.
// Create them if necessary.
//
public static async Task setLegendGroups() {
var map = MapView.Active.Map;
await QueuedTask.Run(async () => {
var groups = new List<string> {
Utils._SLOPE_GROUP_NAME,
Utils._SOIL_GROUP_NAME,
Utils._LANDUSE_GROUP_NAME,
Utils._WATERSHED_GROUP_NAME,
Utils._RESULTS_GROUP_NAME,
Utils._ANIMATION_GROUP_NAME
};
foreach (string name in groups) {
var layer = Utils.getGroupLayerByName(name);
if (layer is null) {
LayerFactory.Instance.CreateGroupLayer(map, 0, name);
//layer.SetExpanded(true);
}
}
// clear anything left accidentally in animation group
await Utils.clearAnimationGroup();
// don't do this - hides landuse, soil and slope
//// move world hillshade layer to base of watershed group
//GroupLayer wshedGroup = Utils.getGroupLayerByName(Utils._WATERSHED_GROUP_NAME);
//if (wshedGroup is not null) {
// List<Layer> layers = MapView.Active.Map.GetLayersAsFlattenedList().Where(layer => layer.Name == "World Hillshade").ToList();
// if (layers.Count > 0 ) {
// map.MoveLayer(layers[0], wshedGroup, -1);
// }
//}
});
}
// Start the SWAT Editor, first setting its initial parameters.
public void startEditor() {
//Debug.Assert(this._gv is not null);
if (!File.Exists(this._gv.SWATEditorPath)) {
Utils.error(string.Format("Cannot find SWAT Editor {0}: is it installed?", this._gv.SWATEditorPath), this._gv.isBatch);
return;
}
this._gv.setSWATEditorParams();
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = this._gv.SWATEditorPath
}
};
process.Start();
process.WaitForExit();
var outputDb = Utils.join(this._gv.tablesOutDir, Parameters._OUTPUTDB);
if (this._gv.forTNC) {
outputDb = outputDb.Replace(".mdb", ".sqlite");
}
if (File.Exists(outputDb)) {
this._odlg.allowVisualise();
}
}
// Close the database connections and subsidiary forms.
public async Task finish(DialogResult result) {
if (result == DialogResult.OK) {
await Project.Current.SaveAsync();
}
Utils.loginfo("Closing databases");
try {
this.delin = null;
this.hrus = null;
this.vis = null;
if (this._gv is not null && this._gv.db is not null) {
if (this._gv.db.conn is not null) {
this._gv.db.conn.Close();
}
if (this._gv.db.connRef is not null) {
this._gv.db.connRef.Close();
if ((this._gv.isHUC || this._gv.isHAWQS) && this._gv.db.SSURGOConn is not null) {
this._gv.db.SSURGOConn.Close();
}
}
}
Utils.loginfo("Databases closed");
}
catch (Exception) {
}
}
// Run visualise form.
public async Task visualise() {
// avoid getting second window
if (this.vis is not null) {
await this.vis.doClose();
}
this.vis = new Visualise(this._gv);
Debug.Assert(this.vis is not null);
await this.vis.run();
}
//// If we have subs1.shp and riv1.shp and an empty watershed group then add these layers.
////
//// Intended for use after a no gis conversion from ArcSWAT.
//public virtual object loadVisualisationLayers() {
// Debug.Assert(this._gv is not null);
// var root = QgsProject.instance().layerTreeRoot();
// var wshedLayers = Utils.getLayersInGroup(Utils._WATERSHED_GROUP_NAME, root);
// // ad layers if we have empty Watershed group
// var addLayers = wshedLayers.Count == 0;
// if (addLayers) {
// var wshedFile = os.path.join(this._gv.shapesDir, "subs1.shp");
// var streamFile = os.path.join(this._gv.shapesDir, "riv1.shp");
// if (os.path.exists(wshedFile) && os.path.exists(streamFile)) {
// var group = root.findGroup(Utils._WATERSHED_GROUP_NAME);
// if (group is not null) {
// var proj = QgsProject.instance();
// var wshedLayer = QgsVectorLayer(wshedFile, "Subbasins", "ogr");
// wshedLayer = cast(QgsVectorLayer, proj.addMapLayer(wshedLayer, false));
// group.insertLayer(0, wshedLayer);
// // style file like wshed.qml but does not check for subbasins upstream frm inlets
// wshedLayer.loadNamedStyle(Utils.join(this._gv.plugin_dir, "wshed2.qml"));
// var streamLayer = QgsVectorLayer(streamFile, "Streams", "ogr");
// streamLayer = cast(QgsVectorLayer, proj.addMapLayer(streamLayer, false));
// group.insertLayer(0, wshedLayer);
// streamLayer.loadNamedStyle(Utils.join(this._gv.plugin_dir, "stream.qml"));
// }
// }
// }
//}
}
}