-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelectSubbasin.cs
126 lines (119 loc) · 4.55 KB
/
SelectSubbasin.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ArcGIS.Core.Data;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Core.Data.UtilityNetwork.Trace;
using System.Windows.Input;
namespace ArcSWAT3
{
public partial class SelectSubbasin : Form
{
private DelinForm _parent;
private string currentTool;
private FeatureLayer _wshedLayer;
private GlobalVars _gv;
private double meanArea;
private int areaIndx;
public SelectSubbasin(FeatureLayer wshedLayer, DelinForm parent, GlobalVars gv) {
InitializeComponent();
this._wshedLayer = wshedLayer;
this._parent = parent;
this._gv = gv;
this.checkBox.Checked = false;
this.groupBox.Visible = false;
this.areaButton.Checked = false;
this.percentButton.Checked = true;
this.threshold.Text = "5";
}
public async Task setup() {
this.areaIndx = await _gv.topo.getIndex(this._wshedLayer, Topology._AREA);
double area = 0;
var count = 0;
await QueuedTask.Run(() => {
using (RowCursor rowCursor = this._wshedLayer.Search(null)) {
while (rowCursor.MoveNext()) {
using (Row polygon = rowCursor.Current) {
area += Convert.ToDouble(polygon[this.areaIndx]);
count++;
}
}
}
});
if (count == 0) {
this.meanArea = 0;
} else {
this.meanArea = area / count;
}
this.currentTool = FrameworkApplication.CurrentTool;
//ICommand cmd = FrameworkApplication.GetPlugInWrapper("ArcSWAT3_PolygonTool") as ICommand;
//if ((cmd != null) && cmd.CanExecute(null))
// cmd.Execute(null);
}
private void saveButton_Click(object sender, EventArgs e) {
FrameworkApplication.CurrentTool = this.currentTool;
Close();
}
private async void cancelButton_Click(object sender, EventArgs e) {
FrameworkApplication.CurrentTool = this.currentTool;
Close();
await MapView.Active.ClearSketchAsync();
await QueuedTask.Run(() => {
this._wshedLayer.ClearSelection();
});
}
private void checkBox_CheckedChanged(object sender, EventArgs e) {
this.groupBox.Visible = this.checkBox.Checked;
}
private async void pushButton_Click(object sender, EventArgs e) {
double threshold, thresholdM2;
var num = this.threshold.Text;
if (string.IsNullOrEmpty(num)) {
Utils.error("No threshold is set", this._gv.isBatch);
return;
}
try {
threshold = Double.Parse(num);
}
catch (Exception) {
Utils.error(string.Format("Cannot parse {0} as a number", num), this._gv.isBatch);
return;
}
if (this.areaButton.Checked) {
thresholdM2 = threshold * 10000;
} else {
thresholdM2 = this.meanArea * threshold / 100;
}
Selection selection = null;
IReadOnlyList<long> selected = new List<long>();
await QueuedTask.Run(() => {
selection = this._wshedLayer.GetSelection();
selected = selection.GetObjectIDs();
var rows = new List<long>();
using (RowCursor rowCursor = this._wshedLayer.Search(null)) {
while (rowCursor.MoveNext()) {
using (Row polygon = rowCursor.Current) {
var area = Convert.ToDouble(polygon[this.areaIndx]);
if (area < thresholdM2) {
var id = polygon.GetObjectID();
if (!selected.Contains(id)) {
rows.Add(id);
}
}
}
}
}
selection.Add(rows);
this._wshedLayer.SetSelection(selection);
});
}
}
}