PostGIS Quickie

November 10, 2011

(This article was first published on dylan's blog, and kindly contributed to R-bloggers)

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);

read more

To leave a comment for the author, please follow the link and comment on their blog: dylan's blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)