PointInPolygonCS - sindizzy/DSW GitHub Wiki
Point In Polygon Testing
This example uses selection, but is not the easiest way to select in response to mouse clicks, which is already provided as a MapFunction. Instead, this give an idea of a practical application of selecting a polygon based on the fact that it intersects with a coordinate that is clicked. This relies on a very new fix to the code base to work correctly, but if you have the latest version it should work fine.
{{ using System; using System.Windows.Forms; using MapWindow.Data; using MapWindow.Geometries; using MapWindow.Map; namespace WindowsFormsApplication19 { public partial class Form1 : Form { public Form1() { InitializeComponent(); map1.MouseUp += map1_MouseUp; map1.FunctionMode = FunctionModes.Pan; }
// Handle the mouse up event
void map1_MouseUp(object sender, MouseEventArgs e)
{
// only select polygons with the right button
if(e.Button != MouseButtons.Right) return;
// Get the geographic location of the mouse click
Coordinate c = map1.PixelToProj(e.Location);
// Return if no data layers exist
if(map1.Layers.Count < 1) return;
// Also, mpl can be used to change the selection color.
IMapPolygonLayer mpl = map1.Layers[0](0) as IMapPolygonLayer;
// If the first layer is not a polygon layer, exit.
if(mpl == null) return;
// Get the featureset that contains the actual polygon data for the layer
IFeatureSet fs = map1.Layers[0](0).DataSet as IFeatureSet;
// If this is null or not a featureset, exit
if(fs == null) return;
// Set up an index
int iShape = 0;
// Cycle through the shapes
foreach (ShapeRange shape in fs.ShapeIndices)
{
// Test if the coordinate is in the polygon
if(shape.Intersects(c))
{
// Select the polygon if the the coordinate intersects.
mpl.Select(iShape);
}
iShape++;
}
}
// Allow the addition of background layers
private void btnAdd_Click(object sender, EventArgs e)
{
map1.AddLayer();
}
}
} }}