Reputation: 686
I am wanting to clip a large shapefile (67MB) in program R and derive a much smaller raster from around ~5% of it. Once loaded the shapefile has 221388 features and 5 fields - and explodes to 746 MB.
My difficulty comes when trying to clip the file to a workable size - the program crashes after a few minutes. I have tried both crop (from raster) and gIntersection (from rgeos) without success. I have 8GB of RAM - clearly there is a memory issue.
I am guessing there maybe a work around. I know that there are some big memory packages out there - but can any of them help in my kind of situation? My current code is below:
# dataset can be found at
# http://data.fao.org/map?entryId=271096b2-8a12-4050-9ff2-27c0fec16c8f
# location of files
ogrListLayers("C:/Users/Me/Documents/PNG Glob")
# import shapefile
ogrDrivers()[10,]
# shapefiles
Glob<-readOGR("C:/Users/Me/Documents/PNG Glob", layer="png_gc_adg_1")
# assign projection
Glob@proj4string<- CRS("+proj=longlat")
#object size
object.size(Glob)
# clipping
crop(Glob, extent(c(144,146,-7,-5)))
Upvotes: 3
Views: 1546
Reputation: 27388
As suggested by @Pascal, GDAL's ogr2ogr
is useful for this. You can call it from R with system
as follows (including on Windows), though this assumes that (1) you have a working GDAL installation and (2) the path to the GDAL binaries exists in your PATH environment variable:
Download and unzip the PNG shapefile:
download.file('http://www.fao.org/geonetwork/srv/en/resources.get?id=37162&fname=png_gc_adg.zip&access=private',
f <- tempfile(fileext='.zip'))
unzip(f, exdir=tempdir())
Call ogr2ogr
with system
from R to clip the PNG shapefile and save the resulting .shp to the working directory:
system(sprintf('ogr2ogr -clipsrc 144 -7 146 -5 png_clip.shp %s',
file.path(tempdir(), 'png_gc_adg_1.shp')))
On my system this took around 70 seconds, and memory usage didn't seem to increase by more than about 100MB. (I did get a lot of warnings of the likes of Warning 1: Value 138717513240 of field AREA of feature 0 not successfully written. Possibly due to too larger number with respect to field width
- not sure what that's about.)
Load the clipped shapefile:
library(rgdal)
p <- readOGR('.', 'png_clip')
plot(p)
Upvotes: 1