How to compute distance and bearing of two points represented by Coordinate strings in Python
Problem:
You have to points represented by some coordinate string in Python:
a = "N 48° 06.112' E 11° 44.113'"
b = "N 48° 06.525' E 11° 44.251'"
and you want to compute both bearing and distance between them
Solution
This can be done using a combination of two of our previous posts:
- How to parse any lat/lon string in Python using GeoPy and
- How to compute distance and bearing between two lat/lon points in Python
from geographiclib.geodesic import Geodesic
from geopy.geocoders import ArcGIS
geolocator = ArcGIS()
a = geolocator.geocode("N 48° 06.112' E 11° 44.113'")
b = geolocator.geocode("N 48° 06.525' E 11° 44.251'")
result = Geodesic.WGS84.Inverse(a.latitude, a.longitude, b.latitude, b.longitude)
distance = result["s12"] # in [m] (meters)
bearing = result["azi1"] # in [°] (degrees)
Result for our example:
distance = 784.3069649126435 # m
bearing = 12.613924599757134 # °