pythongpscoordinatesdirectionsbearing

Calculate cardinal direction from GPS coordinates in Python


How can I calculate the cardinal direction of a second point of geo coordinates in Python? I need to know distance and direction of two different locations.

Example : work : coord 41.4107628,2.1745004 home : coord 41.4126728,2.1704725

from geopy.distance import vincenty
work = geopy.Point(41.4107628,2.1745004)
home = geopy.Point(41.4126728,2.1704725)
print(vincenty(home, work))
0.398011015257 km

I'd like to know in which directions the second point lies, ( ex.: North, North-West, etc) respect to the first one...

Many thanks in advance


Solution

  • Edit for clarity: this is an approximation that will tell general direction. Use the link below for an easy online calculator. Otherwise follow other answers provided.

    The first value in the coordinate system represents North/South direction, the second represents East/West. Simple subtraction will provide a general direction. For example subtracting B from A we get:

    41.4126728 - 41.4107628 = 0.00191

    2.1704725 - 2.1745004 = - 0.0040279

    This indicates that to reach point B from point A, you would need to travel in a North (positive value) West (negative value) direction. A precise angle can be found by using trigonometry (think of each value as a side of a right triangle). As mentioned in the comments, trigonometry is insufficient for an accurate calculation.

    You might find this website interesting: https://www.movable-type.co.uk/scripts/latlong.html