How to Build a 4X Strategy Map of the United States from Public Data

I still remember the first time I opened karpathy.ai/usaciv/. The United States was no longer a political map. It was a 4X strategy game: hex terrain, glowing city markers, a set of lenses that swapped which statistic animated across the country. Food. Production. Gold. Science. Culture.

What grabbed me wasn’t the visuals. It was the claim at the bottom of the readme: everything was built from public data. The Census, the BEA, NOAA, USGS, the EIA. One person, a few scripts, and a canvas.

I wanted to know how hard it would be to rebuild that myself. Turns out, it’s not. Here’s the pipeline I ended up running — and how you can build your own interactive US strategy map from public data in about a weekend.

What You’re Actually Building

Before the code, it helps to know what a strategy map actually is. In 4X games, the “map” layer is the player’s window into a world they can’t fully see. Each hex is a cell with attributes. Each city is a point with a yield. The lenses are filters that reorder your understanding of the map: measure it by food, and you see farmland. Measure it by gold, and you see the financial centers.

My version has four parts:

  • A hex grid covering the continental United States, with each cell colored by a metric you choose.
  • A city layer — named points with population, GDP, and a small set of yields.
  • Lens filters — toggle between production, gold, science, culture, and a combined view.
  • A panel — click any city and read its full stats, plus a search box to jump to any location.

Step 1: The Data Pipeline

The whole thing runs off five public data sources. None of them require a paid API key.

1. US Census ACS. The American Community Survey is the workhorse. It gives you city-level population, median income, rent burden, and industry employment. I pulled the 500 largest metros and a handful of smaller cities to fill the map out.

2. BEA (Bureau of Economic Analysis). GDP by state and, for the major metros, GDP by industry. This is the “production” and “gold” layer in game terms: economic output per region.

3. NOAA and USGS. These are terrain and resource data. NOAA’s Digital Coast gives you coastal elevation and land cover; USGS gives you mineral and energy resource potential. For a simple map you can use the land/water classification as the base hex color, then overlay resource yields on top.

4. EIA (Energy Information Administration). District-level energy production and consumption. Great for a “resources” lens that shows where the energy is coming from.

The good news: every one of these has a public, readable API or a downloadable dataset. You don’t need to scrape anything. You just need to fetch, filter, and normalize into a single JSON. If you want to see the data side of this, check out how to query OpenStreetMap data with the Overpass API, which walks through pulling public data the same way.

Step 2: Build the Hex Grid

The hardest part of any hex map is the geometry — not the rendering, but getting the hexagons to tile without gaps. Each hex has a “pointy top” or “flat top” orientation. For a continental map, flat-top works better because the rows read left-to-right naturally.

The math is straightforward. A hex grid is defined by a radius (the distance from center to a vertex) and a set of columns and rows. For each cell (col, row), you offset every other row by half a hex width, so the hexes interlock like a honeycomb. I used a grid of 117 columns by 90 rows, with a hexagon radius of 25 screen units.

The output is an array of hex centers. That’s your canvas coordinate space. Everything else attaches to it.

Step 3: Render the Base Map

I drew the map with the Canvas 2D API. No WebGL, no framework. Just a loop that paints each hex.

The color wells: each hex is a polygon with six vertices. I compute the centroid, look up its metric value from the data layer, and map that value to a color gradient. For my first pass I used a dark navy base for land and a lighter blue for water, then overlay the yield gradient. The gradient was a simple linear interpolation between two colors returned by a helper function.

The border: draw each hex with no fill on the edges so the adjacent hexes show through. That gives you the honeycomb structure on the canvas.

At this stage, the map is a colored canvas. It’s not interactive yet, but it’s already a real image.

Step 4: Add Lenses

The lenses are the part that makes a strategy map feel like a game. Each lens is a toggle that re-colors the entire grid according to one metric.

I kept a `lenses` array with the available views: production, gold, science, culture, and a combined “total yield.” Each entry has an id, a label, and a function that pulls the right metric from the data layer. When you click a lens button, the render function switches to that metric and repaints every hex.

The value at each hex is the sum of the city yields plus any tile-level resources within that hex. Since many hexes contain multiple cities or no cities at all, I computed a running total per hex during the data-loading step and stored it in the hex object. That way the lens toggle is just a key lookup, not a re-scan of the data.

Step 5: Interactivity

A strategy map that you can’t touch is just a poster. I added four interactions.

Search. A text input that filters the city list and jumps the camera to that city. The search term matched against city name and state abbreviation. I normalized the query to lowercase before comparing.

Click-to-inspect. When you click a hex or a city, a panel slides in from the side with the city’s full details: population, median income, GDP, industry breakdown, unemployment.

Zoom and pan. Left-drag to pan, scroll wheel to zoom. I kept a camera transform — a simple x/y offset plus a scale factor — and applied it to every drawing call. The camera object was a plain object with `offsetX`, `offsetY`, and `scale`. All render functions read from it.

Share. A “share this view” button copied the current viewport coordinates and zoom into the URL hash, so anyone with the link sees the same view. This was the smallest feature but the nicest to have.

Step 6: Deploy It

The map is a static website. All it needs to run is a web server that serves a few JSON files and an HTML page with the canvas. I deployed to GitHub Pages as a free, zero-config option. Cloudflare Pages and Netlify work the same way.

The only gotcha: the JSON data files are fetched with relative paths from the page. If you serve from a subdirectory, run a local AI project first to get comfortable with subdirectory paths and static serving — the same discipline applies here. And the hex grid and city data can get large, so I served the big files separately and only loaded them after the first frame rendered. That kept the first paint snappy.

What I Learned

Building this map taught me three things. First, public data is genuinely usable for more than dashboards — it becomes an experience. Second, the data layer is decoupled from the render layer, which is why I could swap the metric and repaint the whole map without touching the geometry. Third, a small amount of structure — data, render, lenses, UI — goes a long way. You don’t need a game engine to make a compelling strategy map — a few scripts and a canvas are enough. My earlier tutorial on DreamCoder shows the same pattern: pick a tool, write a small working example, then build outward.

If you’re looking for a weekend project that’s part map, part data pipeline, and part canvas practice, build your own. Pull the Census, draw a hex, and watch the country light up with your own eyes.

Hex grid map of the United States in strategy game style
Image: Brobbz via Wikimedia Commons (CC BY-SA 4.0)

Where to Go Next

If you want to go deeper, the place to start is the data. Once you have the normalized JSON, the rest is just a render loop. And if you want to see the data side of things next: query OpenStreetMap data with the Overpass API, or run a local LLM on your own machine. Both are practical, step-by-step guides that follow the same “pull real data, then build something” pattern.

Filed under Tech & Gadgets
Last Update: November 27, 2026 by Felix AlterEgo
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted