0

I have a folder with many csv files. Each file has several columns as well as lat and long columns. Another folder have many rasters in tif format. The .csv files are named based on Julian date (e.g. 251.csv), and so the rasters (e.g. 251.tif). I would like to be able to add the raster value to the csv with matching name and save to a new csv in R. What I want to achieve is this:

raster<-raster("c:/temp/TIFF/2001/273.tif")
points<-read.csv("c:/temp/csv/2001/273.csv")
coordinates(points)=~long+lat
rasValue=extract(raster,points)
combinePointValue <- cbind(points,rasValue)
head(combinePointValue)
library(spdplyr)
combinePointValue <- combinePointValue %>% 
  rename(chloro = 10)
write.table(combinePointValue,file="c:/temp/2001/chloro/273_chloro.csv",append=FALSE, 
sep=",",row.names=FALSE, col.names=TRUE)

Considering the many csv and many tif files, I would prefer avoiding having to type this over and over. Anyone able to help? Many thanks in advance! Ilaria

Ilaria
  • 11
  • 2

2 Answers2

0

It is better to provide a minimal reproducible example since your code can not run without your specific data. However, if I understand well, you can try something like this. Since csv and tif files have the same name, you can sort them and loop through the file index. You can use the original path of csv files to save a new file just by pasting the suffix "_chloro:

library(spdplyr)
csv <- sort(list.files("c:/temp/csv/2001/",full.names = T))
tif <- sort(list.files("c:/temp/TIFF/2001/",full.names = T))

lapply(1:length(csv),function(i){
  raster<-raster(tif[i])
  points<-read.csv(csv[i])
  coordinates(points)=~long+lat
  rasValue=extract(raster,points)
  combinePointValue <- cbind(points,rasValue)
  head(combinePointValue)
  combinePointValue <- combinePointValue %>% 
    rename(chloro = 10)
  write.table(combinePointValue,file=paste0(tools::file_path_sans_ext(csv[i]),"_chloro.csv"),append=FALSE, 
              sep=",",row.names=FALSE, col.names=TRUE)
}) 
Elia
  • 2,210
  • 1
  • 6
  • 18
  • Thank you, Elia, it seems to work, but having now a problem with the tif files. When I run the code without the looping that you provided and call up file by file, the code works. When running your loop, it says that the tif files do not exist! Will try to understand what is happening, but thank you for your help! – Ilaria Jun 21 '22 at 06:22
  • this is really strange since this error could be caused just by the `raster<-raster(tif[i])` line. Try to check if the file path of tif files is correct – Elia Jun 22 '22 at 09:05
  • Hi Elia, the path is correct! My R proficiency is pretty basic..trying to learn. Any chance I can share with you a few of the .tif and corresponding .csv for you to see where the problem could be? Very grateful for any help! – Ilaria Jun 23 '22 at 10:51
  • you can put some of your data in a google drive folder and share the link for downloading it. Glad to help you – Elia Jun 23 '22 at 11:09
0

SInce the R spatial "ecosystem" is undergoing dramatic changes over the past few years, and package like sp and raster will be deprecated, you might consider a solution based on the terra package. It would go something like:

# Not tested!    
library(terra)
csv_path = "c:/temp/csv/2001/"
tif_path = "c:/temp/TIFF/2001/"
tif_list = list.files(file.path(tif_path, pattern = "*.tif", full.names = FALSE)
result_list = lapply(1:length(tif_list), function(i) {
  tif_file = file.path(tif_path, tif_list[i])
  # Do not assume that the list of files are exactly equivalent.
  # Instead create CSV file name from tif file
  csv_name = gsub("tif", "csv", tif_file)
  csv_file = file.path(csv_path, csv_name)
  r = rast(tif_file)
  csv_df = read.csv(csv_file)
  # Assume csv long/lat are the same CRS as the tif files
  pts = vect(csv_df, geom=c("long", "lat"), crs=st_crs(tif)) 
  result = extract(r, pts, xy = TRUE)
  new_csv = paste0(tools::file_path_sans_ext(csv_file),"_chloro.csv")
  write.csv(result, file.path(csv_path, new_csv))
  return(result)
  })
Micha
  • 403
  • 2
  • 13
  • Hi Micha, thank you for trying to help. I cannot get it to work! How can I share some of the .csv and respective .tif with you so that you can test the script? Many thanks, Ilaria – Ilaria Jun 23 '22 at 10:49
  • First can you post what did not work? What error did you get? – Micha Jun 23 '22 at 15:16