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

2.4k views Asked by At

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).

2

There are 2 answers

1
FObermaier On BEST ANSWER

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);
2
alex On

as a simple polygon is a sequence of points, the bounding box of the polygon is a rectangle (itself a polygon) comprised between the [minimumX, minimumY] at southwest, and [maximumX, maximumY] at northeast. minimumX is the minimum of the X of all the points in all the polygons, and respectively minimumY, maximumX, maximumY.

so the extent rectangle should be:

extent = [
    [minimumX, minimumY],
    [maximumX, minimumY],
    [maximumX, maximumY],
    [minimumX, maximumY]
];

even though polygons are not necesarily simple, as they may contain several simple polygons, the calculation should be the same.