DotSpatial Tutorials DotSpatial Tutorial 2
DotSpatial Tutorials DotSpatial Tutorial 2
Purpose of this tutorial: Become familiar with DotSpatial symbology, attribute table filter operations.
Step 2: Add the DotSpatial reference and change the compile option.
Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox.
Step 2: Add the DotSpatial reference and change the compile option.
2.1) Add the required DotSpatial reference in the visual studio development environment.
Create a new VB or C# application and right click over the project on the solution explorer. On the
context menu select the add reference and add the following reference from the DotSpatial folder.
2.2) Change the compile option from .Net FrameWork 4 Client Profile to .Net FrameWork 4
Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox.
1. Add a MenuStrip control on the form. Menu should contains the following menu items.
Under Map Operations menu items create 3 sub menu items as follows:
Note : For implementing shortcut key with alt and letters, you need to add the "&" sign in front of the
letters. Example: File menu can access by pressing "Alt" + F key. Adding "&" sign in front of letter F is
used implement this feature.
2.1 ) Add a SplitContainer control on the form. Set the property of the SplitContainer control as follows:
2.2 ) Add one more SplitContainer control in the same form on the empty space (below the above
SplitContainter control) . Set the property of the second SplitContainer control as follows:
2.1 ) Add the map control on the splcMapLegend control's panel 2 region and add legend control on the
same control's panel1 region.
Map, Legend control are located on the Toolbox under DotSpatial controls.
Set the map properties as follows: Dock = Fill, Legend = Legend1, FunctionMode = Pan
3.1 ) Add a DataGridView on the form: DataGridView is located under Data tab of the toolbox window.
Add a DataGridView on the splcDataOpeation control's panel2 and set its property as follows:
3.2 ) Add a GroupBox control on the splcDataOpeation control's panel1 and set its property as follows:
VB
'Required namespaces
Imports DotSpatial.Controls
Imports DotSpatial.Symbology
C#
// Required namespaces
using DotSpatial.Controls;
using DotSpatial.Symbology;
VB
C#
VB
C#
{
//Clear() method is used to clear the layers from the map control.
Map1.Layers.Clear();
}
VB
C#
VB
C#
This example is used to display the state names on the shape file.
VB
End If
Else
MessageBox.Show("Please add a layer to the map.")
End If
End Sub
C#
VB
Else
MessageBox.Show("Please add a layer to the map.")
End If
End Sub
C#
5.3.3) Implementing a filter operation for more than one attribute from the attribute table.
In this example, two attributes will be filtered and the result will be highlighted on the map.
VB
'Delacre a MapPolygonLayer
Dim stateLayer As MapPolygonLayer
'Check the MapPolygonLayer ( Make sure that it has polygon layer or not)
If stateLayer Is Nothing Then
MessageBox.Show("The layer is not a polygon layer.")
Else
End If
Else
MessageBox.Show("Please add a layer to the map.")
End If
End Sub
C#
if (Map1.Layers.Count > 0)
{
//Delacre a MapPolygonLayer
MapPolygonLayer stateLayer = default(MapPolygonLayer);
}
else
{
//!!!-------------------- this line is necessary otherwise the code doesn't work------------------------
!!!!!!!!!!!!!!!!!!!!
//this will load the attribute table of the layer into memory.
stateLayer.DataSet.FillAttributes();
5.3.4) Set the random colors based on the unique values from an attribute table's column.
VB
stateLayer.Symbology = scheme
End If
Else
MessageBox.Show("Please add a layer to the map.")
End If
End Sub
C#
//create categories on the scheme based on the attributes table and field name
//In this case field name is STATE_NAME
scheme.CreateCategories(stateLayer.DataSet.DataTable);
5.3.5) Filtering the data from shape file during the run time. User can enter the filter amount of
population via the interface.
VB
''' <summary>
''' This method is used filter the attribute table of the shapefile based on the population in 1990.
''' </summary>
''' <param name="population">Amount of population in 1990</param>
''' <remarks>No return value</remarks>
Private Sub filterbyPopulation (ByVal population As Integer)
If Map1.Layers.Count > 0 Then
Dim stateLayer As MapPolygonLayer
stateLayer = CType(Map1.Layers(0), MapPolygonLayer)
If stateLayer Is Nothing Then
MessageBox.Show("The layer is not a polygon layer.")
Else
category.FilterExpression = filter
category.LegendText = "population < " + population.ToString()
scheme.AddCategory(category)
stateLayer.Symbology = scheme
End If
Else
MessageBox.Show("Please add a layer to the map.")
End If
End Sub
C#
/// <summary>
/// This method is used filter the attribute table of the shapefile based on the population in 1990.
/// </summary>
/// <param name="population">Amount of population in 1990</param>
/// <remarks>No return value</remarks>
private void filterbyPopulation(int population)
{
if (Map1.Layers.Count > 0)
{
MapPolygonLayer stateLayer = default(MapPolygonLayer);
stateLayer = (MapPolygonLayer)Map1.Layers[0];
if (stateLayer == null)
{
MessageBox.Show("The layer is not a polygon layer.");
}
else
{
//!!! this line is necessary otherwise the code doesn't work
//this will load the attribute table of the layer into memory.
stateLayer.DataSet.FillAttributes();
category.FilterExpression = filter;
scheme.AddCategory(category);
stateLayer.Symbology = scheme;
}
}
else
{
MessageBox.Show("Please add a layer to the map.");
}
}
VB
End Sub
C#
VB
C#
if (Map1.Layers.Count > 0)
{
MapPolygonLayer stateLayer = default(MapPolygonLayer);
stateLayer = (MapPolygonLayer)Map1.Layers[0];
if (stateLayer == null)
{
MessageBox.Show("The layer is not a polygon layer.");
}
else
{
//Get the shapefile's attribute table to our datatable dt
dt = stateLayer.DataSet.DataTable;
Based on the selection of the state from the DataGridView, state is highlighted on the map.
First of all, select the DataGridView on the GUI and select its property window. On the property window
select the event tab.
VB
C#
stateLayer = (MapPolygonLayer)Map1.Layers[0];
if (stateLayer == null)
{ MessageBox.Show("The layer is not a polygon layer."); }
else
{
stateLayer.SelectByAttribute("[STATE_NAME] =" + "'" + row.Cells["STATE_NAME"].Value + "'");
}
}