I have 10,000 raster files with different sizes. I need to extract values based on a list of points.
I have used the following using the raster pckg:
library(raster)
library(ncdf4)
library(data.table)
files <-list.files(path='/media/ambra/sea_levelss', pattern='*.grd', recursive=TRUE, full.names=TRUE)
st <- stack(files)
#Testing one file only:
ncfile <- brick('L1.grd', varname = "z")
pts= read.table("/Documents/ambra/sea_levelss/data/Ptos.xy",sep="\t",head=F)
extractedData <- extract(ncfile, pts, method = "bilinear")
#Then for the 10k in loop:
for(i in seq_along(files)) {
extractedData = extract(raster(files[i]), pts, method="bilinear")
}
So far, the latter worked for one file, but not when I need to go through all 10k files. I tried with loop but I failed -no error but goes long and only stores 1 column.
Any ideas on how to create a fast solution to read these rasters?
And merging the 10k and merging those values to my pts files (that contains x,y coordinates).
I have checked for a solution to my problem. Similar problems are around but none has led me to my solution. E.g Extracting values from raster stack and aggregating results using R.
extract(filename,...)
rather thanextract(raster(filename),...)
.extractedData
). Try storing each extraction in a list element.extractedData=list()
outside the loop and thenextractedData[[i]] = ...
inside the loop.