16

Is there an easy way to get the bounding box for an entire table in PostGIS?

23

ST_Extent should do the trick.

ST_Extent — an aggregate function that returns the bounding box that bounds rows of geometries.

Applied like this:

SELECT ST_Extent(the_geom) as table_extent FROM your_table;
9

As @underdark post, st_extent will do the job, but be aware that it will not return a geometry. If you need a geometry type you should use something like

SELECT ST_SetSRID(ST_Extent(the_geom),THE_SRID) as table_extent FROM your_table;

Also if what you need is get the bounding box of each of the rows i think that you can use this sentence:

SELECT ST_SetSRID(ST_Extent(the_geom),THE_SRID) as table_extent FROM your_table GROUP BY gid;

Assuming that gid is the primary key of the table

3

Another possibility is to use the ST_Envelope function, which returns a geometry with SRID,

ST_Envelope — Returns a geometry representing the bounding box of the supplied geometry

, along with the aggregate function ST_Union to obtain the union of all geometries (or the union of their respective envelopes) as follows:

SELECT ST_Envelope(ST_Union(geom)) AS table_extent FROM your_table

or

SELECT ST_Envelope(ST_Union(ST_Envelope(geom))) AS table_extent FROM your_table

The second option should be faster as it simplifies the union operation by using the envelopes of individual geometries.

see Source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.