-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
320 lines (247 loc) · 10.8 KB
/
Form1.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
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 DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
namespace Demo1
{
public partial class Form1 : Form
{
#region "Class level variables"
//the line layer
MapLineLayer lineLayer;
//the line feature set
FeatureSet lineF;
int lineID = 0;
//boolean variable for first time mouse click
Boolean firstClick = false;
//boolean variable for ski path drawing finished
Boolean SoilPathFinished = false;
#endregion
public Form1()
{
InitializeComponent();
appManager1.LoadExtensions();
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
map1.AddRasterLayer();
map1.ZoomToMaxExtent();
}
private void drawToolStripMenuItem_Click(object sender, EventArgs e)
{
//remove any existing path
foreach (IMapLineLayer existingPath in map1.GetLineLayers())
{
map1.Layers.Remove(existingPath);
}
//hikingpath is not finished
SoilPathFinished = false;
//change the mouse cursor
map1.Cursor = Cursors.Cross;
//initialize the polyline featureset
lineF = new FeatureSet(FeatureType.Line);
//set projection
lineF.Projection = map1.Projection;
//initialize the featureSet attribute table
DataColumn column = new DataColumn("ID");
lineF.DataTable.Columns.Add(column);
//add the featureSet as map layer
lineLayer = (MapLineLayer)map1.Layers.Add(lineF);
//implement the symbology
LineSymbolizer symbol = new LineSymbolizer(Color.Blue, 2);
lineLayer.Symbolizer = symbol;
//Set the legend text
lineLayer.LegendText = "Soil Profile";
//Before clicking the mouse first time
firstClick = true;
}
private void map1_MouseDown(object sender, MouseEventArgs e)
{
//if soil path is fininshed, don't draw any line
if (SoilPathFinished == true)
return;
if (e.Button == MouseButtons.Left)
{
//left click - fill array of coordinates
//coordinate of clicked point
Coordinate coord = map1.PixelToProj(e.Location);
//first time left click - create empty line feature
if (firstClick)
{
MessageBox.Show("Click on the map to draw a polyline. Right click to stop drawing.");
//Create a new List called lineArray.
//This list will store the Coordinates
//We are going to store the mouse click coordinates into this array.
List<Coordinate> lineArray = new List<Coordinate>();
//Create an instance for LineString class.
//We need to pass collection of list coordinates
LineString lineGeometry = new LineString(lineArray);
//Add the linegeometry to line feature
IFeature lineFeature = lineF.AddFeature(lineGeometry);
//add first coordinate to the line feature
lineFeature.Coordinates.Add(coord);
//set the line feature attribute
lineID = lineID + 1;
lineFeature.DataRow["ID"] = lineID;
firstClick = false;
}
else
{
//second or more clicks - add points to the existing feature
IFeature existingFeature = lineF.Features[lineF.Features.Count - 1];
existingFeature.Coordinates.Add(coord);
//refresh the map if line has 2 or more points
if (existingFeature.Coordinates.Count >= 2)
{
lineF.InitializeVertices();
map1.ResetBuffer();
}
}
}
else if (e.Button == MouseButtons.Right)
{
//right click - reset first mouse click
firstClick = true;
map1.ResetBuffer();
lineF.SaveAs("c:\\MW\\linepath.shp", true);
MessageBox.Show("The line shapefile has been saved.");
map1.Cursor = Cursors.Arrow;
//the soil path is finished
SoilPathFinished = true;
}
}
private void viewElevationToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
//extract the complete elevation
//get the raster layer
IMapRasterLayer rasterLayer = default(IMapRasterLayer);
if (map1.GetRasterLayers().Count() == 0)
{
MessageBox.Show("Please load the DEM");
return;
}
//get the soil path line layer
IMapLineLayer pathLayer = default(IMapLineLayer);
if (map1.GetLineLayers().Count() == 0)
{
MessageBox.Show("Please draw a line");
return;
}
pathLayer = map1.GetLineLayers()[0];
IFeatureSet featureSet = pathLayer.DataSet;
//get the coordinates of the drawn path. this is the first feature of
//the feature set.
IList<Coordinate> coordinateList = featureSet.Features[0].Coordinates;
//use the first raster layer in the map
int nooflayer = map1.GetRasterLayers().Count();
List<string> layerNames = new List<string>();
Dictionary<int, List<PathPoint>> ss = new Dictionary<int, List<PathPoint>>();
int j;
for ( j= 0; j < nooflayer; j++)
{
//get elevation of all segments of the path
List<PathPoint> fullPathList = new List<PathPoint>();
rasterLayer = map1.GetRasterLayers()[j];
for (int i = 0; i < coordinateList.Count - 1; i++)
{
//for each line segment
Coordinate startCoord = coordinateList[i];
Coordinate endCoord = coordinateList[i + 1];
List<PathPoint> segmentPointList = ExtractElevation(startCoord.X, startCoord.Y, endCoord.X, endCoord.Y, rasterLayer);
//add list of points from this line segment to the complete list
fullPathList.AddRange(segmentPointList);
}
//calculate the distance
double distanceFromStart = 0;
for (int i = 1; i <= fullPathList.Count - 1; i++)
{
//distance between two neighbouring points
double x1 = fullPathList[i - 1].X;
double y1 = fullPathList[i - 1].Y;
double x2 = fullPathList[i].X;
double y2 = fullPathList[i].Y;
double distance12 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
distanceFromStart += distance12;
fullPathList[i].Distance = distanceFromStart;
}
ss.Add(j, fullPathList);
layerNames.Add((map1.GetRasterLayers()[j].LegendText.ToString()));
//just for testing
}
frmGraph graphForm = new frmGraph(ss,layerNames);
graphForm.Show();
}
catch (Exception ex)
{
MessageBox.Show("Error calculating elevation. the whole path should be inside the DEM area" + ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// This function is used to get the elevation.
/// Based on the given line segment's start and endpoint, 100 points will be divided and based on the points elevation will be claculated.
/// </summary>
/// <param name="startX">Line segement's start X point</param>
/// <param name="startY">Line segement's start Y point</param>
/// <param name="endX">Line segement's end X point</param>
/// <param name="endY">Line segement's end Y point</param>
/// <param name="raster">Raster DEM</param>
/// <returns>List of elevation</returns>
/// <remarks></remarks>
///
public List<PathPoint> ExtractElevation(double startX, double startY, double endX, double endY, IMapRasterLayer raster)
{
double curX = startX;
double curY = startY;
double curElevation = 0;
List<PathPoint> pathPointList = new List<PathPoint>();
int numberofpoints = 100;
double constXdif = ((endX - startX) / numberofpoints);
double constYdif = ((endY - startY) / numberofpoints);
for (int i = 0; i <= numberofpoints; i++)
{
PathPoint newPathPoint = new PathPoint();
if ((i == 0))
{
curX = startX;
curY = startY;
}
else
{
curX = curX + constXdif;
curY = curY + constYdif;
}
Coordinate coordinate = new Coordinate(curX, curY);
RcIndex rowColumn = raster.DataSet.Bounds.ProjToCell(coordinate);
curElevation = raster.DataSet.Value[rowColumn.Row, rowColumn.Column];
//set the properties of new PathPoint
newPathPoint.X = curX;
newPathPoint.Y = curY;
newPathPoint.Elevation = curElevation;
pathPointList.Add(newPathPoint);
}
return pathPointList;
}
private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
{
map1.FunctionMode = DotSpatial.Controls.FunctionMode.ZoomIn;
}
private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
{
map1.FunctionMode = DotSpatial.Controls.FunctionMode.ZoomOut;
}
}
}