9

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?

ABLX
  • 716
  • 2
  • 7
  • 18

3 Answers3

11

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()):
}
Agemen
  • 1,525
  • 9
  • 18
2

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:

  1. An empty point matching an empty Geometry object.
  2. A single Point, matching the passed in point.
  3. 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;
} 
Dan Devine
  • 859
  • 1
  • 8
  • 19
1

I have never used jts, but googled this:

Iterate through collection and for each object call getBoundary().getEnvelopeInternal()

red1ynx
  • 3,639
  • 1
  • 18
  • 23