I have many raster data in tiff format. I need to extract the values for some coordinates and save it as a csv file. I have a code to extract data from some stations across the raster flies, and the code works as long as I extract just one station. Once, I put other coordinates to extract their values it gives errors. so I have to run the code for each of stations separately (I have 8 stations).
Can you help me modify the code?
library(raster)
library(sp) # used to create a SpatialPoint object
setwd("c:/PET")
# LOAD RASTERS INTO A LIST OBJECT.
tmp <- lapply(list.files("c:/PET", pattern = ".tif$", full.names = TRUE), raster)
coords <- data.frame(
# **1st station**
"lat" = c(498957.72),"lon" = c(4516020.57)
)
pts <- SpatialPoints(coords = coords,
proj4string = CRS("+proj=utm +zone=16 +datum=WGS84 +units=m"))
outt=as.data.frame(cbind(coords, do.call("rbind", lapply(tmp, extract, pts))))
write.csv(outt, file="myFileName.csv", row.names = FALSE)