Minimum bounding box with JTS

I have a collection of geometry objects. Now I want to calculate the minimum bounding rectangle from the entire collection. I am using java topology set, but I can not figure out how to do this?

+4
source share
3 answers

Take a look at http://tsusiatsoftware.net/jts/javadoc/index.html

If I assume that you are using an instance of GeometryCollection. If true, you can directly call

geometry.getEnvelope(); 

or

 geometry.getEnvelopeInternal(); 

If you need an instance of Envelope

It will return you the minimum GeometryCollection rectangle.

If you have a collection of geometries, you can use the envelope directly and expand it each time you process the new geometry 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()): } 
+4
source

I have never used jts, but googled this:

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

+1
source

I just put it like that.

In the Geometry class, there is "getEnvelopeInternal ()" that returns an inscribed shell, but "getEnvelope ()" just returns a different geometry.

Looking at javadoc, it seems that the returned Geometry object:

  • An empty point corresponding to an empty Geometry object.
  • One point corresponding to the passed point.
  • A 4-coordinate polygon that defines a shell.

Looking at other notes on the envelope, I see that you can "expand" the envelope ... so here is the static utility that I built for conversion:

 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; } 
0
source

Source: https://habr.com/ru/post/1386515/


All Articles