Skip to content

Instantly share code, notes, and snippets.

@evandiewald
Created August 19, 2022 19:00
Show Gist options
  • Save evandiewald/15f0a8f09f43b65954e4d5fce31526de to your computer and use it in GitHub Desktop.
Save evandiewald/15f0a8f09f43b65954e4d5fce31526de to your computer and use it in GitHub Desktop.
def get_elevation_profile_of_segment(dataset: rasterio.DatasetReader, coords: list[list]):
"""
Get the elevation profile (distance vs. altitude) of a path segment from the list of coordinates.
Args:
dataset: The opened rasterio dataset for the SRTM global topography data.
coords: The path coordinates in [[lon1, lat1], [lon2, lat2], ...] format.
Returns: The distance (in miles) and elevation (in feet) vectors.
"""
# coordinates are [lon, lat], flip for rasterio
coords = [[c[1], c[0]] for c in coords]
# convert meters to feet and use rasterio.sample.sample_gen to query each point
elev = [e[0] * 3.28084 for e in sample_gen(dataset, coords)]
d = [0.0]
for j in range(len(coords) - 1):
# use haversine distance
d.append(d[j] + haversine((coords[j][1], coords[j][0]), (coords[j + 1][1], coords[j + 1][0]), Unit.MILES))
return d, elev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment