MinimalDistance.cs

This example demonstrates how to calculate the minimal distance from each building to the river and visualize it by charts. 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>
// Calculates a minimal distance from each building to the river.
// </summary>
public void MinimalDistance(AxMap axMap1, string dataPath, ToolStripStatusLabel label)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
string filename1 = dataPath + "waterways.shp";
string filename2 = dataPath + "buildings.shp";
if (!File.Exists(filename1) || !File.Exists(filename2))
{
MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath);
}
else
{
Shapefile sfRivers = new Shapefile();
sfRivers.Open(filename1, null);
Utils utils = new Utils();
Shapefile sfBuildings = new Shapefile();
sfBuildings.Open(filename2, null);
// adds a field in the table
Field field = new Field();
field.Name = "RiverDist";
field.Type = FieldType.DOUBLE_FIELD;
field.Precision = 10;
int fieldIndex = sfBuildings.NumFields;
sfBuildings.StartEditingShapes(true, null);
sfBuildings.EditInsertField(field, ref fieldIndex, null);
ct.Expression = "[Name] <> \"\"";
sfRivers.StartEditingShapes(false, null);
for (int i = 0; i < sfBuildings.NumShapes; i++)
{
if (sfBuildings.ShapeCategory[i] == 0)
{
label.Text = "Processing building: " + (i + 1) + " / " + sfBuildings.NumShapes;
Application.DoEvents();
Shape shp = sfBuildings.Shape[i];
double minDist = Double.MaxValue;
for (int j = 0; j < sfRivers.NumShapes; j++)
{
Shape shp2 = sfRivers.Shape[j];
double distance = shp.Distance(shp2);
if (distance < minDist)
minDist = distance;
}
if (minDist != Double.MaxValue)
sfBuildings.EditCellValue(fieldIndex, i, minDist);
}
else
{
sfBuildings.EditCellValue(fieldIndex, i, 0.0);
}
}
sfRivers.StopEditingShapes(false, true, null);
sfBuildings.Categories.Generate(fieldIndex, tkClassificationType.ctNaturalBreaks, 8);
ColorScheme scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.Blue, tkMapColor.Yellow);
sfBuildings.Labels.Generate("[Name] + \"\n\" + [RiverDist] + \" m\"", tkLabelPositioning.lpCentroid, true);
sfBuildings.Labels.TextRenderingHint = tkTextRenderingHint.SystemDefault;
sfBuildings.VisibilityExpression = "[Name] <> \"\"";
label.Text = "";
}
}
}
}
tkLabelPositioning
The available positioning of the label relative to the parent shape.
Definition: Enumerations.cs:835
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
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
Provides methods for random colour generation and colour interpolation based on the specific set of c...
Definition: ColorScheme.cs:65
void SetColors2(tkMapColor Color1, tkMapColor Color2)
Clears all the existing breaks and creates 2 breaks with the specified colours.
Definition: ColorScheme.cs:156
Represents a single field of the attribute table.
Definition: Field.cs:48
FieldType Type
Gets or sets the field type of the field.
Definition: Field.cs:143
string Name
Gets or sets the name of the field.
Definition: Field.cs:125
int Precision
Gets or sets the precision of the field. Precision only applies to fields of fieldtype double.
Definition: Field.cs:134
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
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
double Distance(Shape shape)
Calculates the distance between 2 shapes.
Definition: Shape.cs:240
Creates a new visualization category, adds it to the list and returns its reference to the caller.
Definition: ShapefileCategories.cs:66
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
Represents a set of visualization options for shapefile layer.
Definition: ShapefileCategory.cs:47
string Expression
Gets or sets expression which defines shapes which belong to the category.
Definition: ShapefileCategory.cs:61
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Labels Labels
Gets or sets the instance of the Labels class associated with the shapefile.
Definition: Shapefile.cs:184
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
string VisibilityExpression
Gets or sets the expression which defines shapes to be visible on the map.
Definition: Shapefile.cs:285
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
int Generate(string Expression, tkLabelPositioning Method, bool LargestPartOnly)
Generates labels for each shape of the parent shapefile.
Definition: Labels.cs:490
tkTextRenderingHint TextRenderingHint
Gets or sets rendering hint to be used during GDI+ rendering.
Definition: Labels.cs:1367
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
Adds a layer to the map.
Definition: AxMap.cs:1342
bool StopEditingShapes(bool applyChanges, bool stopEditTable, ICallback cBack)
Stops editing modes for the shapefile and optionally saves the changes.
Definition: Shapefile.cs:853
bool StartEditingShapes(bool startEditTable, ICallback cBack)
Starts editing mode for the shapefile.
Definition: Shapefile.cs:840
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
int NumFields
Gets the number of fields in attribute table of the shapefile.
Definition: Shapefile.cs:672