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.
using System; using System.Windows.Forms; using AxMapWinGIS; using MapWinGIS; using System.IO; public partial class MapExamples { // <summary> // Selects buildings which lie within specified distance from the parks. // </summary> public void SelectByDistance(AxMap axMap1, string dataPath) { 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; } Shapefile sfBuildings = new Shapefile(); sfBuildings.Open(filename1, null); Shapefile sfParks = new Shapefile(); sfParks.Open(filename2, null); ShapefileCategory ct = sfParks.Categories.Add("Parks"); // choose parks and make them green ct.Expression = "[Type] = \"Park\""; Utils 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.get_ShapeCategory(i); if (index == 0) { object result = null; Shape shp = sfParks.get_Shape(i); if (sfBuildings.SelectShapes(shp.Extents, maxDistance, SelectMode.INTERSECTION, ref result)) { int[] shapes = result as int[]; for (int j = 0; j < shapes.Length; j++) { if (!sfBuildings.get_ShapeSelected(shapes[j])) { Shape shp2 = sfBuildings.get_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(); } }
1.7.6.1