nst-guide / data

Create map waypoints and layers from open data sources
https://nst-guide.github.io/data/
GNU General Public License v3.0
6 stars 0 forks source link

Compute average slope for trail #41

Open kylebarron opened 4 years ago

kylebarron commented 4 years ago

these values are in meters

total elevation change is: 346781.89000000095
total distance is: 4171048.0624210313
average slope is: 0.08314022874114661
from math import sqrt

from shapely.ops import linemerge

import geom
from data_source import Halfmile

def main():
    hm = Halfmile()
    gdf = hm.trail_full(False)
    # Reproject to meters
    gdf = geom.reproject_gdf(gdf, geom.WGS84, geom.CA_ALBERS)

    line = linemerge(gdf.geometry.values)

    # Calculate horizontal distance and vertical distance for each segment of
    # the data:
    dist_list, dz_list = calculate_dist_and_elev_change(line)
    total_elevation_change = sum([abs(x) for x in dz_list])
    total_distance = sum(dist_list)
    average_slope = total_elevation_change / total_distance

    print(f'total elevation change is: {total_elevation_change}')
    print(f'total distance is: {total_distance}')
    print(f'average slope is: {average_slope}')

def calculate_dist_and_elev_change(line):
    dist_list = []
    dz_list = []
    for coord, next_coord in zip(line.coords, line.coords[1:]):
        dx = next_coord[0] - coord[0]
        dy = next_coord[1] - coord[1]
        dz = next_coord[2] - coord[2]
        dist = sqrt(dx ** 2 + dy ** 2)
        dist_list.append(dist)
        dz_list.append(dz)

    return dist_list, dz_list

if __name__ == '__main__':
    main()