Skip to main content
clarified question
Source Link

I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]glaciers[i]), but how do I pass the iteration in my kind of function?
  What would be the way to introduce the i, so to speak?

I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]), but how do I pass the iteration in my kind of function?
  What would be the way to introduce the i, so to speak?

I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[i]), but how do I pass the iteration in my kind of function? What would be the way to introduce the i, so to speak?

clarified question
Source Link

I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glaciers' extentsglacier's extent. I also have the corresponding glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding positionshp in the dataframeSPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]), but how do I pass the iteration in my kind of function?
What would be the way to introduce the i, so to speak?

I have about 50.000 small glacier rasters (rectangular) that need to be clipped to the glaciers' extents. I also have the corresponding glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # corresponding position in the dataframe
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]), but how do I pass the iteration in my kind of function?
What would be the way to introduce the i, so to speak?

I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My (admittedly rudimentary) code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]), but how do I pass the iteration in my kind of function?
What would be the way to introduce the i, so to speak?

Source Link

Clipping multiple rasters by matching polygons from SPDF in R

I have about 50.000 small glacier rasters (rectangular) that need to be clipped to the glaciers' extents. I also have the corresponding glacier polygons stored in a large SpatialPolygonsDataFrame.

I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapplyfunction, but I am open for other approaches.

My code so far is:

filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)

fun_clip <- function(filelocations, filenames, glaciers){
  r <- raster(filelocations)
  r <- crop(r,glaciers) # corresponding position in the dataframe
  writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}

mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)

How can I give the crop-function the right iterative arguments? filelocationis of the same length as glaciers, so in a for-loopI would use something like r <- crop(r,glaciers[1]), but how do I pass the iteration in my kind of function?
What would be the way to introduce the i, so to speak?