How do I find the smallest surrounding rectangle of a set of 2D points in Shapely?

Question:

How do I find the msmallest surrounding rectangle (which is possibly rotated) of a set of 2D points in Shapely?

Asked By: nickponline

||

Answers:

To create the smallest surrounding rectangle in Shapely, first construct a MultiPoint from a sequence of points then use the minimum_rotated_rectangle property (which is in the BaseGeometry class).

from shapely.geometry import MultiPoint, Polygon

points = [(0, 0), (2, 2), (10, 4), (5, 5), (8, 8)]

# create a minimum rotated rectangle containing all the points
polygon = MultiPoint(points).minimum_rotated_rectangle
print(polygon)

Output:

POLYGON ((2.9999999999999996 -2.9999999999999996, 10.999999999999998 4.999999999999998, ...

The example set of points are displayed below in red and the bounding box is in blue.

MBR plot

If want to create an envelope around the points that is the smallest rectangle (with sides parallel to the coordinate axes) containing all the points then call the envelope property on the object.

polygon = MultiPoint(points).envelope

envelope

Answered By: CodeMonkey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.