PostGIS Quickie

Submitted by dylan on Fri, 2011-11-11 00:30.

Today I needed to cut out a rectangle of geologic data from a state-wide map in an AEA coordinate system, using a bounding box from a UTM zone 10 region, with the output saved in UTM zone 10 coordinates. PostGIS makes this type of operation very simple via spatial SQL. Note the special syntax that PostgreSQL uses for referencing variables.

Spatial SQL Example

-- set our query geometry as a variable for use later
\SET query_geom ST_Transform(SetSRID(ST_MakeBox2d(ST_Point(675461, 4168366), ST_Point(749990, 4260786)), 26910), 9001)

-- save the result of our query to a temp table for export
SELECT ST_Transform(ST_Intersection(wkb_geometry, :query_geom), 26910) AS wkb_geometry, ptype, ptype_full, gen_type, note
FROM cageo
JOIN cageo_legend USING (ptype)
WHERE ST_Intersects(wkb_geometry, :query_geom);