Skip to content
Prev 19590 / 29559 Next

crop

You can crop SpatialPolygons/Lines/Points* object using gIntersection in
the rgeos package. You cannot crop a "shapefile" without reading it into sp
classes, and re-writing (or overwriting) a new file. (These objects are
*not* 'shapefiles' once they are in R.)

If you just mean to crop with a bounding box, raster gives a
fast/convenient way to generate the object by coercing from an extent():

Here's an untested hint
library(rgdal)
library(raster)
library(rgeos)

## the rgdal part
d <- readOGR("/path/to/shpdir", "shpname")

## the raster part
## define a crop bounds with xmin/xmax/ymin/ymax
epoly <- as(extent(xmin, xmax, ymin, ymax), "SpatialPolygons")

## (assuming your extents really are in d's projection)
proj4string(epoly) <- CRS(proj4string(d))

## the rgeos part
cropped <- gIntersection(d, epoly, byid = TRUE)

Otherwise there are too many options and vagaries of different scenarios to
cover in the abstract, I suggest trying something like this and then come
back with details about what you tried, what you have and expect.

HTH

Cheers, Mike.
On Tue, Oct 22, 2013 at 2:08 AM, Joseph Bechara <jbechara at lri-lb.org> wrote: