简体   繁体   中英

Why is sharpmap returning inconsistent results while mapping from point to polygon?

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)
        {
            provider.Open();
        }
        var ids = provider.GetObjectIDsInView(envelop);

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

            if (geom.Contains(ntsPoint))
            {
                matchingRowIds.Add(id);
            }
        }
        // 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?

Note : I am using sharpMap v1.0.4.1

We figured the issue here. Accessing shapefiles via SharpMap v1.0.4.1 is not entirely threadsafe, the above function was being called by multiple threads for the same shape file which was causing issues with concurrency. Removing the parallelization fixed the issue for now. The latest version of sharpmap v1.1 seems to have fixed some of the issues with concurency.

Found this in their release notes for the v1.1 - "Fixed issue with simultaneous readers of the same shapefile (locking error of shx-file)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM