0% found this document useful (0 votes)
12 views

DotSpatial Tutorials_DotSpatial_Tutorial_3

This tutorial provides a comprehensive guide on performing raster data operations using DotSpatial, including loading raster data, implementing hill shade, changing colors, multiplying data, and reclassifying raster values. It outlines essential steps such as setting up the DotSpatial library, designing a graphical user interface, and writing code for various functionalities. Additionally, the tutorial includes event handling for user interactions with the raster data layer.

Uploaded by

iwgss2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DotSpatial Tutorials_DotSpatial_Tutorial_3

This tutorial provides a comprehensive guide on performing raster data operations using DotSpatial, including loading raster data, implementing hill shade, changing colors, multiplying data, and reclassifying raster values. It outlines essential steps such as setting up the DotSpatial library, designing a graphical user interface, and writing code for various functionalities. Additionally, the tutorial includes event handling for user interactions with the raster data layer.

Uploaded by

iwgss2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Programming with DotSpatial

Raster Data Operations in DotSpatial


Tutorial (2)

Purpose of this tutorial: Become familiar with the following raster operations in DotSpatial:

1. Loading different formats of raster data in DotSpatial.

2. Implementing the hill shade.

3. Change the color of the raster data.

4. Multiply the raster data.

5. Reclassify the raster data.

6. Get the mouse clicked point values on the raster data layer.

This tutorial has 5 important steps.

Step 1: Download the DotSpatial class library

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 5: Design the GUI. (GUI - Graphical User Interface)

Step 6: Write the code for implementing the map operations.

Step 1: Download the DotSpatial class library

This step is the same as Tutorial #1 step 1.

Step 2: Add the DotSpatial reference and change the compile option.

1.1) Adding the references.

DotSpatial.Data.Forms.dll, DotSpatial.Symbology.dll, DotSpatial.Controls.dll, DotSpatial.Projections.dll,


DotSpatial.Data.dll, DotSpatial.Topology.dll

Teva ~ VeluppillaiPage 1
Introduction to Programming with DotSpatial

Fig. 1 Required references.

2.2) Change the compile option.

Change the compile option from .Net Framework 4 Client Profile to .Net Framework4.

This step is the same as the Tutorial # 1 step 2.2.

Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox.

This step is the same as the Tutorial # 1 step 3.

Step 4: Copy the Data Extension folder from your downloaded folder to your current project bin/debug
folder. The .dlls from this folder is necessary for GDAL data provider access.

Fig. 2 Data Extensions folder from DotSpatial downloaded unzip folder

Teva ~ VeluppillaiPage 2
Introduction to Programming with DotSpatial

Step 5: Design the GUI

Design the GUI as follows:

Fig. 3 Final GUI

Interface design considerations.

1. Add three panel controls. Panel control's properties should be as follows:

Properties Panel1 Panel2 Panel3

Name pnlOperation pnlLegend pnlMap


s

Dock Top Left Fill

2. Add five buttons. Button properties should be as follows:

Properties Button1 Button2 Button3 Button4 Button5

Teva ~ VeluppillaiPage 3
Introduction to Programming with DotSpatial

Name btnLoadRaste btnHillshade btnChangeColor btnMultiplyRaster btnReclassify


r

Text &Load Raster &Hillshade Change &Color &Multiply Raster &Reclassify


Raster

3. Add two label controls. Labels' properties should be as follows:

Properties Label1 Label2

Name lblElevation lblRasterValue

Text Elevation Row: Column: Value:

4. Add a checkbox and it should have the following properties.

Name: chbRasterValue Text : Raster value at clicked point

5. Add a textbox and it should have the following features.

Name: txtElevation Text: 3000

6. Drag a “Legend” control from the DotSpatial tab under toolbox and drop it on pnlLegend. Legend
properties should be as follows:

Name: Legend1 Dock: Fill

7. 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

8. 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.

Teva ~ VeluppillaiPage 4
Introduction to Programming with DotSpatial

Fig. 4 AppManager

9. Set the properties of AppManager1 as follows:

Map: Map1 Legend: Legend1

Step: 6 Loading the different formats of raster data.

6.1) Import the following namespaces in the coding window.

