I've got a collection of geometry objects. Now i want to calculate the minimal bounding rectangle from the whole collection. i'm using the java topology suite, but I can't figure out how to do this?
3 Answers
Have a look in http://tsusiatsoftware.net/jts/javadoc/index.html
If I assume you are using a GeometryCollection instance. If it's true, you can directly call
geometry.getEnvelope();
or
geometry.getEnvelopeInternal();
If you want an Envelope instance
It will return you the minimum rectangle of the GeometryCollection.
If you have a collection of Geometries, you can use an envelope directly, and expand it each time you process a new geometryc of your collection.
Envelope env = new Envelope();
for(Geometry g : mySet){
env.expandToInclude(g.getEnvelopeInternal()):
}
or
Envelope env = new Envelope();
for(Geometry g : mySet){
env.expandToInclude(g.getBoundary().getEnvelopeInternal()):
}

- 1,525
- 9
- 18
-
`getEnvelopeInternal()` will ignore the precision model of your geometry. – Campa Jun 27 '19 at 16:29
I just put one together like this.
The Geometry class has a 'getEnvelopeInternal()' which returns the inscribed envelope, but the 'getEnvelope()' just returns another Geometry.
Looking at the javadoc, it appears that the returned Geometry object is either:
- An empty point matching an empty Geometry object.
- A single Point, matching the passed in point.
- A Polygon with 4 coordinates which specifies the enclosing Envelope.
Looking at other notes on Envelope, I see that you can 'expand' the envelope....so here's the static util that I built to convert:
public static Envelope enclosingEnvelopFromGeometry(Geometry geometry) {
final Envelope envelope = new Envelope();
final Geometry enclosingGeometry = geometry.getEnvelope();
final Coordinate[] enclosingCoordinates = enclosingGeometry.getCoordinates();
for (Coordinate c : enclosingCoordinates) {
envelope.expandToInclude(c);
}
return envelope;
}

- 859
- 1
- 8
- 19
I have never used jts, but googled this:
Iterate through collection and for each object call getBoundary().getEnvelopeInternal()

- 3,639
- 1
- 18
- 23
-
-
In official documentation: http://www.vividsolutions.com/jts/JTSHome.htm, Geometry and Envelope classes. – red1ynx Dec 15 '11 at 13:26
-
It's not the official documentation. vividsolution is not the maintainer of the last JTS version. – Agemen Dec 15 '11 at 13:43
-
Beware that `getEnvelopeInternal()` will ignore the precision model of your geometry. – Campa Jun 27 '19 at 16:28