indiaciv, part 2: Terrain from satellites

Part 1 ended with 9,230 empty hexes and an address for each. This part gives them ground: elevation, land cover, a terrain type, and rivers. The method is the same everywhere: take a big raster, measure it under each hex, then turn the measurements into something that looks like a game.

Measuring a raster under a hex

Every raster source goes through one step: for each hex, compute a statistic of the pixels underneath it. The naive way takes every pixel whose centre falls inside the hex and averages those. That’s fine in the middle of the hex and sloppy at the edges, where a pixel can be 9% inside or 91% inside and the centre test treats both the same.

exactextract does it properly. It computes, for every pixel, the exact fraction wpw_p of the pixel’s area that lies inside the hex polygon, and weights by that:

vˉhex=∑pwp vp∑pwp,σhex=∑pwp (vp−vˉ)2∑pwp \bar{v}_{\text{hex}} = \frac{\sum_p w_p\, v_p}{\sum_p w_p}, \qquad \sigma_{\text{hex}} = \sqrt{\frac{\sum_p w_p\,(v_p - \bar v)^2}{\sum_p w_p}}

A hex over a pixel grid: interior pixels weigh 1, edge pixels weigh the share inside the hex

In the middle of the country the two methods barely differ. Coastal and border hexes are mostly edge, though, and there the centre test is biased. Exact weights also mean hex values add up: the area-weighted sum over all hexes equals the raster’s total, which matters a lot in part 3.

Two rasters do most of the work:

  • Elevation: the Copernicus GLO-90 DEM, read from its cloud-optimised GeoTIFF overviews, reprojected to EPSG:7755 at 500 m with averaging. Per hex I keep the mean, standard deviation, minimum and maximum.
  • Land cover: ESA WorldCover 2021, 11 classes (tree cover, shrubland, grassland, cropland, built-up, bare, snow and ice, open water, wetland, mangroves, moss), resampled to 400 m with nearest-neighbour so the class codes stay valid. Per hex I keep the fraction of each class, so every hex has a small 11-value histogram.

Both are cloud-optimised GeoTIFFs in public S3 buckets, so the pipeline reads only the tiles over India, at a reduced overview level, with HTTP range requests. A 20 km hex has no use for 10 m pixels.

The spread matters more than the height

The first instinct is to call anything high a mountain. But the Deccan plateau sits at 500–900 m and is flat as a table, and Ladakh’s valley floors are at 3,500 m. What makes a place look like mountains is relief: how much the ground rises and falls within the hex. That’s the standard deviation of elevation, σ\sigma.

Here is every Indian hex, by mean elevation against σ\sigma, coloured by the terrain it ended up with:

Scatter of mean elevation vs elevation spread for every Indian hex, coloured by terrain, with the hill and mountain thresholds

The lowlands form a long smear at the bottom: plains, grassland and forest, all under 1,000 m and under 100 m of spread. At σ≥110\sigma \ge 110 m it becomes hills, at σ≥300\sigma \ge 300 m mountains. The vertical tower on the right is the Himalaya. I picked the thresholds by looking at the result against places I know, and make validate pins some of them down with spot checks (the Himalaya must be mountains, the Thar must be desert).

The rule table

Terrain is decided by an ordered rule table. The first rule that matches wins, and that ordering is half the logic: a snowy, steep, wooded hex is snow, not mountains and not forest.

orderterrainrule
1snowsnow and ice ≥ 30% of land, or mean elevation ≥ 5,300 m
2mountainsσ ≥ 300 m, or mean ≥ 3,000 m with σ ≥ 150 m
3hillsσ ≥ 110 m, or mean ≥ 1,200 m
4marshherbaceous wetland + mangrove ≥ 30% of land
5desertbare + ½ shrubland ≥ 40% of land, or rainfall < 350 mm with tree cover < 10%
6jungletree cover ≥ 60%, rainfall ≥ 1,800 mm and mean temperature ≥ 18 °C
7foresttree cover ≥ 40% of land
8grasslandrainfall ≥ 1,000 mm and crops + grass + trees ≥ 50% of land
9plainseverything else on land

Land-cover shares are fractions of the land in the hex: open water is taken out of the denominator, so a coastal hex that is half sea and fully forested counts as 100% forest. Rainfall and temperature come from IMD normals (part 5). Climate is what separates jungle from forest and green grassland from dry plains. Before the climate data was in, those calls came from land cover alone, and they were noticeably worse.

