PostGISGeometry Processing

ST_OrientedEnvelope

What is ST_OrientedEnvelope?

ST_OrientedEnvelope is a PostGIS function that returns the minimum-area rectangle enclosing the input geometry. Unlike ST_Envelope, which is always axis-aligned, the oriented envelope can rotate freely to produce the tightest possible bounding box.

SQL
ST_OrientedEnvelope(geometry geom)geometry

The result is a closed Polygon with four corners (plus the closing vertex). For degenerate inputs (single point, collinear points) the function returns a lower-dimensional geometry.

When would you use ST_OrientedEnvelope?

Use ST_OrientedEnvelope when you need a tight rectangular hull for elongated or rotated features — approximating a building footprint, fitting a rectangle to a parcel, or deriving the primary axis of a geometry. It is a common first step in computing feature orientation or for rectangular simplification of irregular shapes.

SQL
1SELECT building_id, ST_OrientedEnvelope(geom) AS fit_rect
2FROM buildings
3WHERE ST_Area(geom) > 1000;

FAQs

How is this different from ST_Envelope?

ST_Envelope returns the axis-aligned bounding box — always a rectangle with horizontal and vertical sides. ST_OrientedEnvelope finds the rotated rectangle that minimises area, which is often much tighter for diagonally-aligned shapes. Use ST_Envelope for indexing and quick filters; use ST_OrientedEnvelope for shape analysis.

Can I get the rectangle's orientation angle?

Not directly. You can extract it from the result by computing the angle of the longest edge with ST_Azimuth between the first two vertices of the returned polygon.

How does it handle a single point?

A single point or coincident points returns a Point. Two distinct points return a LineString. The function returns a rectangle only when the input spans a non-zero area.

Is the rectangle unique?

For generic input it is unique up to the choice of two opposite sides to call "length". For very symmetric inputs (a square, a regular polygon) multiple rectangles could equally minimise area — PostGIS picks one deterministically.