GeographicLib
GeographicLib is a Python implementation of the geodesic routines from the larger C++ GeographicLib library. It provides accurate algorithms for solving direct and inverse geodesic problems, as well as calculating areas on an ellipsoid model of the Earth. The current version is 2.1, and it maintains a regular release cadence with updates tagged in its GitHub repository.
Warnings
- breaking Version 2.0 dropped support for Python 2.x. Users must upgrade to Python 3.7 or newer to use `geographiclib` version 2.0 and later.
- gotcha Since version 1.48, the default range for returned longitudes and azimuths changed from `[-180°, 180°)` to `(-180°, 180°]`. Code relying on the exact boundary behavior (e.g., expecting -180° instead of 180° for certain cases) might need adjustment.
- breaking Version 2.1 fixed an issue where the `Geodesic.WGS84` attribute might not have been accessible. If you had workarounds for this, they might now be obsolete or cause unexpected behavior.
- gotcha Version 2.0 introduced a more careful treatment of ±0° and ±180° for longitudes and azimuths. Positive values (+0°, +180°) are now reckoned as east-going, while negative values (-0°, -180°) are west-going. This can subtly affect calculations involving these edge cases.
Install
-
pip install geographiclib
Imports
- Geodesic
from geographiclib.geodesic import Geodesic
- GeodesicLine
from geographiclib.geodesicline import GeodesicLine
- PolygonArea
from geographiclib.polygonarea import PolygonArea
Quickstart
from geographiclib.geodesic import Geodesic
# Define the WGS84 ellipsoid
geod = Geodesic.WGS84
# Solve the inverse geodesic problem (distance and azimuth between two points)
# From Wellington, NZ (-41.32, 174.81) to Salamanca, Spain (40.96, -5.50)
g = geod.Inverse(-41.32, 174.81, 40.96, -5.50)
print(f"The distance is {g['s12']:.3f} meters.")
# Solve the direct geodesic problem (find a point at a given distance and azimuth)
# 20000 km SW of Perth, Australia (-32.06, 115.74)
g = geod.Direct(-32.06, 115.74, 225, 20000e3)
print(f"The position is ({g['lat2']:.8f}, {g['lon2']:.8f}).")