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)
- How to Encode Coordinates as Geohashes in Python
- How to Calculate the Bearing Between Two Coordinates in Python
- How to Find a Destination Point from Distance and Bearing in Python
- How to Find the Midpoint and Great-Circle Path Between Two Coordinates in Python
- How to Convert Degrees, Minutes and Seconds to Decimal Degrees in Python
Show every how-to guide in this topic
- How to Convert Latitude and Longitude to UTM in Python
- How to Find All Points Within a Radius in Python
- How to Find the Nearest Coordinates With a BallTree in Python
- How to Find the Time Zone of a Coordinate in Python
- How to Find the Country of a Coordinate in Python
- How to Get the Elevation of Coordinates in Python
- How to Estimate Bike-Share Trip Distance and Speed in Python
- How to See the Jet Stream in Flight Times
- How to Draw a Hurricane's Wind Field from HURDAT2 Wind Radii
🔧 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
- Fixing DMS Coordinates That Land in the Wrong Hemisphere
- Fixing Buffers Drawn in Degrees Instead of Kilometres
- Fixing Coordinates That Lose Precision When Saved to a File
- Dockless E-Bike Trips Pile Up on a Grid: Rounded Coordinates
- Restaurants Plotted in the Ocean: Zero Coordinates in Inspection Data
Related topics
Subjects that keep coming up alongside this one, most overlap first.