DotSpatial Tutorials_DotSpatial_Tutorial_6
DotSpatial Tutorials_DotSpatial_Tutorial_6
Purpose of this tutorial: Finding an elevation distance profile for any hiking path on any DEM.
Step 2: Add the DotSpatial reference and change the compile option.
Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox.
Step 4: Copy the Data Extensions folder to the debugs folder of the current project
Step 2: Add the DotSpatial reference and change the compile option.
Change the compile option from .Net Framework 4 Client Profile to .Net Framework4. This step is as
same as the Tutorial # 1 step 2.2.
Step 2: Add the DotSpatial controls on the Visual Studio Toolbox window.
Create a new tab on the toolbox and add the DotSpatial controls on it, the same as tutorial (1) step 1.
Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox.
Step 4: Copy the Data Extension folder from your downloaded folder to your current project debug
folder. The .dlls from this folder is necessary for GDAL data provider access.
Step 5: Design the GUI : GUI has 2 different parts such as Main interface form and Graph interface
form.
Path
4. Drag a “Legend” control from the DotSpatial tab under toolbox and drop it on pnlLegend. Legend
properties should be as follows:
5. Drag a “Map” control from the DotSpatial tab under toolbox and drop it on pnlMap. Map properties
should be as follows:
Name: Map1, Dock: Fill, Legend: Legend1 , Back color: Silver , ProjectionModeDefine: Never
ProjectionModeReproject: Never
6. Drag an "AppManager" control from DotSpatial tab under toolbox and drop it on the form.
Note: This control is necessary for loading different formats of raster data.
Fig. 3 AppManager.
1. Download the ZedGraph.dll from the class form website or from the following URL:
http://sourceforge.net/projects/zedgraph/files/
2. from the tool box, right click on the DotSpatial tab OR all windows forms tab and select the choose
items from the context menu.
6. Drag the ZedGraph control from the toolbox and drop into the pnlGraph control. Set its properties as
follows:
Dock : fill
VB
#Region "NameSpaces"
Imports DotSpatial.Controls
Imports DotSpatial.Data
Imports DotSpatial.Symbology
Imports DotSpatial.Topology
Imports System.Collections.Generic
#End Region
C#
#region "NameSpaces"
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
#endregion
VB
#End Region
C#
VB
Public Class PathPoint
Public X As Double
Public Y As Double
Public Distance As Double
Public Elevation As Double
End Class
C#
public class PathPoint
{
public double X;
public double Y;
public double Distance;
public double Elevation;
}
Crate an ExtractElevation function as follows: This function is used to get the elevation from the DEM
along with the line segment.
VB
''' <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 Function ExtractElevation(ByVal startX As Double, ByVal startY As Double, ByVal endX As
Double, ByVal endY As Double, ByVal raster As IMapRasterLayer) As List(Of PathPoint)
Dim curX As Double = startX
C#
/// <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;
}
Add the following code in the Load DEM button click event.
VB
C#
Add the following code in the Drawing hiking path button click event.
VB
Private Sub btnDrawPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnDrawPath.Click
'remove any existing path
Dim existingPathList = Map1.GetLineLayers()
For Each existingPath As IMapLineLayer In existingPathList
Map1.Layers.Remove(existingPath)
Next
lineF = New FeatureSet(FeatureType.Line)
'set projection
lineF.Projection = Map1.Projection
'initialize the featureSet attribute table
Dim column As New DataColumn("ID")
lineF.DataTable.Columns.Add(column)
'add the featureSet as map layer
lineLayer = Map1.Layers.Add(lineF)
Dim symbol As New LineSymbolizer(Color.Blue, 2)
lineLayer.Symbolizer = symbol
lineLayer.LegendText = "Hiking path"
firstClick = True
End Sub
C#
//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);
LineSymbolizer symbol = new LineSymbolizer(Color.Blue, 2);
lineLayer.Symbolizer = symbol;
lineLayer.LegendText = "Hiking path";
firstClick = true;
}
VB
C#
VB
InitializeComponent()
ZedGraphControl1.GraphPane.CurveList.Clear()
End Sub
End Class
C#
public frmGraph(List<PathPoint> pathList)
{
InitializeComponent();
zedGraphControl1.GraphPane.CurveList.Clear();
VB
Catch ex As Exception
MessageBox.Show("Error calculating elevation. the whole path should be inside the DEM area")
End Try
End Sub
C#
try
{
//extract the complete elevation
//get the raster layer
IMapRasterLayer rasterLayer = default(IMapRasterLayer);
if (map1.GetRasterLayers().Count() == 0)
{
MessageBox.Show("Please add a raster layer");
return;
}
double y2 = fullPathList[i].Y;
double distance12 = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 -
y1)));
distanceFromStart += distance12;
fullPathList[i].Distance = distanceFromStart;
}
}
catch (Exception ex)
{
MessageBox.Show("Error calculating elevation. the whole path should be inside
the DEM area");
}
}