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.

On this page
  1. 1. Simplify the tract outlines
  2. 2. Count stores per H3 hexagon
  3. 3. Check the hexagon shortcut with an exact radius query
  4. Checks

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.

SQL
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;
Atlanta1,50100523,87940,14492%
Chicago2,28200447,24623,02295%
Detroit1,30300138,10712,45391%
Houston1,60600360,75931,23891%
Phoenix1,09200180,14013,30593%
Seattle86000239,68620,10192%
6 rows

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

SQL
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;
Atlanta1,6361,257739
Chicago1,192858617
Detroit862739279
Houston1,053845437
Phoenix512283341
Seattle33098300
6 rows

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

SQL
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;
Atlanta2113139
Chicago66779
Detroit76005
Houston66558
Phoenix23347
Seattle00665
6 rows

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_store6,7936,793
invalid_after_simplify00
invalid_on_map00
max_metro_bytes_under_10mb11
no_name_column00
radius_h3_close00
stores_match_ch76,7936,793
tracts_match_ch78,6448,644
8 rows

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.