Enhance elevation profile accuracy with cumulative path distance#1153
Open
Aditya-Dahiya wants to merge 3 commits intogeocompx:mainfrom
Open
Enhance elevation profile accuracy with cumulative path distance#1153Aditya-Dahiya wants to merge 3 commits intogeocompx:mainfrom
Aditya-Dahiya wants to merge 3 commits intogeocompx:mainfrom
Conversation
This commit adds an improved method for computing cumulative distances along a non-linear path when extracting elevation profiles from rasters. It replaces the straight-line (Euclidean) distance calculation with a stepwise sum of distances between consecutive points, ensuring more accurate results for curved transects. The new code enhances real-world applicability, especially for hiking trails and routes with turns or elevation changes.
Aditya-Dahiya
commented
Apr 15, 2025
Author
Aditya-Dahiya
left a comment
There was a problem hiding this comment.
Minor improvement in proposed code.
Robinlovelace
requested changes
Apr 15, 2025
Collaborator
Robinlovelace
left a comment
There was a problem hiding this comment.
Minor changes suggested.
Aditya-Dahiya
commented
Apr 16, 2025
Author
Aditya-Dahiya
left a comment
There was a problem hiding this comment.
Assignment operator changed from "<-" to "="
Member
|
Thanks @Aditya-Dahiya -- I agree that the improvement is needed. One thing that still bothers me, though, is the generalization of the proposed approach. It will only work on projected CRSs -- if I understand correctly, the outcome for the data in geographical CRS would be in degrees (not meters), which is not good. See an example and let me know what you think. library(sf)
library(terra)
library(dplyr)
zion_transect = cbind(c(-113.2, -112.9), c(37.45, 37.2)) |>
st_linestring() |>
st_sfc(crs = "EPSG:4326") |>
st_sf(geometry = _)
zion_transect$id = 1:nrow(zion_transect)
zion_transect = st_segmentize(zion_transect, dfMaxLength = 250)
zion_transect = st_cast(zion_transect, "POINT")
zion_transect_coordinates = zion_transect |> group_by(id) |> st_coordinates()
# ~true
zion_tc = zion_transect_coordinates[c(1, 240), ]
zion_tc
#> X Y
#> [1,] -113.2000 37.45000
#> [2,] -112.9199 37.21662
zion_tc_sf = zion_tc |>
as.data.frame() |>
st_as_sf(coords = c("X", "Y"), crs = "EPSG:4326")
st_distance(zion_tc_sf)
#> Units: [m]
#> [,1] [,2]
#> [1,] 0.00 35872.97
#> [2,] 35872.97 0.00
# incorrect?
point_distances0 = c(
0,
cumsum(sqrt(rowSums(diff(zion_tc)^2)))
)
point_distances0 # this result is in "degrees"
#> [1] 0.0000000 0.3646125 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit adds an improved method for computing cumulative distances along a non-linear path when extracting elevation profiles from rasters. It replaces the straight-line (Euclidean) distance calculation with a stepwise sum of distances between consecutive points, ensuring more accurate results for curved transects. The new code enhances real-world applicability, especially for hiking trails and routes with turns or elevation changes.
Original Code from the Book: This computes the straight-line (Euclidean) distance from the first point to every other point using
st_distance(). This works well only if the transect path is a straight line.Proposed Improved Code: This code accounts for the real path distance, even when the route is not a straight line.