library(terra)
library(sf)
WebDAV: R - Get XY info
Get SoilGrids layers values for a list of points
Load libraries
Set variables of interest
We define the variables for the soil property and layer of interest. See here for the naming conventions
= "soc" # variable of interest
voi = "0-5cm"
depth = "mean"
layer
= paste(voi,depth,layer, sep="_") # layer of interest voi_layer
We set other variables necessary for the WCS call for all kinds of requests
= '/vsicurl?max_retry=3&retry_delay=1&list_dir=no&url=https://files.isric.org/soilgrids/latest/data/' webdav_path
Read in point data and re-project to Homolosine
=read.csv("my_points_data.csv")
data<- data.frame(
data id = 1:5, # Unique IDs
longitude = c(34.78, 35.12, 34.95, 35.05, 34.88),
latitude = c(-1.29, -1.31, -1.27, -1.30, -1.28),
value = runif(5, 10, 50) # Some random values
)=st_as_sf(data,coords = c("longitude", "latitude"), crs = 4326)
spdata
='+proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs'
igh=st_transform(spdata, igh)
spdata_igh
=data.frame(st_coordinates(spdata_igh),id=spdata_igh$id) data_igh
Use gdallocationinfo to get the values from the pixels
=function(rowPX,data,VOI,VOI_LYR){
fun_pixel_valuesas.numeric(
gdallocationinfo(
srcfile=paste0(webdav_path,"/",VOI,"/", VOI_LYR,".vrt"),
x=data[rowPX,"X"],
y=data[rowPX,"Y"],
geoloc=TRUE,
valonly=TRUE))
}
=unlist(lapply(1:3,function(x){fun_pixel_values(x,data_igh,voi,voi_layer)})) value_pixels
The value_pixels
vector can be binded with data_igh
paying attention to order. gdallocationinfo
can also return more information, including the coordinates themselves for a safer workflow.