IntersectionLength.cs

This example demonstrates how to calculate the total length of rivers for each land parcel. Two shapefiles with rivers and land parcels act as an input. The code performs "intersects" test for each pair of river and parcel and computes intersection if such takes place. The positions of intersections are added as new polyline shapefile. The [Length] field is added to the parcels shapefile which will hold the total length of rivers. Finally the values of the new field are visualized using bar charts. Here is a screenshot with the results of the code execution.

using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Calculates the length of intersection of rivers and land parcels
// </summary>
public void IntersectionLength(AxMap axMap1, ToolStripStatusLabel label, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
string filename1 = dataPath + "landuse.shp";
string filename2 = dataPath + "waterways.shp";
if (!File.Exists(filename1) || !File.Exists(filename2))
{
MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath);
}
else
{
Shapefile sfParcels = new Shapefile();
sfParcels.Open(filename1, null);
sfParcels.StartEditingShapes(true, null);
Field field = new Field {Name = "Length", Type = FieldType.DOUBLE_FIELD, Precision = 10};
int fieldIndex = sfParcels.NumShapes;
sfParcels.EditInsertField(field, ref fieldIndex, null);
Shapefile sfRivers = new Shapefile();
sfRivers.Open(filename2, null);
sfRivers.StartEditingShapes(true, null);
Utils utils = new Utils();
Shapefile sfNew = sfRivers.Clone();
LinePattern pattern = new LinePattern();
pattern.AddLine(utils.ColorByName(tkMapColor.Blue), 8, tkDashStyle.dsSolid);
pattern.AddLine(utils.ColorByName(tkMapColor.LightBlue), 4, tkDashStyle.dsSolid);
options.LinePattern = pattern;
options.UseLinePattern = true;
for (int i = 0; i < sfParcels.NumShapes; i++)
{
Shape shp1 = sfParcels.Shape[i];
double length = 0.0; // the length of intersection
for (int j = 0; j < sfRivers.NumShapes; j++)
{
Shape shp2 = sfRivers.Shape[j];
if (shp1.Intersects(shp2))
{
Shape result = shp1.Clip(shp2, tkClipOperation.clIntersection);
if (result != null)
{
int index = sfNew.EditAddShape(result);
length += result.Length;
}
}
}
sfParcels.EditCellValue(fieldIndex, i, length);
label.Text = string.Format("Parcel: {0}/{1}", i + 1, sfParcels.NumShapes);
Application.DoEvents();
}
// generating charts
var chartField = new ChartField();
chartField.Name = "Length";
chartField.Color = utils.ColorByName(tkMapColor.LightBlue);
chartField.Index = fieldIndex;
sfParcels.Charts.AddField(chartField);
sfParcels.Charts.Generate(tkLabelPositioning.lpInteriorPoint);
sfParcels.Charts.ChartType = tkChartType.chtBarChart;
sfParcels.Charts.BarHeight = 100;
sfParcels.Charts.ValuesVisible = true;
sfParcels.Charts.Visible = true;
axMap1.AddLayer(sfParcels, true);
axMap1.AddLayer(sfRivers, true);
axMap1.AddLayer(sfNew, true);
axMap1.ZoomToMaxExtents();
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
tkMapColor
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
tkDashStyle
The available style of lines. Can be used for drawing polylines and outlines of the polygons.
Definition: Enumerations.cs:442
tkChartType
The available types of chart.
Definition: Enumerations.cs:200
FieldType
The available types of fields of dbf table.
Definition: Enumerations.cs:34
tkClipOperation
The available clipping operation for Shape.Clip method.
Definition: Enumerations.cs:269
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
Represents a single bar or sector of the chart.
Definition: ChartField.cs:69
bool AddField(ChartField Field)
Adds a field to the chart.
Definition: Charts.cs:108
bool Generate(tkLabelPositioning Type)
Generates a chart for every shape of the parent shapefile.
Definition: Charts.cs:255
int BarHeight
Gets or sets the maximum height of the bar chart in pixels.
Definition: Charts.cs:141
tkChartType ChartType
Gets or sets the type of the charts.
Definition: Charts.cs:171
bool Visible
Turns on or off the visibility of charts.
Definition: Charts.cs:662
bool ValuesVisible
Gets or sets the value which indicates whether labels with values of the fields will be drawn near ea...
Definition: Charts.cs:635
Represents a single field of the attribute table.
Definition: Field.cs:48
Provides means for defining custom pattern from lines and point symbols for rendering polyline layers...
Definition: LinePattern.cs:46
void AddLine(uint Color, float Width, tkDashStyle style)
Adds a line segment to the pattern.
Definition: LinePattern.cs:54
Holds the set of options for visualization of shapefiles.
Definition: ShapeDrawingOptions.cs:239
float LineWidth
Gets or sets the width of the lines to draw shapes.
Definition: ShapeDrawingOptions.cs:624
uint LineColor
Gets or sets the line color of the shapes.
Definition: ShapeDrawingOptions.cs:577
LinePattern LinePattern
Gets or sets line pattern for rendering polyline shapefile.
Definition: ShapeDrawingOptions.cs:586
bool UseLinePattern
Gets or set the value which indicates whether line pattern will be used to render polyline shapefile.
Definition: ShapeDrawingOptions.cs:790
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
Shape Clip(Shape shape, tkClipOperation operation)
Performs clipping operation with 2 shapes.
Definition: Shape.cs:111
bool Intersects(Shape shape)
Tests an "intersects" relation between 2 shapes.
Definition: Shape.cs:396
double Length
Calculates the length of polyline shape.
Definition: Shape.cs:446
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Charts Charts
Gets or sets an instance of Charts class associated with the shapefile.
Definition: Shapefile.cs:195
int NumShapes
Gets the number of shapes in the shapefile.
Definition: Shapefile.cs:254
ShapeDrawingOptions DefaultDrawingOptions
Gets or sets an instance of ShapeDrawingOptions class which holds default drawing options.
Definition: Shapefile.cs:111
A utils object provides access to a set of utility functions to perform a variety of tasks on other o...
Definition: Utils.cs:20
uint ColorByName(tkMapColor Name)
Returns the numeric representation for the specified color.
Definition: Utils.cs:40
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
bool GrabProjectionFromData
Gets or sets a value indicating whether projection for will be taken from the first datasource added ...
Definition: AxMap.cs:2701
void ZoomToMaxExtents()
Zooms the map to the maximum extents of all loaded layers.
Definition: AxMap.cs:652
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
int EditAddShape(Shape shape)
Adds a new shape to the shapefile.
Definition: Shapefile.cs:863
bool StartEditingShapes(bool startEditTable, ICallback cBack)
Starts editing mode for the shapefile.
Definition: Shapefile.cs:840
Shapefile Clone()
Creates a copy of the shapefile.
Definition: Shapefile.cs:1376
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430
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 EditInsertField(Field newField, ref int fieldIndex, ICallback cBack)
Inserts a new field in the shapefile attribute table. The table must be in editing mode.
Definition: Shapefile.cs:658