indiaciv, part 3: Putting 1.46 billion people on the map

Part 2 gave every hex terrain and rivers. Now it needs people. That sounds like the same raster-to-hex step as elevation, and it almost is. But population is a count, not an average, and counts have to add up. That one difference produced this project’s most important rule and its most instructive bug.

A raster of people

The source is GHS-POP from the EU’s Joint Research Centre: a 1 km grid of the world’s population, here the 2025 epoch, in the Mollweide equal-area projection. Each cell holds an estimated head count, built by spreading census counts over the buildings that satellites can see.

For India that’s about three million non-empty cells. Each one becomes a row in a cell table:

columnmeaning
valuepeople in the 1 km cell
hexthe hex its centre falls in (reprojected to EPSG:7755, then the cube-rounding from part 1)
districtthe Census 2011 district it falls in (rasterised polygons)
x, ythe centre, in metres

The population of a hex is then just a sum over its cells:

Ph=∑c : hex(c)=hvaluec P_h = \sum_{c\,:\,\text{hex}(c) = h} \text{value}_c

It’s a sum, not exactextract’s weighted mean, because the cells are tiny next to a hex. A 1 km cell is 0.3% of a 346 km² hex, so assigning each cell whole to the hex holding its centre is accurate, and it gives every person exactly one hex. That turns out to matter.

Population density across India on the hex grid, log scale from 10 to 10,000 people per km²

The Gangetic plain glows, as it should. The densest hex holds 12.4 million people, central Bengaluru, and the whole map sums to 1.458 billion, within 0.03% of the same raster summed over the Survey of India outline.

The bug: people fall into the sea

My first version did exactly the above, and the state totals were off. Some by a lot.

The problem is the hexes on borders and coasts. A hex is Indian if at least half of it is inside India (part 1), and it belongs to the state that covers most of it. So a hex on the Kerala coast that is 45% land is sea, and the people on that 45% fall into the ocean. A hex straddling the Assam–Arunachal border goes to whichever state has more of it, and the other state’s people go with it.

Mumbai is the extreme case. The island city is a narrow strip of land with sea on both sides, so on a 20 km grid much of it falls in hexes that are mostly water:

Mumbai: 1 km population cells on the hex grid; red cells fall in sea hexes and are moved to the nearest Maharashtra hex

8.4 million people in Mumbai sit on cells whose centre lands in a sea hex. Across India, the naive assignment put 29 million people (2%) on non-Indian hexes, and moved another 36 million into the wrong state.

The fix: every cell stays in its own state

The fix is a rule I ended up applying to every dataset in the project, so it has its own module (alloc.own_state):

A cell belongs to the state of its district. If the hex it falls in belongs to a different state, or to the sea, move the cell to the nearest hex of its own state. A cell outside India never lands on an Indian hex.

In code it’s a KD-tree per state:

move = (cell_state > 0) & (state_of_hex[cell_hex] != cell_state)
for st in np.unique(cell_state[move]):
    hexes = np.nonzero(state_of_hex == st)[0]                 # all hexes of this state
    sel = np.nonzero(move & (cell_state == st))[0]            # its stray cells
    _, k = cKDTree(hex_centres[hexes]).query(cell_xy[sel])    # nearest one each
    cell_hex[sel] = hexes[k]

Nobody leaves their state, so every state total is preserved by construction. The people on the sea hexes off Mumbai move to the nearest Maharashtra hexes, the three arrowheads in the picture.

Here’s what that does to every state with more than a million people:

Per-state population error on the hex map: the naive assignment is off by up to 32% (Puducherry) and 16% (Arunachal Pradesh); the own-state rule brings every state within about 3%

Without the fix, Puducherry loses a third of its people. It’s a set of tiny coastal enclaves inside Tamil Nadu, and almost none of its hexes are majority-Puducherry. Nagaland and Ladakh lose a fifth or more to their neighbours, and Arunachal gains 16% from Assam. With the fix, every state is within 3% of its source total. The small remaining difference is measurement: the source totals sum the raster over exact state polygons, while the cell table assigns whole cells by their centre.

make validate holds the line: India’s total within 2% of the source, and every state over 5 million people within 2% of its own total. It currently reports 22 states, all passing.

