Search code examples
.net.net-coregeometrygeospatialnettopologysuite

Get total bounding box of several polygons (using C# NetCore NetTopologySuite)


I'm a bit new to working with polygon data (in C# using NetTopologySuite) and want to get the bounding box of several polygons, depending on the fact whether the bounding box of each polygon is overlapping with another polygon (polygon clustering).

In this demo, I have 3 polygons, whose bounding boxes overlap with the others, and would like to have the red bounding box as an ultimate result.

Boxing

Basically I'm already stuck getting the bounding box of the polygon. I tried Geometry.Boundary, but that just gives back the outer ring...

Finally I could just iterate over the coordinates, but I was wondering if the Geometry or Polygon classes have this capability build in (or if the library has this build in).


Solution

  • This is the fastest way to get the bounding box of a set of NTS geometries:

    var bbox = geoms[0].EnvelopeInternal;
    for (int i = 1; i < geoms.Length; i++)
        bbox.ExpandToInclude(geoms[i].EnvelopeInternal);
    
    // if you need it as a geometry finalize doing
    var bboxGeom = geoms[0].Factory.ToGeometry(bbox);