0

I'm using NetTopologySuite to read a shapefile like below code:

        private FeatureDataTable GetFeatureTableFromShapefile(string shapeFilePath)
    {
        GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
        FeatureDataSet ds;
        using (var sf = new ShapeFile(shapeFilePath) {Encoding = Encoding.Default})
        {
            sf.Open();
            var ext = sf.GetExtents();
            ds = new FeatureDataSet();
            sf.ExecuteIntersectionQuery(ext, ds);
        }

        return ds.Tables[0];
    }

Now I want to merge all features (GeoMetry) and create only one feature from them.

Please note that I'm trying to create one feature and not something like multipolygon like below image: enter image description here

For above image, i used this code, but it's not what i want, because there still a red line between to features that i tried to merge:

            var entities = new List<DbGeometry>(){ d1.Geom, d2.Geom};
        DbGeometry allBoundaries = null;
        for (var i = 0; i < entities.Count; i++)
        {
            if (i == 0) allBoundaries = entities[i].Boundary;
            else allBoundaries = allBoundaries.Union(entities[i].Boundary);
        }

So is there any way to accomplish this?

2
  • It sounds like you are looking for a tool similar to Dissolve in ArcGIS.
    – SMiller
    Commented Dec 18, 2018 at 20:44
  • @smiller That's right. something like that but programatically Commented Dec 19, 2018 at 6:18

1 Answer 1

3

A problem with such a merge is that the polygons' vertices might not be perfectly aligned. It consists of floats that will have small differences. Not sure if there is a perfect solution here.

One workaround is to grow the polygons with a tiny buffer, then union, and then shrink with that same buffer.

Another solution is to snap your points to a grid before Union. I think NTS's SnappedUnion does this

else allBoundaries = allBoundaries.SnappedUnion(entities[i].Boundary, 0.0001);
4
  • oh, and you could also go all fancy and represent your data topological. Meaning the adjacent geometries have a reference to one and the same vertex on their shared border. I guess this is actually the proper solution, but not easy. Commented Dec 18, 2018 at 21:04
  • Your link refer to sql server solution, Is there any c# code for that. Commented Dec 19, 2018 at 6:31
  • I updated my answer Commented Dec 19, 2018 at 19:19
  • Thanks, Now i can do what i wanted, but the code i'm using is this: NetTopologySuite.Operation.Overlay.Snap.SnapOverlayOp.Union Commented Dec 20, 2018 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.