[R-sig-Geo] crop

Michael Sumner mdsumner at gmail.com
Tue Oct 22 00:04:49 CEST 2013


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:

> Can we crop a shapefile using and extent?****
>
> I mean without creating a blank shapefile with the extent.****
>
> ** **
>
> ** **
>
> ** **
>
> *JOSEPH BECHARA*
>
> *Wildfire Management Program Coordinator*
>
> Ashrafieh 1020 Building, 8th floor, Mathaf, Beirut, Lebanon**
>
> Tel.: +961 (1) 426630-1-2****
>
> Fax: +961 (1) 426631****
>
> Mail: jbechara at lri-lb.org <jbejjani at lri-lb.org>****
>
> www.lri-lb.org****
>
> [image: Description: Description: LRI-logo-Email]****
>
> ** **
>
> ** **
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>


-- 
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20131022/3662e2e1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 16370 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20131022/3662e2e1/attachment.jpe>


More information about the R-sig-Geo mailing list