Skip to content

Suggested Code Improvement in Computing elevation profile using an elevation raster (Section 6.3) #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Aditya-Dahiya opened this issue Apr 15, 2025 · 1 comment

Comments

@Aditya-Dahiya
Copy link

Aditya-Dahiya commented Apr 15, 2025

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.

@Robinlovelace
Copy link
Collaborator

I like this suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants