SelectByDistance.cs

This example demonstrates how to select buildings which lie within specified distance from the parks. 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>
// Selects buildings which lie within specified distance from the parks.
// </summary>
public void SelectByDistance(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
string filename1 = dataPath + "buildings.shp";
string filename2 = dataPath + "natural.shp";
if (!File.Exists(filename1) || !File.Exists(filename2))
{
MessageBox.Show("Failed to open shapefile (natural.shp, buildings.shp): " + dataPath);
return;
}
var sfBuildings = new Shapefile();
sfBuildings.Open(filename1, null);
var sfParks = new Shapefile();
sfParks.Open(filename2, null);
ShapefileCategory ct = sfParks.Categories.Add("Parks");
// choose parks and make them green
ct.Expression = "[Type] = \"Park\"";
var utils = new Utils();
ct.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Green);
sfParks.Categories.ApplyExpression(0);
// hide the rest types of objects on the layer
sfParks.DefaultDrawingOptions.Visible = false;
double maxDistance = 150.0; // in meters
bool editing = sfBuildings.StartEditingShapes(true, null);
sfBuildings.UseQTree = true; // this will build a spatial index to speed up selection
for (int i = 0; i < sfParks.NumShapes; i++)
{
int index = sfParks.ShapeCategory[i];
if (index == 0)
{
object result = null;
Shape shp = sfParks.Shape[i];
if (sfBuildings.SelectShapes(shp.Extents, maxDistance, SelectMode.INTERSECTION, ref result))
{
int[] shapes = result as int[];
if (shapes == null) return;
for (int j = 0; j < shapes.Length; j++)
{
if (!sfBuildings.ShapeSelected[shapes[j]])
{
Shape shp2 = sfBuildings.Shape[shapes[j]];
double dist = shp.Distance(shp2);
if (dist < maxDistance)
sfBuildings.set_ShapeSelected(shapes[j], true);
}
}
}
}
}
axMap1.AddLayer(sfParks, true);
axMap1.AddLayer(sfBuildings, true);
axMap1.ZoomToMaxExtents();
}
}
}
SelectMode
The selection mode, which determines which shapes will be considered as included in the rectangular s...
Definition: Enumerations.cs:149
tkMapColor
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
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
uint FillColor
Gets or sets the fill color of the shape.
Definition: ShapeDrawingOptions.cs:471
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
Extents Extents
Gets the extents of the shape.
Definition: Shape.cs:286
double Distance(Shape shape)
Calculates the distance between 2 shapes.
Definition: Shape.cs:240
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
ShapeDrawingOptions DrawingOptions
Gets or sets visualization options associated with the category.
Definition: ShapefileCategory.cs:52
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
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
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