Map Lab
How these maps were built
The three spatial queries behind the Map Lab, with their results: simplifying tract outlines, counting stores per H3 hexagon, and an exact radius query to check the hexagon shortcut.
The Map Lab draws on two chapters. Chapter 7's pipeline reads Overture places, TIGER/Line tracts and the American Community Survey, finds each tract's nearest dollar store and supermarket, and writes a folder of Map Lab files for six metro areas. Chapter 9's pipeline writes the taxi-zone file with one column per hour. The Map Lab's own pipeline, maplab/pipeline/, reads those files back, drops what the pages do not need, writes public/data/maplab/, and runs the three queries below. It reads only local files and takes about 3 seconds. Chapter 7's pipeline, which it depends on, takes about 9 minutes and downloads about 3 GB.
Every input is in longitude and latitude on WGS84 (EPSG:4326), and every session sets geometry_always_xy = true so DuckDB 2.0 reads them in that order. Distances are measured on the spheroid with ST_Distance_Spheroid, in metres.
1. Simplify the tract outlines
TIGER/Line tract outlines follow every bend of a river or road. At metro zoom levels most of those vertices fall within the same few pixels.
SELECT t.slug, count(*) AS tracts,
count(*) FILTER (WHERE NOT ST_IsValid(t.geom)) AS invalid_before,
count(*) FILTER (WHERE NOT ST_IsValid(
ST_SimplifyPreserveTopology(t.geom, 0.0005))) AS invalid_after,
sum(ST_NPoints(t.geom)) AS vertices_before,
sum(ST_NPoints(ST_SimplifyPreserveTopology(t.geom, 0.0005))) AS vertices_after
FROM metro_tracts t GROUP BY slug;| Atlanta | 1,501 | 0 | 0 | 523,879 | 40,144 | 92% |
| Chicago | 2,282 | 0 | 0 | 447,246 | 23,022 | 95% |
| Detroit | 1,303 | 0 | 0 | 138,107 | 12,453 | 91% |
| Houston | 1,606 | 0 | 0 | 360,759 | 31,238 | 91% |
| Phoenix | 1,092 | 0 | 0 | 180,140 | 13,305 | 93% |
| Seattle | 860 | 0 | 0 | 239,686 | 20,101 | 92% |
A tolerance of 0.0005 degrees, about 50 m, removes 91% to 95% of the vertices, and no polygon is invalid before or after. ST_SimplifyPreserveTopology keeps each polygon valid on its own. It does not keep a shared edge identical in the two tracts on either side, so hairline gaps can open between neighbours. Chapter 7's own metro maps simplify all the tracts of a metro together instead, with ST_CoverageSimplify_Agg.
2. Count stores per H3 hexagon
SELECT slug, h3_latlng_to_cell(lat, lon, 8) AS cell,
count(*) FILTER (WHERE kind = 'dollar') AS dollar,
count(*) FILTER (WHERE kind = 'supermarket') AS supermarket
FROM metro_stores GROUP BY ALL;| Atlanta | 1,636 | 1,257 | 739 |
| Chicago | 1,192 | 858 | 617 |
| Detroit | 862 | 739 | 279 |
| Houston | 1,053 | 845 | 437 |
| Phoenix | 512 | 283 | 341 |
| Seattle | 330 | 98 | 300 |
Atlanta's 1,996 stores fall in 1,636 cells, so most cells hold a single store and the hexagon layer is close to the store points snapped to a grid. Only cells with a store are written, which keeps the files small, and the snapping is what lets the radius tool run without a spatial engine.
3. Check the hexagon shortcut with an exact radius query
SELECT p.slug,
count(*) FILTER (WHERE s.kind = 'dollar') AS dollar_exact,
count(*) FILTER (WHERE s.kind = 'supermarket') AS supermarket_exact
-- geom is plain GEOMETRY here: 10_maplab.sql casts it when reading the GeoJSON
FROM radius_points p JOIN stores s
ON s.slug = p.slug
AND ST_DWithin_Spheroid(s.geom::POINT_2D, p.pt::POINT_2D, 2 * 1609.344)
GROUP BY p.slug;| Atlanta | 2 | 1 | 13 | 13 | 9 |
| Chicago | 6 | 6 | 7 | 7 | 9 |
| Detroit | 7 | 6 | 0 | 0 | 5 |
| Houston | 6 | 6 | 5 | 5 | 8 |
| Phoenix | 2 | 3 | 3 | 4 | 7 |
| Seattle | 0 | 0 | 6 | 6 | 5 |
The test point in each metro is the point on surface of its densest tract, with a 2-mile radius. The hexagon sum is off by at most one store in each count, which is what a cell about 0.9 km across allows for.
Checks
| hex_holds_every_store | 6,793 | 6,793 |
| invalid_after_simplify | 0 | 0 |
| invalid_on_map | 0 | 0 |
| max_metro_bytes_under_10mb | 1 | 1 |
| no_name_column | 0 | 0 |
| radius_h3_close | 0 | 0 |
| stores_match_ch7 | 6,793 | 6,793 |
| tracts_match_ch7 | 8,644 | 8,644 |
90_export.sql stops the run with error() if any row differs. The store and tract counts on the maps match Chapter 7's metro index, every store is in exactly one hexagon, no tract is invalid after simplification, and no metro's files reach 10 MB. Store names, a free-text field, are not in any file.