CalculateArea.cs

This example demonstrates how to calculate the area of polygons, to write it to the attribute table, and to display as labels. Here is a screenshot with the results of the code execution.

using System;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// This code calculates an area of polygons, writes it to the attribute table, and displays as labels.
// </summary>
public void CalculateArea(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
string filename = dataPath + "buildings.shp";
if (!File.Exists(filename))
{
MessageBox.Show("Couldn't file the file: " + filename);
return;
}
var sf = new Shapefile();
if (!sf.Open(filename, null))
return;
if (sf.ShapefileType != ShpfileType.SHP_POLYGON)
{
MessageBox.Show("Polygon shapefile is expected." + Environment.NewLine +
"Received: " + sf.ShapefileType);
}
else
{
int layerHandle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
int fldIndex = sf.Table.FieldIndexByName["CalcArea"];
if (fldIndex != -1)
{
if (MessageBox.Show("The area field exists. Do you want to overwrite it?", "",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
sf.Close();
return;
}
}
if (!sf.StartEditingTable(null))
{
MessageBox.Show("Failed to start editing mode: " + sf.ErrorMsg[sf.LastErrorCode]);
sf.Close();
}
else
{
// removing the field in case it is already present
if (fldIndex != -1)
{
if (!sf.EditDeleteField(fldIndex, null))
{
MessageBox.Show("Failed to delete field: " + sf.ErrorMsg[sf.LastErrorCode]);
sf.Close();
return;
}
}
//adding the new field in the end of the table
fldIndex = sf.EditAddField("CalcArea", FieldType.DOUBLE_FIELD, 9, 12);
if (fldIndex == -1)
{
MessageBox.Show("Failed to insert field: " + sf.ErrorMsg[sf.LastErrorCode]);
sf.Close();
return;
}
for (int i = 0; i < sf.NumShapes; i++)
{
Shape shp = sf.Shape[i];
sf.EditCellValue(fldIndex, i, shp.Area);
}
sf.Labels.Generate("[CalcArea] + \" sqr.m\"", tkLabelPositioning.lpCentroid, true);
sf.Labels.FrameVisible = true;
}
}
}
// <summary>
// A shorter variant of procedure with less checks
// </summary>
public void CalculateAreaFast(Shapefile sf)
{
bool editing = sf.EditingTable;
if (!sf.EditingTable)
{
if (sf.StartEditingTable(null))
{
MessageBox.Show("Failed to open editing mode: " + sf.ErrorMsg[sf.LastErrorCode]);
return;
}
int fldIndex = sf.Table.FieldIndexByName["Area"];
if (fldIndex == -1)
{
MessageBox.Show("Field index doesn't exists");
}
else
{
for (int i = 0; i < sf.NumShapes; i++)
{
Shape shp = sf.Shape[i];
sf.EditCellValue(fldIndex, i, shp.Area);
}
if (!editing)
sf.StopEditingTable(true, null);
}
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
ShpfileType
The type of the shapefile.
Definition: Enumerations.cs:169
FieldType
The available types of fields of dbf table.
Definition: Enumerations.cs:34
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
double Area
Calculates the area of the shape. For non-polygon shapes this property will return 0....
Definition: Shape.cs:50
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
int NumShapes
Gets the number of shapes in the shapefile.
Definition: Shapefile.cs:254
int LastErrorCode
Gets the code of last error which took place inside this object.
Definition: Shapefile.cs:249
bool Close()
Closes the attribute table.
Definition: TableClass.cs:164
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546
bool EditingTable
Gets the value indicating whether editing operations are allowed for shapefile attribute table.
Definition: Shapefile.cs:667
Table Table
Gets the reference to the attribute table associated with the shapefile.
Definition: Shapefile.cs:677
bool EditCellValue(int fieldIndex, int shapeIndex, object newVal)
Sets the new value for particular cell in attribute table. The table must be in editing mode.
Definition: Shapefile.cs:633
bool StopEditingTable(bool applyChanges, ICallback cBack)
Closes the editing mode for the attribute table.
Definition: Shapefile.cs:695
bool StartEditingTable(ICallback cBack)
Opens editing mode for the attribute table.
Definition: Shapefile.cs:684