Tuesday 3 April 2012

How to get the extent of a PostGIS table

Here are two way to query the spatial extent of a PostgreSQL/PostGIS table.

For a table bc_roads in the bc schema with a geometry column the_geom...

To get the extent as a Well-known text (WKT) box:

SELECT Extent(the_geom) AS extent FROM bc.bc_roads;

To get the extent as xmax, xmin, ymax, ymin as fields:

SELECT ST_XMax(r) AS xmax, ST_XMin(r) AS xmin, ST_YMax(r) AS ymax, ST_YMin(r) AS ymin FROM  (SELECT ST_Collect(the_geom) AS r FROM bc.bc_roads) AS foo;