1

I have more than 50 raster files (ASCII format) that I need to crop. I already exported the mask from ArcMap in ASCII format as well and loaded it into R. How can I make it work for all rasters in a row and export them with the same name as before (of course in a different folder to not overwrite)?

I know there is a crop function in the raster package, but I never used it so far. I only stacked them for further habitat analysis.

My Code so far:

#### Use only part of area
files2 <- list.files(path="D:/",full.names=TRUE, pattern = "\\.asc$")
files2
# Create a RasterLayer from first file
mask_raster <- raster(files2[1])
# Crop. But how??   
crop(x = , y=mask_raster)
writeRaster(...)`
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Canna
  • 113
  • 6
  • Does this help? https://stackoverflow.com/questions/23073669/clipping-raster-using-shapefile-in-r-but-keeping-the-geometry-of-the-shapefile – Michael Harper Jul 30 '18 at 09:32
  • Thank you Mikey. But not really. I can't wrap my head around this. I have my list of ASCII rasters I need to crop ("files2") and I have a mask to crop these rasters by ("mask_raster"). I guess a loop would be necessary to go through the rasters, cut them, export this to a folder with the name of the original file, repeat. Right? – Canna Jul 30 '18 at 11:19
  • Have you come up with code which can crop a single raster? Focus on developing code to do this before worrying about how you can run this across multiple rasters. If you do write some code to crop a single raster, you should add it to the question. Hopefully I have time to answer this properly later today but these details should make it easier to solve :) – Michael Harper Jul 30 '18 at 12:28
  • I found something online that I was able to change for my needs. It uses a shapefile to cut out the raster. Since I didnt get it to work with a raster, I converted my raster mask file into a shapefile. Here you find the code if interested: http://r-sig-geo.2731867.n2.nabble.com/Crop-a-raster-using-a-shapefile-td7580091.html Furter down, by Mauricio. Thanks anyway Mikey – Canna Jul 30 '18 at 16:41
  • For completion it is good posting your solution to the problem in case others encounter the same problem :) – Michael Harper Jul 30 '18 at 17:38
  • I thought I did in my last comment. But ok, I will post an answer. Probably easier to find for people looking for the same. Thank you. – Canna Jul 31 '18 at 07:56

1 Answers1

0

I did not find an easy solution to crop multiple rasters by a raster, but by a shape file. So I just went back to ArcMap and converted the raster into a shapefile. Then in R, crop and mask were the crucial steps. See code below (modified from Mauricio Zambrano-Bigiarini's code). Hope this helps.

# Reading the shapefile (mask to crop later)
Maskshp <- readOGR("mask.shp")

# Getting the spatial extent of the shapefile
e <- extent(Maskshp)

# for loop with (in this case) 60 runs
for (i in 1:60) {

# Reading the raster to crop
files <- list.files(path="...your path",full.names=TRUE, pattern = "\\.asc$")
Env_raster <- raster(files[i])
# Save the filename
filename <- (paste(basename(files[i]), sep=""))

# Crop the raster
Env_raster.crop <- crop(Env_raster, e, snap="out")

# Dummy raster with a spatial extension equal to the cropped raster,
# but full of NA values
crop <- setValues(Env_raster.crop, NA)

#  Rasterize the catchment boundaries, with NA outside the catchment boundaries
Maskshp.r <- rasterize(Maskshp, crop)

# Putting NA values in all the raster cells outside the shapefile boundaries
Maskshp.masked <- mask(x=Env_raster.crop, mask=Maskshp.r) 
plot(Maskshp.masked)

#Export file to working directory with original name as new name
writeRaster(Maskshp.masked, filename)
}
Canna
  • 113
  • 6