Tony--
rgeos::gIntersection() can do what you want.  I use it often to clip
vegetation map SpatialPolygonsDataFrames by park boundaries, retaining the
attributes of the veg map polygons.

Tips:

byid=c(TRUE,FALSE)
retains the polygon ids of the first SpatialPolygonsDataFrame in the
returned object.  The polygon ids are matched against the dataframe (@data
slot) rownames to associate attributes with polygons.

Think about to what you want to happen when the clipping creates 2 new
polygons from what formerly was a single polygon (e.g., a fat U polygon
with the bottom cut off).  The polygon ID values must be unique; you may
need to specify the id= parameter as well.

If you had area, perimeter, etc., in the @data attribute table, remember
that those values in the clipped object are still the area or whatever of
the original, unclipped version of each polygon.  If you need areas of the
clipped polygons, use gArea.

In general, rasterizing before clipping or masking can be much faster than
gIntersection of thousands of polygons.  If your final product will be
rasterized, at a resolution where you don't need to track what fraction of
a raster cell was inside a polygon, you might do the rasterization earlier
rather than later in the workflow.  Also, if you have a multi-core
computer, several functions in the raster package are trivial to run on
multiple cores.

Good luck!

Tom 2


On Tue, Dec 10, 2013 at 12:18 PM, Anthony Fischbach <afischbach@usgs.gov>wrote:

> I wish to clip SpatialPolygonsDataFrames with a SpatialPolygonsDataFrame
> and
> return a SpatialPolygonsDataFrames with new clipped boundaries and with
> data
> from the source SpatialPolygonsDataFrames.
> [The ArcGIS equivalent is:
>
> arcpy.Identity_analysis("InterestingPolygonsToBeClipped","StudyAreaClippingPolygons",
>
> "resultingPolygons.shp","ALL","#","NO_RELATIONSHIPS")]
>
> Some have suggested that polygon on polygon clipping may best be done by a
> GIS, and have lamented that efforts to implement this within R have
> suffered
> from depenentcies on commercial software (see for example:
>
> http://r-sig-geo.2731867.n2.nabble.com/clipping-polygons-overlaying-big-polygon-on-polygons-td4606656.html
> ).
> Yet, I still wish to be able to do this entirely within R, because I have
> needs to clip a large number  (sample size ~ 1000) of daily sea ice extent
> polygons by study area polygons, and wish to handle the tabulated results
> with the richness of R.
>
> Without the ability to clip SpatialPolygonsDataFrames by a
> SpatialPolygonsDataFrame , I am trying to solve this problem with the
> raster
> package.  I rasterize my stack of sea ice extent polygons , as well as the
> study area polygons.  Then overlay operations are run within the raster
> context.
> ## Code sketch  ##
> ## Dates is a data.frame containing columns for dates, names of sea ice
> shape files
> ## provided by a sea ice agency, and a column for the results.
> ## Study Area is a SpatialPolygonsDataframe defined elsewhere.
> ## First read in raster of appropriate resolution (5km x 5km cells from
> across the study area).
> r<-raster(file='rasterOfAppropriateResolution.tif')
> StudyArea.r<-rasterize(StudyArea, r)  ## rasterize a
> SpatialPolygonsDataFrame of the study area
>                                                          ## StudyArea.r
> cells are 1 in the study area, 0 elsewhere.
>
> for(d 1:nrow(Dates)){  ##Loop through the hundreds of days of sea ice data
>      Ice<-readOGR(dsn='D:/Temp/Ice', layer=Dates[d, 'shapeFile'])  ## Read
> the Ice shape file
>      Ice.r<-rasterize(Ice, StudyArea.r, getCover=TRUE)   ## rasterize sea
> ice SpatialPolygonsDataFrame
>      IceInStudyArea.r<-Ice.r * StudyArea.r  ## raster multiplication
>      Dates[d,'IceInStudyArea']<-(cellStats(IceInStudyArea.r, stat=sum)/100)
> }
>
> It works great, but runs slowly on my workstation.  Is there a better
> approach?
>
>
>
> -----
> Tony Fischbach, Wildlife Biologist
> Walrus Research Program
> Alaska Science Center
> U.S. Geological Survey
> 4210 University Drive
> Anchorage, AK 99508-4650
>
> AFischbach@usgs.gov
> http://alaska.usgs.gov/science/biology/walrus
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/clipping-SpatialPolygonsDataFrames-tp7585333.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

