MapMath

All chapters.

5 free · 22 paid · with worked examples and runnable code.

Unlock all paid chapters →

27 chapters

CH 01free

Why map math matters

What this guide covers, the notation we'll use, and why understanding map math matters even when libraries hide it.

notationfundamentals
4 min
CH 02free

The shape of the Earth

Sphere, ellipsoid, geoid — three models, three answers, and which one you actually need for app development.

WGS84ellipsoidgeoid
4 min
CH 03free

Coordinate systems: latitude and longitude

Latitude and longitude explained — decimal vs DMS notation, WGS84 and EPSG:4326, datum shifts, and how big a degree actually is in metres.

lat/lonDMSEPSG
3 min
CH 04free

Map projections — flattening the globe

Why every flat map lies, the math behind Web Mercator and equal-area projections, and how to pick the right projection for your use case.

MercatordistortionCRS
3 min
CH 05free

Web Mercator and tile math

How every web map (Google, OSM, Mapbox) tiles the world, with the formulas to convert lat/lon to tile coordinates.

z/x/y tilesWeb Mercator
3 min
CH 06

Pixel and world coordinates

World pixels and screen pixels: the coordinate system behind every marker, overlay, and mouse-to-lat-lon conversion at any zoom level.

pixel coordsworld space
3 min
CH 07

Distance on a sphere — Haversine and friends

Why Pythagoras lies on a sphere, the full Haversine derivation, Vincenty for ellipsoid accuracy, and which formula to use for which job.

Haversinegreat circle
3 min
CH 08

Bearings, headings, and azimuths

Computing the angle from one point to another, normalizing to compass headings, and why bearings change along a path.

bearingazimuthcompass
3 min
CH 09

Destination points — moving on a sphere

Given a starting point, bearing, and distance, compute the destination on a sphere — used for geofences, range rings, and dead reckoning.

destinationspherical
2 min
CH 10

Bounding boxes and spatial queries

How to compute a bbox from a point and radius for fast database queries — and the edge cases nobody warns you about.

bboxradiusqueries
3 min
CH 11

Polygons — area, centroid, and containment

Ray-casting point-in-polygon, planar and spherical polygon area, centroid computation, and the edge cases every implementation misses.

areacentroidray cast
3 min
CH 12

Spatial indexing — Geohash, Quadtrees, S2, H3

Geohash, Quadtrees, S2, and H3 compared — how to answer 'what's near here?' across millions of points in milliseconds, and which to pick.

GeohashS2H3
3 min
CH 13

Map clustering algorithms

Grid clustering, distance-based methods, and the Supercluster KD-tree algorithm Mapbox and MapLibre use to cluster millions of markers.

DBSCANSupercluster
2 min
CH 14

Advanced topics — Vincenty, ECEF, and affine transforms

Sub-meter ellipsoidal accuracy with Vincenty, 3D Earth-Centered coordinates, pan/zoom transforms, and rhumb lines.

VincentyECEFaffine
3 min
CH 15

Practice problem set

Eight worked exercises with full step-by-step solutions, covering coordinate conversions, Haversine distance, tile math, and spherical area.

exercisessolutions
3 min
CH 16

The antimeridian — the bug that breaks every map

Why lines and polygons that cross the ±180° longitude line break naive code, the splitting algorithm that fixes it, and GeoJSON specifics.

antimeridiandate line
3 min
CH 17

GeoJSON — the universal format

Every GeoJSON geometry type, winding order rules, the lon/lat ordering trap, and the validation rules that every developer trips over.

GeoJSONRFC 7946
3 min
CH 18

Coordinate transforms — converting between CRS

EPSG:4326 ↔ 3857 by hand, using proj4js, and the practical situations where you need to transform coordinates.

EPSG:3857proj4UTM
4 min
CH 19

How maps render — tiles, vectors, and the GPU pipeline

From lat/lon to pixels on screen — the full rendering pipeline for raster tiles, vector tiles, and WebGL-based maps like MapLibre and Mapbox.

WebGLvector tilesMVT
4 min
CH 20

Geometry simplification

Douglas-Peucker and Visvalingam-Whyatt — how to strip vertices from polygons and lines without losing their shape.

Douglas-Peuckersimplifyperformance
3 min
CH 21

Buffering and spatial operations

Expanding geometries by a distance, computing unions and intersections, and building spatial pipelines with Turf.js.

bufferunionTurf.js
3 min
CH 22

Routing on road networks

Graph representation of road networks, Dijkstra's algorithm, A* with a Haversine heuristic, and how turn restrictions change the model.

DijkstraA*road graph
3 min
CH 23

Interpolation and heat maps

Turning sparse sample points into continuous surfaces — IDW, kernel density, Kriging, and rendering techniques.

IDWKDEheat map
3 min
CH 24

Terrain and elevation data

Digital Elevation Models, Terrain RGB encoding, hillshading math, slope and aspect — the foundation of 3D maps.

DEMhillshadingterrain RGB
4 min
CH 25

Camera and perspective math

The map camera model, pitch and bearing, perspective projection matrices, view frustum, and unprojecting pixels to world coordinates.

camerafrustumperspective
4 min
CH 26

3D buildings and feature extrusion

Turning 2D building footprints into 3D meshes with Mapbox fill-extrusion, LOD strategies, rendering order, and the math behind extrusion.

extrusionLOD3D mesh
3 min
CH 27

ECEF and 3D coordinate systems

Earth-Centered Earth-Fixed coordinates, converting lat/lon/altitude to XYZ, the ENU local frame, and 3D distances.

ECEFENU3D coords
3 min