inconsistent results while mapping from point to polygon

Topics: General Topics, SharpMap Project
Sep 16, 2014 at 8:14 PM
I am using a shapefile(shp) that has an addition field called "ID". The shapefile is clean and does not have any overlapping polygons. When I pass this shapeFile to the following function, I keep getting inconsistent results. (The Id returned is not always the same for same set of latitude longitude).
public static long? GetIdFromLatLong(IProvider provider, double lat, double lon)
    var matchingRowIds = new Collection<uint>();
    var vertex = new GeoAPI.Geometries.Coordinate(lon, lat);

    var ntsPoint = new NetTopologySuite.Geometries.Point(vertex.X, vertex.Y);

    var envelop = new GeoAPI.Geometries.Envelope(vertex);
    if (!provider.IsOpen)
    var ids = provider.GetObjectIDsInView(envelop);

    foreach (uint id in ids)
        var geom = provider.GetGeometryByID(id);

        if (geom.Contains(ntsPoint))
    // Get region Id from RowId
    var matchedId = matchingRowIds.Count == 0
        ? null
        : matchingRowIds.Select(i => (long?) provider.GetFeature(i)["ID"]).FirstOrDefault(i => i != 0);
    return matchedId;
I initially suspected that there are multiple polygons that are being matched and the FirstOrDefault is causing me to get different results but that does not seem to be the case either because when I put a breakpoint in this function, I always have only one entry in matchingRowIds.

Am I doing something wrong/Is there a better way to get the polygon that contains a given point from a shapefile?

FYI: I am using sharpMap v1.0.4.1
Sep 17, 2014 at 7:15 AM
Edited Sep 17, 2014 at 7:19 AM
You can use the shapefile provider's ExecuteIntersectionQuery function to get the desired result.
Don't forget to set ((ShapeFile)provider).DoTrueIntersectionQuery = true;

From looking at your code, I can't tell why you'd get different results. If you want us to evaluate that, submit an issue along with your shapefile and sample points.