WORK: a folder contains name.csv contains filename in columns, and latitude and longitude in next columns. the same folder contains .tif files. I have written code in r to loop through filename each time and find matching .tif file names in the folder, if the file name from .tif files and name_list.csv file matches then extract values from .tif files in folder using latitude, longitude files from name.csv files.
SYNTAX ERROR: the above mentioned program did not contain any syntax error.
PROBLEM: the result shows extracted values as NA.
REQUEST: I KINDLY REQUEST YOU TO HELP ME SOLVE THE ISSUE AND GET PROPER OUTPUT.
the code as follows:
Load required libraries
library(readr)
library(raster)
library(sf)
#set working directory
setwd("D:\Raju_V\data\ocean bottom\out")
Read the CSV file
data = read_csv("name.csv", show_col_types = FALSE)
Get a list of all .tif files in the folder
tif_list = list.files(path = "D:\Raju_V\data\ocean bottom\out",pattern = ".tif", full.names = FALSE)
Loop through each row in the CSV data
for (i in 1:nrow(data)) {
filename = data$filename[i]
lat = data$latitude[i]
lon = data$longitude[i]
tif = tif_list[i]
ras = raster(tif)
Search for matching .tif file
same_file <- grep(filename,tif_list)
if (same_file > 0) {
ext = extract(ras, cbind(lat,lon), method = 'bilinear')
print(ext)
ext_data = cbind(data, ext)
}
}
write.csv(ext_data, 'result.csv')
print(ext_data)
the link to the data and output of this code is: out.zip - Google Drive