-
-
Notifications
You must be signed in to change notification settings - Fork 608
Description
In Section 6.3: Raster Extraction, the code provided in the text - using st_distance() - computes the distance from the first point to every other point using the crow’s flight (Euclidean) distance. While this works for straight-line transects, it does not account for non-linear paths commonly encountered in real-world scenarios such as hiking trails. This results in inaccurate distance values when generating elevation profiles.
A more accurate approach involves computing the cumulative distance along the path itself. In the proposed code below, we extract the coordinates of each point and then compute the Euclidean distance between consecutive points. Taking the cumulative sum of these segment distances gives the total distance from the starting point along the actual path, whether it is straight or winding. This results in a more realistic and useful elevation profile.
Original Code (Straight-Line Assumption):
zion_transect = zion_transect |>
group_by(id) |>
mutate(dist = st_distance(geometry)[, 1])My Proposed Code (Cumulative Distance Along Path):
# Obtain coordinates of each point
zion_transect_coordinates <- zion_transect |>
group_by(id) |>
st_coordinates()
# Compute the cumulative distance from the starting point along the path
point_distances <- c(
0,
cumsum(sqrt(rowSums(diff(zion_transect_coordinates)^2)))
)
zion_transect <- zion_transect |>
mutate(dist = point_distances)This enhancement ensures that the distance reflects the true travel path and thus improves the elevation profiling workflow. A real-life example of this approach is on my webpage Getting Elevation Profile of the UTMB Ultra-marathon.