geohierarchy.chunked_h3_grid¶
Per-tile GeoParquet output for a large H3 attribute grid, never one big in-memory GeoDataFrame.
Companion to :func:geohierarchy.raster_resample.raster_to_h3_tiled -- same
motivation (Shanghai/Boston-scale OOM, one process holding the whole city's
data at once), same partition-by-H3-ancestor strategy, but for a plain
attribute table (population/access/census columns keyed by H3 cell id,
e.g. code.pipeline.read_h3_grid_table's output in CS_transitLOS) rather
than a raster.
Unlike :func:raster_to_h3_tiled, this needs no halo/buffer: that
function's halo exists because a raster PIXEL can straddle the true
geometric boundary between two H3 cells, so a tile computing "which cell
does this pixel belong to, and how much of it" needs to see its neighbors'
candidate cells to split correctly. Here, every row already IS one specific,
whole H3 cell (no splitting question), and a cell's parent at any coarser
resolution is a pure id-arithmetic fact (h3.cell_to_parent), not a
geometric computation that could disagree near a boundary. Grouping rows by
their tile_resolution ancestor is therefore an EXACT partition on its
own -- every fine cell belongs to exactly one tile, full stop, matching the
same reasoning already used (and tested) for the in-memory chunk loops in
CS_transitLOS.code.pipeline._chunked_h3_grid_join/_resample_h3_chunked.
The one place a real geometric halo genuinely would matter -- a street
segment or census polygon that spans a tile boundary -- belongs to a
DIFFERENT stage (isochrones / census join) than this module, which only
builds hexagon geometry + writes out already-computed attributes.
partition_h3_table(df, tile_resolution, h3_col='h3_cell')
¶
Group df's rows by each row's H3 ancestor cell at tile_resolution.
Exact, non-overlapping, non-duplicating partition (see module docstring)
-- every row of df appears in exactly one output value, under its own
tile id key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table with one H3 cell id per row. |
required |
tile_resolution
|
int
|
H3 resolution of the tiles (coarser than the cells). |
required |
h3_col
|
str
|
Column holding the H3 cell ids. |
'h3_cell'
|
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
|
build_h3_grid_chunked(df, output_dir, tile_resolution=5, h3_col='h3_cell', max_workers=None)
¶
Write one hexagon-geometry GeoParquet file PER tile_resolution H3 tile.
This is the chunked replacement for building one whole-city
GeoDataFrame (code.pipeline._add_h3_grid / h3_ops.to_gdf called
once on everything) -- at Shanghai's scale (~25.7M resolution-11 cells)
that single call needs 20GB+ resident simultaneously; this instead
partitions by each row's tile_resolution ancestor (exact, no halo
needed -- see module docstring) and builds+writes each tile's own
(small, bounded) geometry in its own worker process, so peak memory is
bounded by one tile's size times max_workers, never the whole city's.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Geometry-free H3 attribute table (e.g.
|
required |
output_dir
|
str
|
Directory to write |
required |
tile_resolution
|
int
|
Coarser H3 resolution defining the partition
(default 5, matching |
5
|
h3_col
|
str
|
Name of the cell-id column. |
'h3_cell'
|
max_workers
|
Optional[int]
|
|
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Paths of every tile file written, one per non-empty tile. Never |
List[str]
|
merged -- callers (map tiling, cross-chunk stats) work from this |
List[str]
|
list of files directly. |
read_h3_grid_chunked_columns(tile_paths, columns)
¶
Lazily read only columns (never geometry) from every per-tile GeoParquet, as one scan.
The polars-lazy cross-chunk aggregation primitive requested for
citywide statistics (Distribution/Regression/ANOVA panels, ...): a
pl.scan_parquet glob-style multi-file scan restricted to exactly the
non-geometry columns a given statistic needs, so a stats computation
over ALL tiles never materializes any tile's geometry and never holds
more than one tile's worth of the needed columns in memory at a time
(polars' streaming engine reads/aggregates file-by-file lazily).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile_paths
|
Sequence[str]
|
Per-tile GeoParquet paths, e.g.
|
required |
columns
|
Sequence[str]
|
Attribute column names to read (must exclude |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
An unevaluated |
LazyFrame
|
row-order-independent (callers |
LazyFrame
|
filter/aggregate they need). |