This project has moved. For the latest updates, please go here.

Select polygon from the map

Topics: SharpMap v2.0
Apr 14, 2015 at 9:39 AM

How to pick polygon on the map?

I try this code:
private void mapBox1_MouseClick(object sender, MouseEventArgs e)
            if (mapBox1.ActiveTool != MapBox.Tools.DrawPolygon)
                Envelope box = new Envelope(mapBox1.Map.ImageToWorld(e.Location));

                FeatureDataSet ds = new FeatureDataSet();

                ILayer rajz = mapBox1.Map.Layers.GetLayerByName("rajz");
                if (rajz != null)
                    VectorLayer vl = (VectorLayer)rajz;
                    GeometryFeatureProvider gf = (GeometryFeatureProvider)vl.DataSource;

                    gf.ExecuteIntersectionQuery(box, ds);
                    FeatureDataTable dt = ds.Tables[0];
                    if (dt.Count > 0)
                        Coordinate worldPos = mapBox1.Map.ImageToWorld(e.Location);
                        FeatureDataRow row = FindGeoNearPoint(new NetTopologySuite.Geometries.Point(worldPos.X, worldPos.Y), vl, 0);

The problem is that when you click the red X marked area also popup the message.
That there may be a way that only detect clicks within the territory of the polygon?
Apr 16, 2015 at 6:16 AM
Edited Apr 16, 2015 at 6:17 AM
IProvider.ExectueIntersectionQuery(Envelope e, FeatureDataSet ds) only checks the bounding boxes for intersection, that is why you also get the marked polygon as a result.
If you switch to use IProvider.ExectueIntersectionQuery(IGeometry geom, FeatureDataSet ds), you should only get true intersection results in the first place and don't need to call FindGeoNearPoint at all:
gf.ExecuteIntersectionQuery(new NetTopologySuite.Geometries.Point(worldPos.X, worldPos.Y), ds);
Marked as answer by Destroy666 on 4/16/2015 at 1:07 AM