Calculating line direction
In addition to distance, you'll often want to know the bearing of a line between its end points. We can calculate this line direction from one of the points using only the Python math module, as shown in the following calculation:
>>> from math import atan2, cos, sin, degrees >>> lon1 = -90.21 >>> lat1 = 32.31 >>> lon2 = -88.95 >>> lat2 = 30.43 >>> angle = atan2(cos(lat1)*sin(lat2)-sin(lat1) * >>> cos(lat2)*cos(lon2-lon1), sin(lon2-lon1)*cos(lat2)) >>> bearing = (degrees(angle) + 360) % 360 >>> print(bearing) 309.3672990606595
Sometimes, you end up with a negative bearing value. To avoid this issue, we add 360 to the result to avoid a negative number and use the Python module operator to keep the value from climbing over 360.
The math in the angle
calculation is reverse engineering a right triangle and then figuring out the acute angle of the triangle. The following URL provides...