-2

Work: a folder contains name.csv, this file 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.

Problem: the result shows extracted values as NA.

# 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: https://drive.google.com/file/d/1jtcDbtw8LqLPFuKRnk2M8dliow8231V1/view?usp=sharing

Mark
  • 7,785
  • 2
  • 14
  • 34
G T
  • 1
  • 2
    Raju_V, what is the actual problem? The title needs to be more descriptive than "Please help me I don't get the right result" – Mark Aug 21 '23 at 09:26
  • 1
    you need to locate the `extract`ion point with `cbind(longitude, latitude)` *in that order* – I_O Aug 21 '23 at 11:09

1 Answers1

0

As noted in comments, it's the lat/lon order.
And there's not much need for loops or conditionals.
One approach for extracting values using terra, which is also more explicit in documentation about location parameter: /../ The locations can be a SpatVector (points, lines, polygons), a matrix with (x, y) or (longitude, latitude – in that order!) coordinates /../ :

# prepare reprex ----------------------------------------------------------
zip_url <- "https://drive.google.com/u/0/uc?id=1jtcDbtw8LqLPFuKRnk2M8dliow8231V1&export=download"
tmp_dir <- tempdir()
archive::archive_extract(zip_url, dir = tmp_dir)
library(terra)
library(dplyr)

out_path <- file.path(tmp_dir, "out")
(data <- readr::read_csv(file.path(out_path, "name.csv"), show_col_types = FALSE))
#> # A tibble: 10 × 3
#>    filename       latitude longitude
#>    <chr>             <dbl>     <dbl>
#>  1 01-01-1999.tif    -15.1     -174.
#>  2 02-01-1999.tif    -20.3     -174.
#>  3 03-01-1999.tif    -18.5     -174.
#>  4 04-01-1999.tif    -15.2     -172.
#>  5 05-01-1999.tif    -20.5     -174.
#>  6 06-01-1999.tif    -20.3     -174.
#>  7 07-01-1999.tif    -16.7     -173.
#>  8 08-01-1999.tif    -21.3     -174.
#>  9 09-01-1999.tif    -18.8     -174.
#> 10 10-01-1999.tif    -14.9     -173.

(tif.list <- list.files(out_path, "tif$"))
#>  [1] "01-01-1999.tif" "02-01-1999.tif" "03-01-1999.tif" "04-01-1999.tif"
#>  [5] "05-01-1999.tif" "06-01-1999.tif" "07-01-1999.tif" "08-01-1999.tif"
#>  [9] "09-01-1999.tif" "10-01-1999.tif"

data |>
  filter(filename %in% tif.list) |>
  rowwise() |>
  mutate(extract = rast(file.path(out_path, filename)) |> 
                   extract(pick(longitude, latitude), method = "bilinear", ID = FALSE) |> 
                   unlist()) |>
  ungroup()
#> # A tibble: 10 × 4
#>    filename       latitude longitude extract
#>    <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

Created on 2023-08-21 with reprex v2.0.2

margusl
  • 7,804
  • 2
  • 16
  • 20