David
David

Reputation: 87

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

Upvotes: 2

Views: 9180

Answers (3)

Ghoti
Ghoti

Reputation: 759

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

Upvotes: 1

John Allwork
John Allwork

Reputation: 29

FWIW, I needed north, south, east, west etc. as text. Here's my (old style programmer) code:

import math

def calcBearing (lat1, long1, lat2, long2):
    dLon = (long2 - long1)
    x = math.cos(math.radians(lat2)) * math.sin(math.radians(dLon))
    y = math.cos(math.radians(lat1)) * math.sin(math.radians(lat2)) - math.sin(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.cos(math.radians(dLon))
    bearing = math.atan2(x,y)   # use atan2 to determine the quadrant
    bearing = math.degrees(bearing)

    return bearing

def calcNSEW(lat1, long1, lat2, long2):
    points = ["north", "north east", "east", "south east", "south", "south west", "west", "north west"]
    bearing = calcBearing(lat1, long1, lat2, long2)
    bearing += 22.5
    bearing = bearing % 360
    bearing = int(bearing / 45) # values 0 to 7
    NSEW = points [bearing]

    return NSEW

# White house 38.8977° N, 77.0365° W
lat1 = 38.8976763
long1 = -77.0365298
# Lincoln memorial 38.8893° N, 77.0506° W
lat2 = 38.8893
long2 = -77.0506

points = calcNSEW(lat1, long1, lat2, long2)
print ("The Lincoln memorial is " + points + " of the White House")
print ("Actually bearing of 231.88 degrees")

print ("Output says: The Lincoln memorial is south west of the White House ")

Upvotes: 3

cffk
cffk

Reputation: 1929

Use my python pacakge geographiclib.

After

pip install geographiclib

do

$ python
Python 2.7.14 (default, Nov  2 2017, 18:42:05) 
>>> from geographiclib.geodesic import Geodesic
>>> geod = Geodesic.WGS84
>>> g = geod.Inverse(41.4107628,2.1745004, 41.4126728,2.1704725)
>>> print "The initial direction is {:.3f} degrees.".format(g['azi1'])
The initial direction is -57.792 degrees.

The direction is measured clockwise from north. So -57.8 degrees = 302.2 degrees = northwest-by-west; see Points of the compass.

Upvotes: 5

Related Questions