//Required namespaces
using DotSpatial.Symbology;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Topology;

6.2 ) btnLoadRaster click event should be as follows:

private void btnLoadRaster_Click(object sender, EventArgs e)


{
//AddRasterLayer() method is used to load the raster layers on the map
map1.AddRasterLayer();
map1.ZoomToMaxExtent();
}

Implementing the hill shade.

btnHillshade_Click event should have the following code.

private void btnHillshade_Click(object sender, EventArgs e)


{

Teva ~ VeluppillaiPage 5
Introduction to Programming with DotSpatial

if (map1.Layers.Count > 0)
{

//IMapRasterLayer layer = (IMapRasterLayer)map1.Layers[0];

IMapRasterLayer layer =map1.Layers[0] as IMapRasterLayer;

if (layer == null)
{
MessageBox.Show("Please select a raster layer");
return;
}

layer.Symbolizer.ShadedRelief.ElevationFactor = 1;

layer.Symbolizer.ShadedRelief.IsUsed = true;

layer.WriteBitmap();

}
else
{
MessageBox.Show("Please add a layer to the map.");
}
}

Change the color.

btnChangeColor click event should has the following event.

private void btnChangeColor_Click(object sender, EventArgs e)


{
if (map1.Layers.Count > 0)
{
//change the color of raster

//typecast the first layer to MapRasterLayer


IMapRasterLayer layer = map1.Layers[0] as IMapRasterLayer;

if (layer == null)
{
MessageBox.Show("Please add a raster layer.");
return;
}

//set the color scheme


Teva ~ VeluppillaiPage 6
Introduction to Programming with DotSpatial

//create an instance for a colorscheme


ColorScheme scheme = new ColorScheme();

//create a new category


ColorCategory category1 = new ColorCategory(2500, 3000,
Color.Red, Color.Yellow);

category1.LegendText = "Elevation 2500 - 3000";

//add the category to the color scheme


scheme.AddCategory(category1);

//create another category


ColorCategory category2 = new ColorCategory(1000, 2500,
Color.Blue, Color.Green);

category2.LegendText = "Elevation 1000 - 2500";

scheme.AddCategory(category2);

//assign new color scheme


layer.Symbolizer.Scheme = scheme;

//refresh the layer display in the map


layer.WriteBitmap();

}
else
{
MessageBox.Show("Please add a layer to the map.");
}

Multiply the raster.

btnMultiplyRaster should has the following event.

private void btnMultiplyRaster_Click(object sender, EventArgs e)


{
if (map1.Layers.Count > 0)
{
IMapRasterLayer layer = map1.Layers[0] as IMapRasterLayer ;

Teva ~ VeluppillaiPage 7
Introduction to Programming with DotSpatial

if (layer == null)
{
MessageBox.Show("Please select a raster layer");
}

IRaster demRaster = layer.DataSet;

string[] rasterOptions = new string[1];

IRaster newRaster = Raster.CreateRaster("multiply.bgd", null,


demRaster.NumColumns, demRaster.NumRows, 1, demRaster.DataType,
rasterOptions);

//Bounds specify the cellsize and the coordinates of raster corner


newRaster.Bounds = demRaster.Bounds.Copy();

newRaster.NoDataValue = demRaster.NoDataValue;

newRaster.Projection = demRaster.Projection;

for (int i = 0; i <= demRaster.NumRows - 1; i++)


{
for (int j = 0; j <= demRaster.NumColumns - 1; j++)
{
if (demRaster.Value[i, j] != demRaster.NoDataValue)
{
newRaster.Value[i, j] = demRaster.Value[i, j] * 2;
}
}
}

//save the new raster to the file


newRaster.Save();

//add the new raster to the map


map1.Layers.Add(newRaster);

else
{
MessageBox.Show("Please add a layer to the map.");
}

Teva ~ VeluppillaiPage 8
Introduction to Programming with DotSpatial

Reclassify the raster.

btnReclassify should has the following event.

private void btnReclassify_Click(object sender, EventArgs e)


{
//typecast the selected layer to IMapRasterLayer
IMapRasterLayer layer = map1.Layers.SelectedLayer as
IMapRasterLayer ;

if (layer == null)
{
MessageBox.Show("Please select a raster layer.");
}
else
{
//get the raster dataset
IRaster demRaster = layer.DataSet;

//create a new empty raster with same dimension as original raster


string[] rasterOptions = new string[1];

IRaster newRaster = Raster.CreateRaster("reclassify.bgd", null,


demRaster.NumColumns, demRaster.NumRows, 1, demRaster.DataType,
rasterOptions);
newRaster.Bounds = demRaster.Bounds.Copy();
newRaster.NoDataValue = demRaster.NoDataValue;
newRaster.Projection = demRaster.Projection;

//reclassify raster.
// values >= specified value will have new value 1
// values < specified value will have new value 0

double oldValue = 0;

//get the specified value from the textbox


double specifiedValue = Convert.ToDouble(txtElevation.Text);

for (int i = 0; i <= demRaster.NumRows - 1; i++)


{
for (int j = 0; j <= demRaster.NumColumns - 1; j++)
{
//get the value of original raster
oldValue = demRaster.Value[i, j];

Teva ~ VeluppillaiPage 9
Introduction to Programming with DotSpatial

if (oldValue >= specifiedValue)


{
newRaster.Value[i, j] = 1;
}
else
{
newRaster.Value[i, j] = 0;
}
}
}

newRaster.Save();

map1.Layers.Add(newRaster);
}
}

Get the mouse clicked point values on the raster data layer.

Select the chbRasterValue's "CheckedChanged" event.

fig.5 ChbRasterValue checkbox's checked changed event.

chbRasterValue_CheckedChanged event should has the following events.

private void chbRasterValue_CheckedChanged(object sender, EventArgs e)


{
if (chbRasterValue.Checked)

Teva ~ VeluppillaiPage 10
Introduction to Programming with DotSpatial

{
IMapRasterLayer rasterLayer = map1.Layers.SelectedLayer as
IMapRasterLayer ;

if ((rasterLayer != null))
{
//set the map cursor to cross
map1.Cursor = Cursors.Cross;
}
else
{
//if no raster layer is selected, uncheck the checkbox
MessageBox.Show("Please select a raster layer.");
chbRasterValue.Checked = false;
}
}
else
{
//change map cursor back to arrow
map1.Cursor = Cursors.Arrow;
}

Select the Map1's mouse up event.

fig.6 Map1_MouseUp event

map1_MouseUp event should have the following code.

Teva ~ VeluppillaiPage 11
Introduction to Programming with DotSpatial

private void map1_MouseUp(object sender, MouseEventArgs e)


{
if (chbRasterValue.Checked)
{
//get the layer selected in the legend
IMapRasterLayer rasterLayer = map1.Layers.SelectedLayer as
IMapRasterLayer;

if ((rasterLayer != null))
{

//get the raster data object


IRaster raster = rasterLayer.DataSet;

//convert mouse position to map coordinate


Coordinate coord = map1.PixelToProj(e.Location);

//convert map coordinate to raster row and column


RcIndex rc = raster.Bounds.ProjToCell(coord);

int row = rc.Row;

int column = rc.Column;

//check if clicked point is inside of raster


if ((column > 0 & column < raster.NumColumns & row > 0 &
row < raster.NumRows))
{
//get the raster value at row and column
double value = raster.Value[row, column];

//show the row, column and value in the label


lblRasterValue.Text = string.Format("row: {0} column: {1}
value: {2}", row, column, value);
}
else
{
lblRasterValue.Text = "outside of raster";
}

Teva ~ VeluppillaiPage 12
Introduction to Programming with DotSpatial

Output screen shot for different operations.

Fig.7 Load a raster data

Teva ~ VeluppillaiPage 13
Introduction to Programming with DotSpatial

Fig.8 After the hill shade operation

Fig.9 After the color change operation

Teva ~ VeluppillaiPage 14
Introduction to Programming with DotSpatial

Fig.10 After the raster multiplication

Fig.11 After the reclassify operation

Teva ~ VeluppillaiPage 15
Introduction to Programming with DotSpatial

Fig.12 Getting the mouse click point value on the raster data file.

Teva ~ VeluppillaiPage 16

You might also like