transitlos.map.pop_chunks¶
On-demand static population chunks for the map editor's client-side access recompute.
The scenario editor (build.py's editor section, Phase 4) recomputes
accessibility_score entirely in the browser, from a plain static file
server -- no server-side logic at all. That recompute needs, for every H3
res-11 cell near a newly drawn/edited stop:
(h3_cell, lat, lng, population, baseline level_of_service)
Boston's metro grid is 1.6M res-11 cells, so inlining all of it into
map.html is not an option (~100 MB of JSON). Instead this module
pre-partitions the grid into many small static JSON files -- the same
general idea as the vector tiles the rest of the map already serves, but far
simpler, because the recompute is a straight-line buffer operation
(explicit user instruction: "just do simple buffer operations, not through
the street network, but just straight lines, using the h3 cells with res
11") and therefore needs only a point + two numbers per cell, never a
hexagon polygon.
Partitioning scheme: a plain lat/lon degree grid, NOT H3 parents.¶
A coarse H3 parent (res 6/7) would be the "natural" key, but the client
would then need an H3 implementation in JavaScript just to work out which
chunk covers a given point -- a new third-party dependency on a page that
otherwise has none of its own. A fixed lat/lon degree grid is computable in
the browser with a single Math.floor, so the client stays dependency-free:
key = f"{floor(lat / CHUNK_DEG)}_{floor(lon / CHUNK_DEG)}"
CHUNK_DEG = 0.04 deg is ~4.4 km north-south and ~3.3 km east-west at
Boston's latitude (~14 km^2). A res-11 cell is ~2150 m^2, so a fully
populated chunk holds at most a few thousand rows -- a couple hundred KB of
JSON -- and the 2 km buffer around a single stop (the real pipeline's own
walk_distance_steps[-1] outer cutoff, which the JS mirrors) touches only
about 2x2 chunks. A route drawn anywhere in the metro therefore downloads a
small local slice, never the whole metro.
Only chunks that actually contain populated cells are written, and an
index.json lists every key that exists -- so the client never fires 404s
at empty ocean/forest.
Row format is a compact array-of-arrays rather than array-of-objects, which roughly halves the byte count for identical information:
{"key": "1063_-1772", "deg": 0.04, "res": 11,
"cells": [["8b2a...ffff", 42.3512, -71.0634, 118.4, 0.5213], ...]}
# h3_cell, lat, lng, pop, level_of_service
chunk_key(lat, lon, deg=POP_CHUNK_DEG)
¶
The chunk key covering (lat, lon) -- mirrored exactly in the editor JS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude. |
required |
lon
|
float
|
Longitude. |
required |
deg
|
float
|
Chunk size (degrees). |
POP_CHUNK_DEG
|
build_population_chunks(h3_table, out_dir, deg=POP_CHUNK_DEG, h3_column='h3_cell', population_column='population', access_column='level_of_service', min_population=0.0, resolution=11)
¶
Write the per-chunk population/access files plus their index.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h3_table
|
Any
|
Any object exposing the needed columns as sequences --
a Polars DataFrame (what |
required |
out_dir
|
str
|
Directory to write into (created if missing). Existing
|
required |
deg
|
float
|
Chunk size in degrees. Must match the client's
|
POP_CHUNK_DEG
|
h3_column
|
str
|
H3 cell id column name. |
'h3_cell'
|
population_column
|
str
|
Population column name. |
'population'
|
access_column
|
str
|
Access (level of service) column name. |
'level_of_service'
|
min_population
|
float
|
Cells at/below this population are dropped. They
carry zero weight in every consumer (population-weighted access,
|
0.0
|
resolution
|
int
|
H3 resolution recorded in each chunk's metadata (the grid's own native resolution; not re-derived per cell). |
11
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A summary dict: `{"n_chunks", "n_cells", "bytes", "deg", |
Dict[str, Any]
|
"resolution", "dir"}`. |
build_population_chunks_from_parquet(parquet_path, out_dir=None, **kwargs)
¶
Convenience wrapper: read a cached h3_grid.parquet and chunk it.
Reads only the three columns needed (h3_cell, population,
level_of_service) -- never the WKB geometry column, which on Boston's grid
is ~4 GB once decoded and is entirely unnecessary here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parquet_path
|
str
|
Path of |
required |
out_dir
|
Optional[str]
|
Output folder; |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
The chunk manifest. |
build_geoid_chunks(h3_cells, lats, lngs, geoids, out_dir, level='blockgroup', deg=POP_CHUNK_DEG)
¶
Write {h3_cell: GEOID} chunk files (same grid as build_population_chunks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h3_cells
|
Any
|
H3 cell ids. |
required |
lats
|
Any
|
Latitude of each cell's own centroid, in degrees (parallel to |
required |
lngs
|
Any
|
Longitude of each cell's own centroid, in degrees (parallel to |
required |
geoids
|
Any
|
Parallel sequence of GEOID strings (or |
required |
out_dir
|
str
|
Directory to write into (existing |
required |
level
|
str
|
Census level name recorded in each chunk's metadata. |
'blockgroup'
|
deg
|
float
|
Chunk size in degrees; must match |
POP_CHUNK_DEG
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Summary dict: |
build_geoid_chunks_from_h3_and_census(h3_table, census_gdf, out_dir, id_col='GEOID', level='blockgroup', deg=POP_CHUNK_DEG, h3_column='h3_cell')
¶
Convenience wrapper: join an h3 grid table onto a census polygon layer, then chunk it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h3_table
|
Any
|
Object exposing |
required |
census_gdf
|
Any
|
Census polygon GeoDataFrame (e.g. |
required |
out_dir
|
str
|
Directory to write chunk files into. |
required |
id_col
|
str
|
GEOID column name on |
'GEOID'
|
level
|
str
|
See |
'blockgroup'
|
deg
|
float
|
See |
POP_CHUNK_DEG
|
h3_column
|
str
|
H3 cell id column name on |
'h3_cell'
|