Dot Spatial Tutorial 5
Dot Spatial Tutorial 5
Page 1
2.2) Change the compile option from .Net FrameWork 4 Client Profile to .Net FrameWork 4 This step is as same as the Tutorial # 1 step 2.2. Step 3: Add the DotSpatial Controls into the Visual Studio Toolbox. This step is as same as the Tutorial # 1 step 3. Step 4: Design the GUI. (Graphical User Interface) Design the GUI as follows: 4.1) Add a panel control and set its properties as follows: Name : pnlMain Autoscroll : True Dock : Fill BorderStyle : Fixed Single
Page 2
Set the above six panels' properties as follows: Controls Panel1 Panel2 Panel3 Panel4 Panel5 Panel6 Name pnlMap1 pnlMap2 pnlMap3 pnlMap4 pnlMap5 pnlMap6 BackColor ActiveCaption ActiveCaption ActiveCaption ActiveCaption ActiveCaption ActiveCaption BorderStyle Fixed3D Fixed3D Fixed3D Fixed3D Fixed3D Fixed3D
4.3) Add 6 map controls into the above panel controls. Set the map properties as follows: Dock : top The following figure shows indicates the order in which the map controls should be placed on their associated panel controls.
Page 3
Map1
Map2
Map3
Map4
Map5
Map6
fig. 4 . Map Controls 4.4) Add the 37 labels described below. 4.4.1) Place the first label at the top of your form to serve as a title for the page. Set its label properties as follows: Name : lbltitle ForeColor : Blue ( Optional) Text : Projection Explorer!!! (or use a title of your choosing). 4.4.2) Set the other 36 labels properties as follows:
Controls label1 label2 label3 label4 label5 label6 label7 label8 label9 label10 label11 label12 label13 label14 label15 label16 Name lblmap1Projection lblmap2Projection lblmap3Projection lblmap4Projection lblmap5Projection lblmap6Projection lbltotalAreaMap1 lbltotalAreaMap2 lbltotalAreaMap3 lbltotalAreaMap4 lbltotalAreaMap5 lbltotalAreaMap6 lblmap1selectedinfo lblmap2selectedinfo lblmap3selectedinfo lblmap4selectedinfo ForeColor DarkRed DarkRed DarkRed DarkRed DarkRed DarkRed ControlText ControlText ControlText ControlText ControlText ControlText ControlText ControlText ControlText ControlText Text UtmNad1983.NAD1983UTMZone12N NorthAmerica.NorthAmericaAlbersEqualAreaConic NorthAmerica.USAContiguousLambertConformalConic World.CylindricalEqualAreaworld Polar.NorthPoleAzimuthalEquidistant NorthAmerica.USAContiguousAlbersEqualAreaConicUSGS Total Area Total Area Total Area Total Area Total Area Total Area Area of the selected region Area of the selected region Area of the selected region Area of the selected region Visible True True True True True True True True True True True True False False False False
Page 4
Difference between current and default projection Difference between current and default projection Difference between current and default projection Difference between current and default projection Difference between current and default projection Difference between current and default projection Assumption: Default projection
Page 5
Set the controls properties as follows: Control Label1 Label2 Combobox Combobox Button1 Button2 Name Text lblFieldName Field Name lblSelectedRegion Selected Region cmbFiledName cmbSelectedRegion btnRegionArea btnCompareProjections
Page 6
C# //Required namespaces using DotSpatial.Controls; using DotSpatial.Projections; using DotSpatial.Data; 2nd International MapWindow GIS and DotSpatial Conference 2011 Page 7
VB
Private Sub btnLoadShapeFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadShapeFile.Click 'define the projections Map1.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone12N Map2.Projection = KnownCoordinateSystems.Projected.NorthAmerica.NorthAmericaAlbersEqualAreaConic Map3.Projection = KnownCoordinateSybn stems.Projected.NorthAmerica.USAContiguousLambertConformalConic Map4.Projection = KnownCoordinateSystems.Projected.World.CylindricalEqualAreaworld Map5.Projection = KnownCoordinateSystems.Projected.Polar.NorthPoleAzimuthalEquidistant Map6.Projection = KnownCoordinateSystems.Projected.NorthAmerica.USAContiguousAlbersEqualAreaConicUSGS 'add the layers Dim fileDialog As New OpenFileDialog() fileDialog.Filter = "Shapefiles|*.shp" If fileDialog.ShowDialog = DialogResult.OK Then 'add layer to first map Dim featureSet1 As New FeatureSet featureSet1.Open(fileDialog.FileName) 'Populate the FiledName dropdownlist with the help of featureset1. 'We need to pass featureset as an input paramter to FillColumnNames method. FillColumnNames(featureSet1) 'set the projection featureSet1.Reproject(Map1.Projection) Map1.Layers.Add(featureSet1) 'add layer to second map Dim featureSet2 As New FeatureSet featureSet2.Open(fileDialog.FileName) featureSet2.Reproject(Map2.Projection) Map2.Layers.Add(featureSet2) 'add layer to map3 Dim featureSet3 As New FeatureSet featureSet3.Open(fileDialog.FileName) featureSet3.Reproject(Map3.Projection) Map3.Layers.Add(featureSet3) 'add layer to map4 Dim featureSet4 As New FeatureSet featureSet4.Open(fileDialog.FileName) featureSet4.Reproject(Map4.Projection) Map4.Layers.Add(featureSet4) 'add layer to map5 Dim featureSet5 As New FeatureSet
Page 8
C#
private void btnLoadShapeFile_Click(object sender, EventArgs e) { //define the projections map1.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone12N; map2.Projection = KnownCoordinateSystems.Projected.NorthAmerica.NorthAmericaAlbersEqualAreaConic; map3.Projection = KnownCoordinateSystems.Projected.NorthAmerica.USAContiguousLambertConformalConic; map4.Projection = KnownCoordinateSystems.Projected.World.CylindricalEqualAreaworld; map5.Projection = KnownCoordinateSystems.Projected.Polar.NorthPoleAzimuthalEquidistant; map6.Projection = KnownCoordinateSystems.Projected.NorthAmerica.USAContiguousAlbersEqualAreaConicUSGS; //add the layers OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Shapefiles|*.shp"; if (fileDialog.ShowDialog() == DialogResult.OK) { //add layer to first map FeatureSet featureSet1 = new FeatureSet(); featureSet1.Open(fileDialog.FileName); //Populate the FiledName dropdownlist with the help of featureset1. //We need to pass featureset as an input paramter to FillColumnNames method. FillColumnNames(featureSet1); //set the projection featureSet1.Reproject(map1.Projection); map1.Layers.Add(featureSet1); //add layer to second map FeatureSet featureSet2 = new FeatureSet(); featureSet2.Open(fileDialog.FileName); featureSet2.Reproject(map2.Projection); map2.Layers.Add(featureSet2); //add layer to map3 FeatureSet featureSet3 = new FeatureSet();
Page 9
} }
VB
Private Sub btnGetTotalArea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetTotalArea.Click lbltotalAreaMap1.Text = "Total area in sq meters: " + _getTotalArea(Map1).ToString() lblAreaMap2.Text = "Total area in sq meters: " + _getTotalArea(Map2).ToString() lblAreaMap3.Text = "Total area in sq meters: " + _getTotalArea(Map3).ToString() lblAreaMap4.Text = "Total area in sq meters: " + _getTotalArea(Map4).ToString() lblAreaMap5.Text = "Total area in sq meters: " + _getTotalArea(Map5).ToString() lblAreaMap6.Text = "Total area in sq meters: " + _getTotalArea(Map6).ToString() End Sub
C#
private void btnGetTotalArea_Click(object sender, EventArgs e) { lbltotalAreaMap1.Text = "Total area in sq meters: " + _getTotalArea(map1).ToString(); lbltotalAreaMap2.Text = "Total area in sq meters: " + _getTotalArea(map2).ToString(); lbltotalAreaMap3.Text = "Total area in sq meters: " + _getTotalArea(map3).ToString(); lbltotalAreaMap4.Text = "Total area in sq meters: " + _getTotalArea(map4).ToString(); lbltotalAreaMap5.Text = "Total area in sq meters: " + _getTotalArea(map5).ToString(); lbltotalAreaMap6.Text = "Total area in sq meters: " + _getTotalArea(map6).ToString(); }
Page 10
C#
/// <summary> /// This method is used to claculate total area of a feature /// </summary> /// <param name="mapInput">map control</param> /// <returns>total are of the feature from the mapcontrol</returns> /// <remarks></remarks> private double _getTotalArea(DotSpatial.Controls.Map mapInput) { double stateArea = 0; if ((mapInput.Layers.Count > 0)) { MapPolygonLayer stateLayer = default(MapPolygonLayer); stateLayer = (MapPolygonLayer)mapInput.Layers[0]; if ((stateLayer == null)) { MessageBox.Show("The layer is not a polygon layer."); } else { foreach (IFeature stateFeature in stateLayer.DataSet.Features) { stateArea += stateFeature.Area(); } } } return stateArea; }
Page 11
C#
/// <summary> /// This method is used to populate the FieldName combobox /// </summary> /// <param name="featureSet"></param> /// <remarks></remarks> private void FillColumnNames(IFeatureSet featureSet) { foreach (DataColumn column in featureSet.DataTable.Columns) { cmbFiledName.Items.Add(column.ColumnName); } }
Write the following sub method for populating the cmbSelectedRegion combo box. VB
''' <summary> ''' This method is used to fill the unique values on the cmbSelectedRegion combobox ''' </summary> ''' <param name="uniqueField">Fieldname combobox's selected value</param> ''' <param name="mapInput">Map layer</param> ''' <remarks></remarks> Private Sub FillUniqueValues(ByVal uniqueField As String, ByVal mapInput As DotSpatial.Controls.Map) Dim fieldList As New List(Of String) If (mapInput.Layers.Count > 0) Then Dim currentLayer As MapPolygonLayer currentLayer = CType(mapInput.Layers(0), MapPolygonLayer) If (currentLayer Is Nothing) Then MessageBox.Show("The layer is not a polygon layer.") Else Dim dt As DataTable = currentLayer.DataSet.DataTable cmbSelectedRegion.Items.Clear() For Each rows As DataRow In dt.Rows cmbSelectedRegion.Items.Add(rows(uniqueField)) Next End If End If
Page 12
C#
/// <summary> /// This method is used to fill the unique values on the cmbSelectedRegion combobox /// </summary> /// <param name="uniqueField">Fieldname combobox's selected value</param> /// <param name="mapInput">Map layer</param> /// <remarks></remarks> private void FillUniqueValues(string uniqueField, DotSpatial.Controls.Map mapInput) { List<string> fieldList = new List<string>(); if ((mapInput.Layers.Count > 0)) { MapPolygonLayer currentLayer = default(MapPolygonLayer); currentLayer = (MapPolygonLayer)mapInput.Layers[0]; if ((currentLayer == null)) { MessageBox.Show("The layer is not a polygon layer."); } else { DataTable dt = currentLayer.DataSet.DataTable; cmbSelectedRegion.Items.Clear(); foreach (DataRow rows in dt.Rows) { cmbSelectedRegion.Items.Add(rows[uniqueField]); } } } }
VB
Private Sub cmbFiledName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFiledName.SelectedIndexChanged FillUniqueValues(cmbFiledName.Text, Map1) End Sub
C#
private void cmbFiledName_SelectedIndexChanged(object sender, EventArgs e) { FillUniqueValues(cmbFiledName.Text, map1); }
Page 13
VB
''' <summary> ''' This sub method is used to control the visibility of any label control ''' </summary> ''' <param name="lbl">label name</param> ''' <param name="vis">Either True / False</param> ''' <remarks></remarks> Private Sub setVisible(ByVal lbl As Label, ByVal vis As Boolean) lbl.Visible = vis End Sub
C#
/// <summary> /// This sub method is used to control the visibility of any label control /// </summary> /// <param name="lbl">label name</param> /// <param name="vis">Either True / False</param> /// <remarks></remarks> private void setVisible(Label lbl, bool vis) { lbl.Visible = vis; }
Write the following function for getting the selected region's area. VB
''' <summary> ''' This method is used to get the area of the selected region on the combobox ''' </summary> ''' <param name="uniqueColumnName">Field name</param> ''' <param name="uniqueValue">Unique value from the selected region combobox</param> ''' <param name="mapInput">map layer</param> ''' <returns>area of the selected field</returns> ''' <remarks></remarks> Private Function _getArea(ByVal uniqueColumnName As String, ByVal uniqueValue As String, ByVal mapInput As DotSpatial.Controls.Map) As Double Dim stateArea As Double If (mapInput.Layers.Count > 0) Then Dim stateLayer As MapPolygonLayer stateLayer = CType(mapInput.Layers(0), MapPolygonLayer) If (stateLayer Is Nothing) Then MessageBox.Show("The layer is not a polygon layer.") Else stateLayer.SelectByAttribute("[" + uniqueColumnName + "] =" + "'" + uniqueValue + "'") For Each stateFeature As IFeature In stateLayer.DataSet.Features If uniqueValue = stateFeature.DataRow(uniqueColumnName) Then stateArea = stateFeature.Area Exit For End If Next End If
Page 14
C#
/// <summary> /// This method is used to get the area of the selected region on the combobox /// </summary> /// <param name="uniqueColumnName">Field name</param> /// <param name="uniqueValue">Unique value from the selected region combobox</param> /// <param name="mapInput">map layer</param> /// <returns>area of the selected field</returns> /// <remarks></remarks> private double _getArea(string uniqueColumnName, string uniqueValue, DotSpatial.Controls.Map mapInput) { double stateArea = 0; if ((mapInput.Layers.Count > 0)) { MapPolygonLayer stateLayer = default(MapPolygonLayer); stateLayer = (MapPolygonLayer)mapInput.Layers[0]; if ((stateLayer == null)) { MessageBox.Show("The layer is not a polygon layer."); } else { stateLayer.SelectByAttribute("[" + uniqueColumnName + "] =" + "'" + uniqueValue + "'"); foreach (IFeature stateFeature in stateLayer.DataSet.Features) { if (uniqueValue.CompareTo(stateFeature.DataRow[uniqueColumnName]) == 0) { stateArea = stateFeature.Area(); break; // TODO: might not be correct. Was : Exit For } } } } return stateArea; }
VB
Private Sub btnRegionArea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegionArea.Click setVisible(lblmap1selectedinfo, True) setVisible(lblmap2selectedinfo, True) setVisible(lblmap3selectedinfo, True) setVisible(lblmap4selectedinfo, True) setVisible(lblmap5selectedinfo, True) setVisible(lblmap6selectedinfo, True)
Page 15
C#
private void btnRegionArea_Click(object sender, EventArgs e) { setVisible(lblmap1selectedinfo, true); setVisible(lblmap2selectedinfo, true); setVisible(lblmap3selectedinfo, true); setVisible(lblmap4selectedinfo, true); setVisible(lblmap5selectedinfo, true); setVisible(lblmap6selectedinfo, true); lblMap1SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map1).ToString(); lblMap2SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map2).ToString(); lblMap3SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map3).ToString(); lblMap4SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map4).ToString(); lblMap5SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map5).ToString(); lblMap6SelectedArea.Text = _getArea(cmbFiledName.Text, cmbSelectedRegion.Text, map6).ToString(); }
C#
/// /// /// /// /// <summary> This function is used to claculate the difference between 2 ares </summary> <param name="area1"></param> <param name="area2"></param>
Page 16
Write the following code under the compare projection button click event. VB
Private Sub btnCompareProjections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompareProjections.Click lblmap1difference.Text = _calculateDifference(CDbl(lblMap2SelectedArea.Text), CDbl(lblMap1SelectedArea.Text)).ToString() lblmap3difference.Text = _calculateDifference(CDbl(lblMap2SelectedArea.Text), CDbl(lblMap3SelectedArea.Text)).ToString() lblmap4difference.Text = _calculateDifference(CDbl(lblMap2SelectedArea.Text), CDbl(lblMap4SelectedArea.Text)).ToString() lblmap5difference.Text = _calculateDifference(CDbl(lblMap2SelectedArea.Text), CDbl(lblMap5SelectedArea.Text)).ToString() lblmap6difference.Text = _calculateDifference(CDbl(lblMap2SelectedArea.Text), CDbl(lblMap6SelectedArea.Text)).ToString() setVisible(lblmap1difference, setVisible(lblmap1info, True) setVisible(lblmap3difference, setVisible(lblmap3info, True) setVisible(lblmap4difference, setVisible(lblmap4info, True) setVisible(lblmap5difference, setVisible(lblmap5info, True) setVisible(lblmap6difference, setVisible(lblmap6info, True) End Sub True) True) True) True) True)
C#
private void btnCompareProjections_Click(object sender, EventArgs e) { lblmap1difference.Text = _calculateDifference(Convert.ToDouble(lblMap2SelectedArea.Text), Convert.ToDouble(lblMap1SelectedArea.Text)).ToString(); lblmap3difference.Text = _calculateDifference(Convert.ToDouble(lblMap2SelectedArea.Text), Convert.ToDouble(lblMap3SelectedArea.Text)).ToString(); lblmap4difference.Text = _calculateDifference(Convert.ToDouble(lblMap2SelectedArea.Text), Convert.ToDouble(lblMap4SelectedArea.Text)).ToString(); lblmap5difference.Text = _calculateDifference(Convert.ToDouble(lblMap2SelectedArea.Text), Convert.ToDouble(lblMap5SelectedArea.Text)).ToString();
Page 17
Page 18
Page 19
Page 20