Select element on the map

Topics: Algorithms, CompactFramework, Data Access, General Topics, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, WinForms Controls
Nov 30, 2008 at 11:51 AM
Hi all.
I want to select element (point, line, polyline) on the map.
What mapImage.ActiveTool it is necessary choose?


        public SharpMap.Data.FeatureDataRow FindGeoNearPoint(SharpMap.Geometries.Point pos, SharpMap.Layers.VectorLayer layer, double amountGrow)
        {
            SharpMap.Geometries.BoundingBox myBoundingBox = pos.GetBoundingBox();
            SharpMap.Data.FeatureDataSet myDataSet = new SharpMap.Data.FeatureDataSet();
            layer.DataSource.Open();
            layer.DataSource.ExecuteIntersectionQuery(myBoundingBox, myDataSet);
            DataTable myDataTable = myDataSet.Tables[0] as SharpMap.Data.FeatureDataTable; // --     While myDataTable is empty??
            GisSharpBlog.NetTopologySuite.IO.WKTReader myReader = new GisSharpBlog.NetTopologySuite.IO.WKTReader();
            GeoAPI.Geometries.IGeometry point = myReader.Read(pos.ToString());
            if (myDataTable.Rows.Count == 0)
                return null;

            double distance = point.Distance(myReader.Read((myDataTable.Rows[0] as SharpMap.Data.FeatureDataRow).Geometry.ToString()));

            SharpMap.Data.FeatureDataRow selectedFeature = myDataTable.Rows[0] as SharpMap.Data.FeatureDataRow;
            if (myDataTable.Rows.Count > 1)
            {
                for (int i = 1; i < myDataTable.Rows.Count; i++)
                {
                    GeoAPI.Geometries.IGeometry line = myReader.Read((myDataTable.Rows[i] as SharpMap.Data.FeatureDataRow).Geometry.ToString());
                    if (point.Distance(line) < distance)
                    {
                        distance = point.Distance(line);

                        selectedFeature = myDataTable.Rows[i]

                        as SharpMap.Data.FeatureDataRow;
                    }
                }
            }

            return selectedFeature;
        }

        private void mapImage1_MouseClick(object sender, MouseEventArgs e)
        {
            Map _map = mapImage1.Map;
            if (_map.Layers.Count > 0)
            {
                SharpMap.Layers.VectorLayer needLayer = (SharpMap.Layers.VectorLayer)_map.Layers[0];

                SharpMap.Data.FeatureDataRow rowSelected = FindGeoNearPoint(new SharpMap.Geometries.Point(e.X, e.Y), needLayer, 100);

                SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");

                if (rowSelected != null)
                {
                    laySelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(rowSelected.Geometry);

                    laySelected.Style.Fill = new System.Drawing.SolidBrush(SystemColors.Highlight);

                    _map.Layers.Add(laySelected);
                }
                mapImage1.Map = _map;
            }
        }

What is not incorrectly?

Thanks for all!

Aug 21, 2009 at 12:06 PM

SharpMap.Geometries.BoundingBox myBoundingBox = new SharpMap.Geometries.BoundingBox(new SharpMap.Geometries.Point(pos.X-100, pos.Y-100), new SharpMap.Geometries.Point(pos.X+100, pos.Y+100));