Skip to content

The Earth Is Not a Ball

Edit source

Turning latitude and longitude into vectors you can do geometry on, on an Earth that is lumpier than a sphere.

Part 21 of 38ofFamiliars
  1. 1Session Zero
  2. 2Rolling for a Name
  3. 3The Haunted Handler
  4. 4The Sparse Wheel
  5. 5The Fork in the Path
  6. 6The Planet as a Prefix
  7. 7The Long Division
  8. 8The Line You Usually Cancel
  9. 9Sixteen Bytes Too Far
  10. 10Packing a UUID
  11. 11Bytes You Can Type
  12. 12The Shunting Yard
  13. 13Interchangeable Parts
  14. 14One Loop Per Core
  15. 15The Seam
  16. 16Append Only
  17. 17Who Owns the Tree
  18. 18Down the Flume
  19. 19The Syscalls That Lie
  20. 20Asking the Machine
  21. 21The Earth Is Not a Ball
  22. 22Smith and Schmidt
  23. 23Many Ways to Be Similar
  24. 24Built to Disappear
  25. 25Sometimes a Mutex Is Right
  26. 26Work, Not Threads
  27. 27Before the Program Runs
  28. 28No Collisions, Ever
  29. 29The Names Enums Throw Away
  30. 30The Bag of Holding
  31. 31A Map of Blocks
  32. 32Certainly Absent
  33. 33A Tree Made of Types
  34. 34Everything but the Views
  35. 35Who Owns the Wheel
  36. 36A Guest on the Loop
  37. 37Acknowledged Means Durable
  38. 38Sorted as Bytes

Part of Familiars: the small, sharp libraries I carved out of Xapiand. This one is the ground the mesh stands on: cartesian.

Latitude and longitude feel like coordinates, but they are angles on a curved surface, and you cannot do vector math on angles. You cannot dot two latitudes, or take a meaningful cross product of two longitudes, or measure the angle between two cities by subtracting their coordinates. Every real piece of geographic geometry, the angular distance between two points, the plane through three of them, feeding a circular region to a spatial index, starts by leaving lat/long behind and turning each point into a genuine 3-D vector: its position in space, measured in meters from the center of the Earth. cartesian is the library that does that crossing, in both directions, and the little algebra you need once you are on the other side.

The type is Cartesian, and it wears two hats. As a converter, you hand it a latitude, longitude, and height and it returns the geocentric (x, y, z) for that point; toGeodetic takes a vector back to lat/long/height, and toDegMinSec pretty-prints it. As a 3-vector, it is exactly what you would want: dot product, cross product, addition and subtraction, norm, normalize, inverse, and an angular distance between two surface points. Once your cities are unit vectors, “how far apart are they” is the angle between two vectors, an acos of a dot product, and “the boundary of this region” is geometry you can actually compute. This is precisely the vector type the HTM mesh runs on: the mesh tiles the unit sphere, and cartesian is what puts your points onto it.

The naive version of this conversion pretends the Earth is a sphere, and for rough work that is fine. cartesian does not pretend. It converts on the WGS84 ellipsoid, because the Earth is flattened at the poles by about a third of a percent, and treating it as a ball puts you tens of kilometers off in the worst places. That is one source of lumpiness. The other is human: the world’s maps were not all drawn against the same reference. A coordinate reference system (an SRID) pins down which ellipsoid and which datum, which origin and orientation, the numbers are measured against, and there are many: WGS84, NAD83 and the older NAD27, Britain’s OSGB36, Europe’s ED50, Tokyo, seventeen of them shipping with their parameters. A latitude in one datum is a different place from the same latitude in another, sometimes by hundreds of meters.

So converting a point is not just trigonometry on an ellipsoid; it is also moving between datums, which cartesian does with a 7-parameter Helmert transform, three translations, three rotations, and a scale, that shifts a coordinate from its source datum into WGS84, the canonical frame everything converts into. It is the unglamorous, essential geodesy that separates a geospatial system that is roughly right from one that is actually right, and it is the reason “put a pin on the map” is a harder sentence than it sounds.

Reach for it when you need real geometry on Earth coordinates: angular distances, spatial indexing, moving between coordinate reference systems, anything where “close enough for a sphere” is not close enough. Skip it if a flat-Earth approximation over a small area is all you need (planar math on a local projection is simpler and faster), or if a full GIS library already owns your coordinate handling.

The next familiar leaves space for sound. It matches words not by how they are spelled but by how they are said, so that Smith and Schmidt finally land in the same place.