This project has moved and is read-only. For the latest updates, please go here.

how can I select line on mapbox ?

Topics: Data Access, General Topics, SharpMap Project, WinForms Controls
Oct 26, 2014 at 11:50 PM
Hello everybody. I have an issue?
I want to select any line on my roads table on mymapbox ( image 1 )

When I select any line on mymapbox, line's attribute appear left side on image 2
But I coundt do it ?
How can I do it ? please help to me.
Actually I can select any point on mymapbox with FindGeoNearPoint.
This is my code that is work.
public static SharpMap.Data.FeatureDataRow FindGeoNearPoint(GeoAPI.Geometries.IPoint point, SharpMap.Layers.VectorLayer layer, double amountGrow)
            var box = new GeoAPI.Geometries.Envelope(point.Coordinate);

            var fds = new SharpMap.Data.FeatureDataSet();
            layer.DataSource.ExecuteIntersectionQuery(box, fds);

            SharpMap.Data.FeatureDataRow result = null;
            var minDistance = double.MaxValue;

            foreach (SharpMap.Data.FeatureDataTable fdt in fds.Tables)
                foreach (SharpMap.Data.FeatureDataRow fdr in fdt.Rows)
                    if (fdr.Geometry != null)
                        var distance = point.Distance(fdr.Geometry);
                        if (distance < minDistance)
                            result = fdr;
                            minDistance = distance;
            return result;
 private void mapBox1_MouseDown(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)

 VectorLayer Pdpoint = new VectorLayer("levhanokta", new PostGIS("server=localhost;port=5432;user=postgres;pwd=hs427034;database=kgmdb", "levhanokta", "geom", "id"));
                        GeoAPI.Geometries.IPoint point = new NetTopologySuite.Geometries.Point(worldPos.X, worldPos.Y);
                        SharpMap.Data.FeatureDataRow rowSelected = FindGeoNearPoint(point, Pdpoint, 50);
                        SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");
                        laySelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(rowSelected.Geometry);
                        laySelected.Style.PointSymbolizer = SharpMap.Rendering.Symbolizer.PathPointSymbolizer.CreateCircle(new Pen(System.Drawing.Color.Gold, 4), Brushes.Transparent, 25);
                        laySelected.Style.PointSize = 0;
                        laySelected.Style.Fill = Brushes.Transparent;
But it works only with point. I want to select any line ? How can I do it ?
Oct 27, 2014 at 5:38 PM
Edited Oct 27, 2014 at 5:41 PM
Does FindGeoNearPoint return anything?
If not maybe you need to transform to a different coordinate system.

MapBox has its own query tool. Simply set MapBox.ActiveTool to Query...
Oct 29, 2014 at 8:00 PM
Thank you for your answer. :)
I found something thats are working.
 public SharpMap.Data.FeatureDataRow FindGeoNearLinePoint(GeoAPI.Geometries.IPoint pos, SharpMap.Layers.VectorLayer layer, double amountGrow)
            var myBoundingBox = new GeoAPI.Geometries.Envelope(pos.Coordinate);
            SharpMap.Data.FeatureDataSet myDataSet = new SharpMap.Data.FeatureDataSet();
            layer.DataSource.ExecuteIntersectionQuery(myBoundingBox, myDataSet);
            DataTable myDataTable = myDataSet.Tables[0] as SharpMap.Data.FeatureDataTable; 
            NetTopologySuite.IO.WKTReader myReader = new 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 mapBox1_MouseDown(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)

        SharpMap.Map _map = mapBox1.Map;
        if (_map.Layers.Count > 0)
            SharpMap.Layers.VectorLayer needLayer = (SharpMap.Layers.VectorLayer)_map.Layers["Leksen"];
            GeoAPI.Geometries.IPoint point = new NetTopologySuite.Geometries.Point(worldPos.X, worldPos.Y);
            SharpMap.Data.FeatureDataRow rowSelected = FindGeoNearLinePoint(point, needLayer, 100);
            SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");
            if (rowSelected != null)
                laySelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(rowSelected.Geometry);
                textBox1.Text = rowSelected.ItemArray[0].ToString();
                textBox2.Text = rowSelected.ItemArray[1].ToString();
                textBox3.Text = rowSelected.ItemArray[2].ToString();
                textBox4.Text = rowSelected.ItemArray[3].ToString();
                textBox5.Text = rowSelected.ItemArray[4].ToString();
                textBox6.Text = rowSelected.ItemArray[5].ToString();
                textBox7.Text = rowSelected.ItemArray[6].ToString();
                laySelected.Style.Fill = new System.Drawing.SolidBrush(SystemColors.Highlight);
                laySelected.Style.Line.Width = 20;
                laySelected.Style.Line.Color = Color.HotPink;
            mapBox1.Map = _map;
I wonder about MapBox.ActiveTool to Query.
How can I use that. Please give me some tips or sample code.
When I select with "MapBox.ActiveTool to Query", can I get selected feature attribute?
Marked as answer by huseyin8316 on 10/29/2014 at 12:01 PM
Oct 30, 2014 at 10:43 AM
Why do you always write a geometry to Well-Known-Text (ToString) and read that using WKTReader before you actually use it.
THIS IS NOT NECESSARY. You can use the geometry passed or from the FeatureDataRow directly. If you are afraid to change it, use Geometry.Clone().

If you set MapBox.ActiveTool to MapBox.Tools.QueryBox or MapBox.Tools.QueryPoint, you don't need to handle MapBox.MouseDown event.
You'll receive a MapQueryStarted event, a series of MapQueried events and a MapQueryDone event.
The MapQueried event will return a FeatureDataTable with all objects satisfying the query. You can control the behavior by changing MapBox.MapQueryMode.