I am working with a huge amount of data in daily tif files (thousands of daily files). I am analyzing the mean of the raster in a shapefile area, repeated over potentially thousands of shape layers.

My current .tif files are for an entire country when in reality I only need a small area of the of each tif file for each shapefile layer. Each shape layer has a different set of the days to extract from, i.e. data frame like this:

df <- data.frame(shplayer=c("shp_layerbuffer1","shp_layerbuffer2", "shp_layerbuffer3"), start=c("2000_02_18", "2004_03_19", "2010_08_20"), end=c("2010_01_09", "2005_03_15", "2012_09_04"))

Is there a way, in R, to crop a .tif (or any raster type file) BEFORE reading the file? This way I could read just the cropped area of each of the tif files

I envisoned something like (repeating across the entire set of dates):

shp_layerbuffer1 <- st_read("myshpfolder", layer="shp_layerbuffer1", quiet=T)

###EXAMPLE BUT doesn't work to crop the raster as it comes in
tempraster <- raster::raster("mytif_2000_02_18.tif", ext=extent(shp_layerbuffer1))

Then the usual velox (or raster) extract, repeat.

Is this possible?

  • 1
    well, raster() doesn't read everything into memory at first (which is quick) and crop is quite quick - how big are the rasters? look into readGDAL() with the offset = & region.dim = arguments set but this does not easily allow for changing crop areas. Otherwise, for this quantity, use PostGIS and SQL (in R) – Sam Jun 13 '18 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.