Question

I found the envelope class in Java's JTS library very handy. An envelope holds the minimal and maximal coordinates of a geometry and is also called bounding box sometimes.

I wanted to get the common envelope of a number of shapely points. In JTS you could call expandToInclude to enlarge the envelope point by point.

As JTS was serving as a blueprint to GEOS / shapely, I was expecting something similar in shapely, but could not find it (I am new to the library though). I know it is no rocket science to do it yourself, but I doubt there is no more elegant way to do this. Do you have any idea?

Was it helpful?

Solution 2

No, there's no envelope class in Shapely, which relies on (minx, miny, maxx, maxy) tuples. If you wanted to program in the same JTS style, it would be trivial to write an envelope class wrapping such a tuple.

Another option:

from shapely.geometry import MultiPoint
print MultiPoint(points).bounds

OTHER TIPS

To create simple box geometries, there is a box function that returns a rectangular polygon:

from shapely.geometry import box
# box(minx, miny, maxx, maxy, ccw=True)
b = box(2, 30, 5, 33)
b.wkt  # POLYGON ((5 30, 5 33, 2 33, 2 30, 5 30))
b.area  # 9.0

For anyone coming here, shapely Polygon has now bounds which I believe is equivalent to JTS envelop. Following is the documentation from official page

from shapely.geometry import Polygon
polygon = Polygon([(0, 0), (1, 1), (1, 0)])
polygon.bounds
(0.0, 0.0, 1.0, 1.0)

Its x-y bounding box is a (minx, miny, maxx, maxy) tuple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top