Fork me on GitHub

ST_MinimumRectangle

Signature

GEOMETRY ST_MinimumRectangle(GEOMETRY geom);

Description

Returns the minimum rectangle enclosing geom.

Examples

SELECT ST_MinimumRectangle('MULTIPOINT((8 3), (4 6))');
-- Answer: LINESTRING(8 3, 4 6)

SELECT ST_MinimumRectangle('POLYGON((1 2, 3 0, 5 2, 3 2, 2 3, 1 2))');
-- Answer: POLYGON((1.3999999999999997 3.1999999999999997,
--                  0.6 0.8,
--                  4.2 -0.3999999999999996,
--                  4.999999999999998 1.9999999999999991,
--                  1.3999999999999997 3.1999999999999997))

SELECT ST_MinimumRectangle('LINESTRING(1 2, 2 4, 5 5, 2 1, 6 1)');
-- Answer: POLYGON((6 1, 6 5, 1 5, 1 1, 6 1))

SELECT ST_MinimumRectangle('GEOMETRYCOLLECTION(
                               POLYGON((0 0, 3 -1, 1.5 2, 0 0)),
                               POLYGON((2 0, 3 3, 4 2, 2 0)),
                               POINT(5 6),
                               LINESTRING(1 1, 1 6))');
-- Answer: POLYGON((5 6,
--                  1.2352941176470589 6.9411764705882355,
--                  -0.5294117647058824 -0.1176470588235294,
--                  3.235294117647059 -1.0588235294117647,
--                  5 6))
Comparison with ST_Envelope
CREATE TABLE input_table(geom GEOMETRY);
INSERT INTO input_table VALUES
     ('POLYGON((0 0, 3 -1, 3 1, 2 1, 0 0))'),
     ('POLYGON((2 0, 3 3, 4 2, 2 0))'),
     ('POINT(5 6)'),
     ('LINESTRING(1 1, 1 6)');

SELECT ST_Envelope(geom) ENV, ST_MinimumRectangle(geom) MinRect
      FROM input_table;
-- Answer:
-- |            ENV           |          MinRect            |
-- |--------------------------|-----------------------------|
-- | POLYGON((0 -1, 0 1, 3 1, | POLYGON((3 -1, 3.59 0.80,   |
-- |     3 -1, 0 -1))         |     0.6 1.79, 0 -0, 3 -1))  |
-- | POLYGON((2 0, 2 3, 4 3,  | POLYGON((2 -0, 3.2 -0.40,   |
-- |     4 0, 2 0))           |    4.20 2.59, 3 2.99, 2 0)) |
-- | POINT(5 6)               | POINT(5 6)                  |
-- | LINESTRING(1 1, 1 6)     | LINESTRING(1 1, 1 6)        |

See also