On top of the table: an inland hex that is at least 60% open water and doesn’t touch the sea is a lake. Five qualify: Chilika, and four hexes of the Great Rann of Kutch, a salt flat that floods every monsoon and that WorldCover sees as open water. A sea hex touching land is coast.

Rather than describe every case, here’s the table running live on the real data. Hover a hex to see its inputs and each rule fail or match in order:

Every Indian hex with its real elevation, land-cover, rainfall and temperature inputs. The JavaScript re-implementation of the rule table agrees with the pipeline’s terrain.u8 on all 9,225 hexes it covers (the other 5 are lakes).

Writing that demo found a bug in the demo. My first version disagreed with the pipeline on 17 hexes in Ladakh, all “snow” against “mountains”. WorldCover’s 11th class, moss and lichen, is part of the land total in the pipeline but I’d left it out of the demo, so snow looked like a bigger share than it was. Same lesson as make validate: if you can compute something two ways, do, and compare.

The final counts:

Terrain across India and the count of hexes per terrain type: plains 3,597, grassland 1,877, forest 1,044, mountains 929, hills 854, desert 399, jungle 265, snow 236, marsh 24, lake 5

Plains dominate, which is right for India: the Indo-Gangetic plain and the Deccan interior are enormous. Jungle hugs the Western Ghats and the north-east. Desert is the Thar and the cold desert of Ladakh.

Beside the terrain enum, each hex also gets a byte of feature flags used only for decoration: woods (tree cover ≥ 40%), wetland, cropland (≥ 45%), urban (built-up ≥ 15%) and high (≥ 3,000 m). They tell the renderer to scatter trees on a wooded hill, or pick pines over broadleaf trees above 3,000 m.

See the Western Ghats on the live map →

Rivers run on edges, not through hexes

In most hex games, rivers run between hexes along the edges, not through the middle. That reads better (a river becomes a border, as real rivers often are), and it’s a fun constraint: every river has to become a path along hex edges.

The source is HydroRIVERS, a global river network where every reach carries its upstream drainage area and mean discharge. I keep reaches draining at least 9,000 km². Smaller rivers are below what a 20 km grid can show.

Snapping a river to edges:

  1. Sample each reach every R/4≈2.9R/4 \approx 2.9 km.
  2. Snap every sample to its nearest hex corner (a KD-tree over all corners).
  3. Join consecutive corners. Adjacent corners are joined directly; otherwise a breadth-first search finds the shortest corner-to-corner path. A back-and-forth spur (A → B → A) is dropped.
  4. Prune loops. Two reaches running either side of a hex can snap into a ring around it, so I run union-find over the corner graph and keep an edge only if it doesn’t close a cycle. Wider rivers go first, so if something has to give, it’s the smaller river.

HydroRIVERS reaches near Prayagraj and Varanasi in light blue, and the hex edges they snap to in dark blue

The Ganga and Yamuna meet at Prayagraj, and the zig-zag is the price of a hex grid. That’s about 5,100 river edges for the whole map.

Six bits per hex

Rivers are stored in two byte arrays. rivers.u8 is a bitmask: bit dd is set if a river runs along edge dd (the 0 NE … 5 SE numbering from part 1). Every edge is shared by two hexes, so both get the bit, on opposite edges. A river along the north edge of hex A is along the south edge of the hex above it.

hex (81, 67), just west of Prayagraj     rivers.u8 = 0b110011 = 51
  bit 0 (NE), bit 1 (N)  → river along the north-east and north edges
  bit 4 (S),  bit 5 (SE) → river along the south and south-east edges

Width needs one more trick. Mean discharge maps to four width classes (breaks at 150, 800 and 3,000 m³/s), which takes 2 bits per edge. Six edges would need 12 bits, but each edge belongs to two hexes, so each hex only stores the three edges it “owns”, NE, N and NW, and reads the rest from its neighbours:

riverw=wNE  ∣  (wN≪2)  ∣  (wNW≪4) \texttt{riverw} = w_{\text{NE}} \;|\; (w_{\text{N}} \ll 2) \;|\; (w_{\text{NW}} \ll 4)

For hex (81, 67) that byte is 10, 0b001010: width class 2 on both its NE and N edges. Six bits, one byte, no duplicated data. In the browser, the renderer walks the edge graph, chains the edges back into long polylines, smooths the corners, and strokes them three times (dark outline, body, highlight) at the width of the class.

See the Ganga–Yamuna confluence on the live map →

Next: people

The map now has ground and water. In part 3 it gets 1.46 billion people: spreading a 1 km population raster over the hexes without moving anyone across a state border, a bug where coastal cells drifted into the sea, and how 983 cities were picked and given land to work.