Please help me fin the reason for no output in R

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

Longitude is along x coordinate and latitude is along y coordinate, therefore you need to swap lat and lon:

ext = extract(ras, cbind(lon, lat), method = 'bilinear')

Otherwise, you are extracting latitudes of -174, which do not exist, hence NA.

Dear,

I really thank you for your great help. I am not a programmer or one who is used to r language either. The program works, but the doubt from my side is this? the output is the same value for all the extractions how can it be. is there any way to check it? please help me.

Here is a modification of your code to give you the required result:

library(readr)
library(raster)
library(sf)

data <- read_csv("name.csv", show_col_types = FALSE)
tif_list <- list.files(path = ".", pattern = ".tif", full.names = FALSE)

ext_data <- c()
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)
  same_file <- grep(filename,tif_list)
  
  if (same_file > 0) {
    ext <- extract(ras, cbind(lon, lat), method = 'bilinear')
    print(ext)
    ext_data <- c(ext_data, ext)
  } else {
    ext_data <- c(ext_data, NA)
  }
}

data$ext_data <- ext_data

write.csv(data, "result.csv")

The result is

   filename       latitude longitude ext_data
   <chr>             <dbl>     <dbl>    <dbl>
 1 01-01-1999.tif    -15.1     -174.     26.4
 2 02-01-1999.tif    -20.3     -174.     31.1
 3 03-01-1999.tif    -18.5     -174.     19.4
 4 04-01-1999.tif    -15.2     -172.     54.1
 5 05-01-1999.tif    -20.5     -174.     42.3
 6 06-01-1999.tif    -20.3     -174.     34.0
 7 07-01-1999.tif    -16.7     -173.     13.4
 8 08-01-1999.tif    -21.3     -174.     54.2
 9 09-01-1999.tif    -18.8     -174.     47.1
10 10-01-1999.tif    -14.9     -173.     39.3

Dear,

Once again I thank you humbly for your time and concern for me. Let me show you my experience with the same problem without for-loop. How the data extraction from with and without for-loop is the same.

pardon me. I am sorry if I am annoying. below is the output from my code.

library(raster)

#set the working directory
setwd("D:\\Raju_V\\data\\ocean bottom\\out")

# Load the CSV file containing latitude and longitude
csv_data = read.csv("name_list.csv")

# Load the raster (GeoTIFF) file
ras = "01-01-1999.tif"
raster_data <- raster(ras)

# Extract values from the raster using latitude and longitude coordinates
extracted_values <- extract(raster_data, cbind(csv_data$longitude, csv_data$latitude), method='bilinear')

# Combine the original CSV data with the extracted values
extracted_data <- cbind(csv_data, extracted_values)

# Print the extracted data
print(extracted_data)

the output is

 print(extracted_data)
     filename latitude longitude extracted_values
1  01-01-1999  -15.098  -173.809         26.43355
2  02-01-1999  -20.295  -174.058         31.09247
3  03-01-1999  -18.502  -174.067         19.34565
4  04-01-1999  -15.203  -172.178         54.10612
5  05-01-1999  -20.480  -173.988         42.27061
6  06-01-1999  -20.345  -174.040         34.02535
7  07-01-1999  -16.666  -173.237         13.44261
8  08-01-1999  -21.260  -173.929         54.19917
9  09-01-1999  -18.768  -173.520         47.08451
10 10-01-1999  -14.947  -173.177         39.31665
> 

i have used only one image and all the coordinates from name.csv file

Dear [Marek Gierlinski],

I thank you once again humbly and sorry for posting my experience. I checked with the data, and the code you provided worked well to solve the problem. the similarity confusion arose from my understanding because of the rounded-off values provided by you as an output.

great job sir, may God bless you.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.