[R-sig-Geo] clipping SpatialPolygonsDataFrames
Anthony Fischbach
afischbach at usgs.gov
Tue Dec 10 21:18:49 CET 2013
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 at 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.
More information about the R-sig-Geo
mailing list