Book Image

Python Geospatial Analysis Cookbook

Book Image

Python Geospatial Analysis Cookbook

Overview of this book

Table of Contents (20 chapters)
Python Geospatial Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Other Geospatial Python Libraries
Mapping Icon Libraries
Index

Calculating 3D ground distance and total elevation gain


We've finished finding points on lines and returning points on a line, so now, it is time to calculate the true ground 3D distance that we actually ran or biked along a real 3D road. It is also possible to calculate the elevation profile and we will see this in the Chapter 7, Raster Analysis.

Calculating the ground distance sounds easy, but 3D calculations are more complicated to calculate than 2D. Our 3D LineString has a z-coordinate for each vertex that makes up our LineString. Therefore, we need to calculate the 3D distance between each set of coordinates, —that is, from vertex to vertex in our input LineString.

The mathematics to calculate the distance between two 3D Cartesian coordinates is relatively simple and uses the 3D form of the Pythagoras formula:

3d_distance = square root √ ( (x2 – x1) 2 + (y2 – y1) 2 + (z2 -z1)2)

Here it is in Python:

import math
3d_dist = math.sqrt((x2 – x1)**2 + (y2 – y1)**2 + (z2 – z1)**2 )

Getting ready...