Wednesday, 3 October 2018

Clip Spatial object to bounding box in R


Given a Spatial object in R, how do I clip all of its elements to lie within a bounding box?


There are two things I'd like to do (ideally I'd know how to do both, but either is an acceptable solution to my current problem--restricting a polygon shapefile to the continental U.S.).




  1. Drop each element not fully within the bounding box. This seems like bbox()<- would be the logical way, but no such method exists.





  2. Do a true clip operation, such that non-infinitesimal elements (e.g. polygons, lines) are cut off at the boundary. sp::bbox lacks an assignment method, so the only way I've come up with would be to use over or gContains/gCrosses in conjunction with a SpatialPolygons object containing a box with the new bounding box's coordinates. Then when clipping a polygon object, you'd have to figure out which are contained vs. cross, and alter the coordinates of those polygons so that they don't exceed the box. Or something like gIntersection. But surely there's a simpler way?




While I know that there are many problems with bounding boxes, and that a spatial overlay to a polygon that defines the region of interest is typically preferable, in many situations, bounding boxes work fine and are simpler.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...