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:


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.

  • What went wrong with the loop?
    – Spacedman
    Oct 10, 2019 at 12:06
  • @Spacedman, is more an error message related to the loop where I put the extract function. " Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"character", "data.frame"’
    – Amber
    Oct 10, 2019 at 15:06
  • 1
    Can you edit your question to show the code with the loop and the error message? I'm guessing but I suspect you are running something like extract(filename,...) rather than extract(raster(filename),...).
    – Spacedman
    Oct 10, 2019 at 15:44
  • @Spacedman, I edited what I did, not sure if the loop itself is wrong the way I did it.
    – Amber
    Oct 11, 2019 at 8:48
  • 1
    That loop works for me, except it doesn't accumulate the results over all the rasters (it only leaves the extracted values for the last raster in extractedData). Try storing each extraction in a list element. extractedData=list() outside the loop and then extractedData[[i]] = ... inside the loop.
    – Spacedman
    Oct 11, 2019 at 10:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.