Coordinates & distance
Latitude and longitude as numbers: haversine and geodesic distance, bearings, destination points, great circles, DMS and UTM conversion, radius and nearest searches, and the time zone, country and elevation of a coordinate.
Latitude and longitude are angles, not x and y, and most errors with coordinates come from forgetting it. The guides here do the everyday calculations on raw coordinates in Python: haversine and ellipsoidal distance, bearings, destination points and range rings, midpoints and great-circle paths, DMS and UTM conversion, radius and nearest-neighbour searches with a BallTree, and the time zone, country and elevation of a point, with the fixes for radians, lost hemispheres, wrong UTM zones, buffers in degrees and truncated decimals.
Every number comes from a run on open data, mostly the 80,000 places of OurAirports and the US Geological Survey’s names for Hawaii. Haversine is within -0.38% to +0.56% of the ellipsoid for 19,981 random pairs; a half-degree buffer covers 9,649 km² at Singapore but 4,701 km² at Anchorage; float32 storage moves points by up to 0.84 m; and the 90 m elevation model puts Everest 125 m short.
🧭 Start with the concepts
The ideas the how-to guides below assume you already have.
⚙️ Then do the task
Step-by-step walkthroughs with code you can copy and adapt.
- How to Calculate the Distance Between Two Coordinates in Python (Haversine) haversine distance python latitude longitude
- How to Encode Coordinates as Geohashes in Python geohash python encode decode
- How to Calculate the Bearing Between Two Coordinates in Python bearing between two coordinates python
- How to Find a Destination Point from Distance and Bearing in Python destination point distance bearing python
- How to Find the Midpoint and Great-Circle Path Between Two Coordinates in Python midpoint great circle path python
- How to Convert Degrees, Minutes and Seconds to Decimal Degrees in Python convert dms to decimal degrees python
- How to Convert Latitude and Longitude to UTM in Python convert latitude longitude to utm python
- How to Find All Points Within a Radius in Python find points within radius python lat lon
- How to Find the Nearest Coordinates With a BallTree in Python nearest coordinates balltree haversine python
- How to Find the Time Zone of a Coordinate in Python timezone from latitude longitude python
- How to Find the Country of a Coordinate in Python country from coordinates python offline
- How to Get the Elevation of Coordinates in Python elevation from latitude longitude python api
🔧 When it breaks
The errors this topic produces, with the cause explained rather than just the command.
- Fixing a Haversine Function That Returns the Wrong Distance haversine wrong distance radians python
- Fixing DMS Coordinates That Land in the Wrong Hemisphere dms coordinates wrong hemisphere negative longitude
- Fixing Buffers Drawn in Degrees Instead of Kilometres buffer in degrees not kilometres geopandas
- Fixing Coordinates That Lose Precision When Saved to a File coordinates lose precision csv float32
Related topics
Subjects that keep coming up alongside this one, most overlap first.