CreateBuffer.cs

This example demonstrates how to create several buffers around the rivers. Four buffers are build sequentially, each of them as the separate shapefile.The overlapping shapes of each buffer are merged together. Then smaller buffers are subtracted from the larger ones, making "holes" in them. After it all 4 buffers are copied to a single shapefile with buffer distance field. Finally a color scheme ranging from blue to yellow is applied. An implementation of ICallback interface is used for reporting progress information. Here is a screenshot with the results of the code execution.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Creates several buffers around the waterways.
// </summary>
public void CreateBuffer(AxMap axMap1, string dataPath, System.Windows.Forms.ToolStripStatusLabel label)
{
axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
string filename = dataPath + "waterways.shp";
if (!File.Exists(filename))
{
MessageBox.Show("The shapefile with rivers wasn't found: " + filename);
}
else
{
var callback = new Callback(label);
var sf = new Shapefile();
if (!sf.Open(filename, callback))
{
MessageBox.Show(sf.ErrorMsg[sf.LastErrorCode]);
}
else
{
int layerHandle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
var utils = new Utils();
sf.DefaultDrawingOptions.LineWidth = 3.0f;
sf.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue);
const double distance = 150; // meters
var buffers = new List<Shapefile>();
for (int i = 1; i < 5; i++)
{
Shapefile sfBuffer = sf.BufferByDistance(distance * i, 30, false, true);
if (sfBuffer == null)
{
MessageBox.Show("Failed to calculate the buffer: " + sf.ErrorMsg[sf.LastErrorCode]);
return;
}
else
{
sfBuffer.GlobalCallback = callback;
buffers.Add(sfBuffer);
}
}
// now subtract smaller buffers from larger ones
for (int i = buffers.Count - 1; i > 0; i--)
{
Shapefile sfDiff = buffers[i].Difference(false, buffers[i - 1], false);
if (sfDiff == null)
{
MessageBox.Show("Failed to calculate the difference: " + sf.ErrorMsg[sf.LastErrorCode]);
return;
}
else
{
buffers[i] = sfDiff;
}
}
// pass all the resulting shapes to a single shapefile and mark their distance
Shapefile sfResult = buffers[0].Clone();
sfResult.GlobalCallback = callback;
int fieldIndex = sfResult.EditAddField("Distance", FieldType.DOUBLE_FIELD, 10, 12);
for (int i = 0; i < buffers.Count; i++ )
{
Shapefile sfBuffer = buffers[i];
for (int j = 0; j < sfBuffer.NumShapes; j++)
{
int index = sfResult.NumShapes;
sfResult.EditInsertShape(sfBuffer.Shape[j].Clone(), ref index);
sfResult.EditCellValue(fieldIndex, index, distance * (i + 1));
}
}
// create visualization categories
sfResult.DefaultDrawingOptions.FillType = tkFillType.ftStandard;
sfResult.Categories.Generate(fieldIndex, tkClassificationType.ctUniqueValues, 0);
// apply color scheme
var scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.LightBlue, tkMapColor.LightYellow);
sfResult.Categories.ApplyColorScheme(tkColorSchemeType.ctSchemeGraduated, scheme);
layerHandle = axMap1.AddLayer(sfResult, true);
axMap1.Redraw();
//sfResult.SaveAs(@"c:\buffers.shp", null);
}
}
}
}
class Callback : ICallback
{
private ToolStripStatusLabel m_label = null;
public Callback(ToolStripStatusLabel label)
{
m_label = label;
if (label == null)
throw new NullReferenceException("No reference to the label was provided");
}
public void Error(string KeyOfSender, string ErrorMsg)
{
Debug.Print("Error reported: " + ErrorMsg);
}
public void Progress(string KeyOfSender, int Percent, string Message)
{
m_label.Text = Message + ": " + Percent + "%";
Application.DoEvents();
}
}
}
tkClassificationType
The type of the classification available for ShapefileCategories.Generate and Labels....
Definition: Enumerations.cs:227
tkMapColor
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
tkColorSchemeType
The type of color scheme. Determines how colors will be extracted from the color scheme (see Shapefil...
Definition: Enumerations.cs:305
tkFillType
The type of shape fill.
Definition: Enumerations.cs:521
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
void Redraw()
Redraws all layers in the map if the map is not locked.
Definition: AxMap.cs:183
Provides methods for random colour generation and colour interpolation based on the specific set of c...
Definition: ColorScheme.cs:65
tkFillType FillType
The type of the polygon fill. See the enumeration for details.
Definition: ShapeDrawingOptions.cs:539
void ApplyExpressions()
Maps shapes to the visualization categories based in ShapefileCategory.Expression.
Definition: ShapefileCategories.cs:143
void ApplyColorScheme(tkColorSchemeType Type, ColorScheme ColorScheme)
Applies color scheme to the visualization categories.
Definition: ShapefileCategories.cs:94
bool Generate(int FieldIndex, tkClassificationType ClassificationType, int numClasses)
Generates visualization categories by certain attribute
Definition: ShapefileCategories.cs:190
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
ICallback GlobalCallback
Gets or sets a Callback object which handles progress and error messages of the Shapefile class.
Definition: Shapefile.cs:219
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
ShapefileCategories Categories
Gets or sets an instance of ShapefileCategories class associated with the shapefile.
Definition: Shapefile.cs:81
A utils object provides access to a set of utility functions to perform a variety of tasks on other o...
Definition: Utils.cs:20
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 EditInsertShape(Shape shape, ref int shapeIndex)
Inserts a new shape in the shapefile.
Definition: Shapefile.cs:819
Shapefile Difference(bool selectedOnlySubject, Shapefile sfOverlay, bool selectedOnlyOverlay)
Calculates difference of 2 shapefiles.
Definition: Shapefile.cs:1087
Shapefile BufferByDistance(double distance, int nSegments, bool selectedOnly, bool mergeResults)
Creates a new shapefile by building a buffer around the shapes of the input shapefile.
Definition: Shapefile.cs:1057
Shapefile Clone()
Creates a copy of the shapefile.
Definition: Shapefile.cs:1376
int EditAddField(string name, FieldType type, int precision, int width)
Adds a field to the attribute table of the shapefile. The table must be in editing mode.
Definition: Shapefile.cs:708
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
An interface for callback objects which can be used to return information about progress and errors.
Definition: GlobalCallback.cs:34