How to get Terrain Characteristics from Coordinate Data Using the Terra Package?

77 views Asked by At

I have a set of coordinate data that I'd like to get terrain characteristic data for (specifically TRI). I know the terrain() function can provide this information, but this function requires a spatraster object as an input with elevation data, and I am struggling trying to figure out how to create the right spatraster object.

The spatraster for the terrain function needs to have elevation as its layer, so I thought I should start by getting elevation values for my coordinates using the elevatR package, however now I'm not sure if this is the right approach.

library(ctmm)
library(dplyr)
library(elevatR)
library(terra)

#Creating example data from built in tracking data from the ctmm package
data<-data("buffalo")
Cilla<-buffalo$Cilla
Cilla<-data.frame(Cilla)
class(Cilla)
lonlat<-Cilla %>% dplyr::select(2,3)

#definining the lonlat projection
prj_dd <- "+proj=longlat +datum=NAD83"

#Pull each points elevation using the ElevatR package
coords_E <- as.data.frame(elevatr::get_elev_point(lonlat, prj = prj_dd, src = "aws"))

#Add an elevation column to our main data frame (elevation in meters)
lonlat$elev <- coords_E$elevation

I think the next step would be to use the rast() function to create a spatraster object, however it doesn't seem to be accepting the dataframe input

raster<-rast(lonlat,type="xyz")

Does anyone know where I'm going wrong here and how I would go about getting TRI values for my coordinate data using the terrain function? Thanks!

1

There are 1 answers

0
Robert Hijmans On

You should start with a raster file with elevation data

library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
tri <- terrain(r, "TRI")

pts <- cbind(c(6,6.1), c(49.6, 49.8))
extract(tri, pts)
#     TRI
#1 14.125
#2 18.750

You can download raster elevation data from different websites. You can also download raster data with R. For example

belev <- geodata::elevation_30s(country="Belgium", path=".")