PostGIS Quickie
[This article was first published on dylan's blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
CREATE TEMP TABLE tt AS
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);
\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
CREATE TEMP TABLE tt AS
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);
To leave a comment for the author, please follow the link and comment on their blog: dylan's blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.