Normalize longitude, split crossings and treat projection distance as a display property.
Map boundaries: handle antimeridian crossings before drawing routes
Longitude has a seam
A route from 177 degrees east to 178 degrees west crosses a narrow part of the Pacific. A renderer that simply connects numeric longitudes may draw a line across nearly the entire map. Choose a longitude convention, validate it at ingestion, and split a crossing before sending geometry to the visual layer. Keep the original coordinates as well as display segments. Symbol maps also need a clear coordinate meaning; a route line needs the additional seam rule.
A bounding box can wrap
For an antimeridian-spanning area, a western longitude numerically greater than the eastern longitude can describe a small region. Do not automatically swap the two values or calculate width by direct subtraction. Compute wrapped angular span and tell a mapping library whether its viewport crosses the seam. A naive min-to-max bounding box may zoom to almost the whole world. Region boundaries need the same care when a polygon is split into multiple pieces for display.
Projection changes visual geometry
A flat map projects a curved surface. Pixel distance, area and direction can vary with projection and latitude. Use a projection appropriate to the question, and avoid estimating physical travel distance with screen coordinates. A straight segment drawn in longitude and latitude is not automatically the shortest path on Earth. When a route represents measured travel, retain the actual track; when it represents an abstract connection, label it as such. Scale integrity applies to maps as well as charts.
Preserve identity across pieces
After a crossing is cut into two display segments, both pieces must retain one route ID so a tooltip, filter and export refer to the same underlying trip. A segment count is not a trip count. Include source coordinate order, time zone for event timestamps, geometry version and wrapping method in the export. Test endpoints exactly at positive and negative 180 degrees. The export manifest should record enough to reproduce the same viewport and splitting rule after a map library upgrade.
Worked contract
from math import isfinite
def split_dateline_segment(west: tuple[float, float],
east: tuple[float, float]
) -> list[list[tuple[float, float]]]:
lon_a, lat_a = west
lon_b, lat_b = east
if not all(isfinite(value) for value in (*west, *east)):
raise ValueError("coordinates must be finite")
if not (-180 <= lon_a <= 180 and -180 <= lon_b <= 180
and -90 <= lat_a <= 90 and -90 <= lat_b <= 90):
raise ValueError("longitude or latitude outside range")
difference = lon_b - lon_a
if abs(difference) <= 180:
return [[west, east]]
adjusted_b = lon_b - 360 if difference > 180 else lon_b + 360
seam = -180 if adjusted_b < lon_a else 180
fraction = (seam - lon_a) / (adjusted_b - lon_a)
seam_lat = lat_a + fraction * (lat_b - lat_a)
opposite = 180 if seam == -180 else -180
return [[west, (seam, seam_lat)], [(opposite, seam_lat), east]]
shipping_route = split_dateline_segment((177, -17), (-178, -18))
assert shipping_route == [
[(177, -17), (180, -17.6)],
[(-180, -17.6), (-178, -18)],
]Performance and operating cost
Splitting one two-point segment is O(1) time and space; a route with N points needs O(N) processing and output. The interpolation is linear in longitude and latitude for display geometry, not a geodesic calculation. For long-distance navigation or polar tracks, densify with a suitable geographic method before projecting. A production pipeline must define how exact-seam endpoints, multi-segment tracks and duplicate 180-degree coordinates are serialized.
Common Mistakes
- Drawing a nearly global line between two points that are close across the seam.
- Swapping wrapped bounding-box longitudes and accidentally selecting most of the world.
- Measuring real distance from flat-map pixels.
- Counting two rendered pieces as two journeys.
Validation drill
Draw 47 shipping tracks with three seam crossings. Keep one ID per journey, verify wrapped viewport width, and compare the exported track length with the number of display segments.
Read next
- Proportional-symbol maps: size circles by area, not radius
- Choropleth maps: normalize counts and audit boundary coverage
- Axis scales, baselines and labels: prevent a correct number from telling a false story
- Chart exports: carry the data, filters and render contract together
