Natural Earth · Drawn by Graticule
Writing a map engine from scratch · 2
Twenty map projections over one sphere, written for this page with no map library underneath. Drag to turn the globe, or switch to Drag countries and carry one to another latitude.
Six steps from a file of borders to the map above. Each one is a few hundred lines of code, and each can be watched from the controls on the map.
Natural Earth gives 242 countries as separate polygons, so every border between two countries is stored twice. Graticule's own TopoJSON writer finds the points where lines part ways, cuts the rings there into arcs and keeps each arc once. The 242 countries and 412 lakes become 2,460 arcs holding 98,307 points, and 384 of those arcs are borders shared by two countries. The same pass removes Antarctica's slit down to the pole and sews Russia and Fiji back together across 180°.
"arcs": [[0, 1, ~2]] ~2 means arc 2, walked backwards
Every point gets an importance once, when the file loads: the area of the triangle it makes with its neighbours (Visvalingam), or how far it lies from the chord (Douglas–Peucker), both measured on the sphere. Moving the slider only moves a threshold. A shared border is simplified once, so neighbours stay joined. Untick Shared borders and lower the detail to see what happens when each country is simplified on its own.
Every formula on this page assumes the centre of the map is at 0°, 0°. A Pacific-centred map, a transverse Mercator or an oblique Mollweide is the same formula applied after the sphere has been turned. Dragging the map turns the sphere, not the picture.
R = Rx(roll) · Ry(φ₀) · Rz(−λ₀)
On a world map the sphere is slit open along the meridian opposite the centre. Where a coastline crosses that slit, the crossing point is found on the sphere and the ring is cut. Each piece then walks anticlockwise along the edge of the map to the next piece. A coast that goes round the pole, like Antarctica's, crosses the slit an odd number of times, so one of its pieces has to walk along the bottom edge, under the pole, to close itself. Azimuthal maps cut along a small circle instead, and stitch along it in the same way.
exit → along the edge, anticlockwise → next entry
A straight segment on the sphere is seldom straight on the map. The midpoint of each segment is projected; if it lands more than 0.4 px from the chord, the segment is split in two, and so on until the curve is smooth. Tick Resampled points to see the points this adds.
At any point, a tiny circle on the sphere becomes an ellipse on the map. Its axes, a and b, come from the derivatives of the projection: a × b is how much area grows, and the difference between them is how far angles bend. Equal-area projections come out at 1.00 everywhere. The self-check relies on this: on 7 equal-area projections from 6 viewpoints, the projected area of every country equals its area on the sphere.
Made with Natural Earth 1:50m (v5.1.2, public domain). Borders are Natural Earth's de facto lines; a few long names are shortened for labels. Areas are measured on a sphere of radius 6,371 km.