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

2

9

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

Roxyroy answered 7/12, 2020 at 15:50 Comment(0)
L
8

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);
Lavernalaverne answered 8/12, 2020 at 7:28 Comment(1)
@FObermair Yes, that's it - thanks! And for me, the 'bbox' Envelope is enough.Roxyroy
G
0

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.

Geostrophic answered 18/3 at 14:44 Comment(2)
I admit that the term 'bounding box' isn't exactly correct regarding the first part of the answer where the total containing Envelope is calculated. But isn't the ToGeometry eventually creating the bbox with the points you suggested?Roxyroy
indeed it probably does. i just explained the idea behind the calculation. it may give you the bounding box of one or any number of geometric elements.Geostrophic

© 2022 - 2024 — McMap. All rights reserved.