Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

shapely

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

like image 487
nickponline Avatar asked Oct 31 '25 08:10

nickponline


1 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

like image 84
CodeMonkey Avatar answered Nov 02 '25 23:11

CodeMonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!