Skip to content

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]

{tile id: rows of df inside that tile}; empty for an empty table.

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. code.pipeline.read_h3_grid_table's output), one row per H3 cell, with an h3_col id column.

required
output_dir str

Directory to write tile_<id>.parquet files into (created if missing). Each file is a standalone GeoParquet with hexagon geometry + every attribute column df had.

required
tile_resolution int

Coarser H3 resolution defining the partition (default 5, matching raster_to_h3_tiled's default and geohierarchy's other tiling machinery).

5
h3_col str

Name of the cell-id column.

'h3_cell'
max_workers Optional[int]

ProcessPoolExecutor worker count (default: os.cpu_count()). Pass 1 to run serially in-process (useful for tests / tiny fixtures where pool startup overhead would dominate, and for environments where spawn-context pools aren't available, e.g. some sandboxed CI).

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. build_h3_grid_chunked's return value.

required
columns Sequence[str]

Attribute column names to read (must exclude geometry; geometry is WKB-encoded and not meaningful to scan this way).

required

Returns:

Type Description
LazyFrame

An unevaluated polars.LazyFrame over every tile's columns union,

LazyFrame

row-order-independent (callers .collect() after any further

LazyFrame

filter/aggregate they need).