Spreading statistics: dasymetric allocation

The same cell table does a second job. Lots of data comes as one number per district or per state: crop production, state GDP. To put a district total on the map you have to decide where in the district it goes. Spreading it evenly over the area is wrong. Wheat isn’t grown in the Thar part of a district, and industry isn’t in the forest.

Dasymetric allocation spreads a unit’s total TuT_u over the unit’s cells in proportion to a weight wcw_c that says where the thing probably is:

Vh=∑uTu⋅∑c∈u, hex(c)=hwc∑c∈uwc V_h = \sum_{u} T_u \cdot \frac{\sum_{c \in u,\ \text{hex}(c) = h} w_c}{\sum_{c \in u} w_c}

Each unit’s total is shared out exactly. The fractions over a unit’s cells sum to 1, so hex values sum back to the district and state totals no matter what the weight is. The weight only decides where. Cropland pixels spread crop production; night lights spread industry; part 4 is all about these choices. And because the cells go through own_state first, nothing leaks across a state border.

Cities

Cities come from a different JRC product, the GHS Urban Centre Database (R2024A). An urban centre is a contiguous cluster of 1 km cells with at least 1,500 people per km² and at least 50,000 people in total. It’s a definition of a city by what’s on the ground, not by municipal boundaries.

I keep every urban centre inside the Survey of India outline with at least 100,000 people: 983 cities. Each goes on the hex holding its centroid, one city per hex; if two collide, the smaller one moves to the nearest free neighbour. They come in four size tiers for the banner and sprite: 680 under 300k, 226 up to a million, 64 up to five million, and 13 above.

The definition has one striking consequence. In densely settled countryside the 1,500/km² clusters merge across towns. The largest “city” is New Delhi with 31.4 million, because Ghaziabad, Gurugram and Noida are part of the same continuous built-up cluster. And the sixth largest is Hajipur, 9.8 million, because much of north Bihar, including Muzaffarpur, is one unbroken dense settlement. I show the numbers as published, with the merged place names in the tooltip.

See Hajipur’s urban centre on the live map →

Working the land

In the games this is inspired by, a city works the tiles around it. I borrow that: each city works every hex within 3 steps, 37 hexes in all. A city’s yields are the sums of the hex yields over its worked hexes. Cities are often closer together than 6 hexes, though, so the rings overlap, and a hex counted by two cities would double its food. So each hex is worked by exactly one city, the nearest, with ties going to the larger city:

Hexes around Delhi coloured by the city that works them: each hex within 3 steps of a city goes to the nearest city

The rule is simple to implement: go through the cities from largest to smallest and claim each hex in range if this city is strictly closer than the current owner. Larger cities claim first, so they win ties. Hex distance on an offset grid is easiest in cube coordinates, where it’s the largest of the three axis differences:

d(a,b)=max⁡(∣xa−xb∣, ∣ya−yb∣, ∣za−zb∣) d(a, b) = \max\big(|x_a - x_b|,\ |y_a - y_b|,\ |z_a - z_b|\big)

From rupees to a 1–30 score

City yields span three orders of magnitude. A small hill town’s worked hexes produce a few hundred crore rupees of industrial output a year, and New Delhi’s produce 1.4 lakh crore. A linear 1–30 scale would put almost every city at 1. So the score is logarithmic, between the 5th percentile (lolo) and the maximum (hihi) across all cities:

score=round⁡(1+29⋅clamp⁡(ln⁡(x/lo)ln⁡(hi/lo), 0, 1)) \text{score} = \operatorname{round}\left(1 + 29 \cdot \operatorname{clamp}\left(\frac{\ln(x / lo)}{\ln(hi / lo)},\ 0,\ 1\right)\right)

Production score against industrial value added over each city’s worked hexes, on a log axis: a straight line from 1 at the 5th percentile to 30 at the maximum

Each step on the 1–30 scale is about 20% more output. The banners show these scores, and the tooltip shows the real totals behind them. Part 4 is about where those totals come from.

Next: yields

Part 4 is the economics: how district crop statistics become food, state industry accounts and night lights become production, services and population become gold, and universities and museums become science and culture. All in real units, all adding back up to their sources.