Select Geometry using GeoAPI

Topics: Algorithms, SharpMap Project, SharpMap v0.9 / v1.x
Dec 29, 2012 at 1:57 PM

hi there i'm trying to create a function that will select a layers geometry and change its colour or highlight it.


I've found the FindGeoNearPoint function but this deals with the old SharpMap.Geometries whih no longer exists and has been replaced with GeoAPI.Geometries.

How do I alter the FindGeoNearPoint Function to use GeoAPI

Here is my attempt


public SharpMap.Data.FeatureDataRow FindGeoNearPoint(GeoAPI.Geometries.IPoint pos, SharpMap.Layers.VectorLayer layer, double amountGrow)

            GeoAPI.Geometries.Envelope bbox = new GeoAPI.Geometries.Envelope();
            SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();

            layer.DataSource.ExecuteIntersectionQuery(bbox, ds);

            DataTable tbl = ds.Tables[0] as SharpMap.Data.FeatureDataTable;

            NetTopologySuite.IO.WKTReader reader = new NetTopologySuite.IO.WKTReader();

            GeoAPI.Geometries.IGeometry point = reader.Read(pos.ToString());

            if (tbl.Rows.Count == 0)

                return null;

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

            SharpMap.Data.FeatureDataRow selectedFeature = tbl.Rows[0] as SharpMap.Data.FeatureDataRow;

            if (tbl.Rows.Count > 1)

                for (int i = 1; i < tbl.Rows.Count; i++)

                    GeoAPI.Geometries.IGeometry line = reader.Read((tbl.Rows[i] as SharpMap.Data.FeatureDataRow).Geometry.ToString());

                    if (point.Distance(line) < distance)

                        distance = point.Distance(line);

                        selectedFeature = tbl.Rows[i] as SharpMap.Data.FeatureDataRow;



            return selectedFeature;


Jan 2, 2013 at 7:43 AM

You can omit all the conversion code, since we are using GeoAPI/NTS geometries by default:

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;

Hth FObermaier