
#close(mypb) #not sure why but closing the pb while operating causes it to return an empty final dataset.

Reproducible Example: library(maptools) # For wrld_simplĭata(wrld_simpl) #polygon of world countriesīound <- wrld_simpl #name it this to subset to 25 countries and because my loop is set up with that variableĬ <- raster(nrow=2e3, ncol=2e3, crs=proj4string(wrld_simpl), xmn=-180, xmx=180, ymn=-90, ymx=90)įastest Method so far result <- ame() #empty result dataframeįor (i in 1:nrow(bound)) ) #trycatch error so it doesn't kill the loop
#Croping a raster in r how to#
I looked into those, but have had trouble with gdal in the past and don't know it well enough to know how to implement it. The only thing I've found related to this is this response by Roger Bivand who suggested using GDAL.open() and GDAL.close() as well as getRasterTable() and getRasterData(). Nonetheless, it's pretty slow, and I'm wondering if anyone has any suggestions for improving the efficiency and speed of my code. I've found that the extract function works much faster if I iterate through each individual polygon and crop then mask the raster down to the size of the particular polygon. Here's a very simple plot of the results: plot(elevation.I'm extracting the area and percent cover of different land use types from a raster based on several thousand polygon boundaries. elevation.sub <- mask(elevation.sub, state.sub) # Crop elevation data by extent of state subsetĮlevation.sub <- crop(elevation, extent(state.sub))Īs a final step, you need to identify those pixels of your elevation raster that lie within the borders of the given state polygons. elevation <- raster("/path/to/data/alt.bil") Next, import the raster data using raster() and crop it with the extent of the previously generated states subset. "Maryland", "Delaware", "Virginia", "West Virginia") "Rhode Island","New York","Pennsylvania", "New Jersey", Nestates <- c("Maine", "Vermont", "Massachusetts", "New Hampshire" ,"Connecticut", Pay attention to the use of capital and small initial letters! state <- readOGR(dsn = path.data, layer = "usa_state_shapefile") After setting the shapefile's CRS, I create a subset containing the desired states.

Let's import the US shapefile now using readOGR(). You need to load rgdal and raster libraries in order to proceed.
#Croping a raster in r zip#
zip data and decompress it to your working directory. I tested it with altitude and mean temperature data from the WorldClim website (I limit this example to altitude, temperature works similar), and an appropriate shapefile of the US containing state borders is to be found here. Here is an approach using extract() from the raster package. The elevation and meantemp objects are the ones that need to be clipped to the area extent of the nestates object. Map(database="state", regions=nestates, interior=TRUE, add=TRUE, fill=TRUE, col="grey") Map(database="state", interior=T, add=TRUE, fill=FALSE) The numbers are proportional distances within the graphics window (xmin,xmax,ymin,ymax) on a scale of 0 to 1. # Next, we create a new graphics space in the lower-right hand corner.

Map(database="state", regions = nestates, interior=T, lwd=2) "maryland", "delaware", "virginia", "west virginia") "rhode island","new york","pennsylvania", "new jersey", Nestates<- c("maine", "vermont", "massachusetts", "new hampshire" ,"connecticut", This is what I have so far: #load libraries I have two rasters from which give me these variables but I need to clip them to the extent of the states I am interested in.

The map background needs to be either an altitude map or a mean annual temperature map. I am building a map for the northeastern U.S.A.
