[postgis-users] MBR from a collection of points?

Kevin Neufeld kneufeld at refractions.net
Mon Jan 25 08:24:33 PST 2010


ST_Collect and ST_Envelope may help you here.

create temp table foo (grp_id int, geom geometry);
insert into foo values (1, 'POINT(0 0)');
insert into foo values (1, 'POINT(0 1)');
insert into foo values (1, 'POINT(1 1)');
insert into foo values (1, 'POINT(1 0)');
insert into foo values (2, 'POINT(3 0)');
insert into foo values (2, 'POINT(3 1)');
insert into foo values (2, 'POINT(4 1)');
insert into foo values (2, 'POINT(4 0)');

select grp_id, st_astext(st_envelope(st_collect(geom)))
from foo
group by grp_id;
  grp_id |           st_astext
--------+--------------------------------
       1 | POLYGON((0 0,0 1,1 1,1 0,0 0))
       2 | POLYGON((3 0,3 1,4 1,4 0,3 0))
(2 rows)

-- Kevin


On 1/25/2010 8:09 AM, Robert Hicks wrote:
> Hey all, I have a collection of points stored in Postgres and I want
> to extract the geom from each row and aggregate each one together to
> form the minimum bounding rectangle. Is there a stored procedure for
> this? Or at least one I could loop over?
>
> Thanks!
>



More information about the postgis-users mailing list