Skip to content

geohierarchy.maps.folium.tiles

Vector tile generation using the fastest available methods.

This module provides the default tile generation implementation: - freestiler for GeoParquet files (10-100x faster than pure Python) - PMTiles output for optimal static hosting

For backward compatibility with the old API, this module wraps the fast implementation from fast_tiles.py.

The old slow implementation (H3J/H3T) is available in h3_slow.py.

generate_pmtiles_from_geoparquet(input_path, output_path, layer_name, min_zoom=0, max_zoom=14, property_cols=None, tile_format='mvt', **kwargs)

Generate PMTiles directly from a GeoParquet file.

This is the FASTEST method for geometry data - no Python geometry processing, the Rust engine reads the GeoParquet directly.

For H3 cells with WKB geometry, this is the recommended approach. No geometry computation is needed - uses existing geometry from the file.

Parameters:

Name Type Description Default
input_path Union[str, Path]

Path to GeoParquet file

required
output_path Union[str, Path]

Output .pmtiles file path

required
layer_name str

Name of the layer in the PMTiles

required
min_zoom int

Minimum zoom level (default: 0)

0
max_zoom int

Maximum zoom level (default: 14)

14
property_cols Optional[Sequence[str]]

Columns to include as properties (default: all non-geometry)

None
tile_format str

'mvt' or 'mlt' (default: 'mvt' for compatibility)

'mvt'
**kwargs Any

Additional arguments passed to freestile_file()

{}

Returns:

Type Description
Path

Path to the generated .pmtiles file

Example

generate_pmtiles_from_geoparquet( "census.parquet", "census.pmtiles", layer_name="census", max_zoom=12 )

For H3 cells with WKB geometry

generate_pmtiles_from_geoparquet( "h3_grid.parquet", # Must have geometry column with WKB "h3_grid.pmtiles", layer_name="h3_grid", min_zoom=0, max_zoom=14 )

generate_pmtiles_from_polars(pl_df, output_path, layer_name, geometry_col='geometry', min_zoom=0, max_zoom=14, tile_format='mvt', **kwargs)

Generate PMTiles from a Polars DataFrame with geometry.

For DataFrames that already have geometry columns (as WKB or similar).

Parameters:

Name Type Description Default
pl_df

Polars DataFrame with geometry

required
output_path Union[str, Path]

Output .pmtiles file path

required
layer_name str

Name of the layer

required
geometry_col str

Name of the geometry column

'geometry'
min_zoom int

Minimum zoom level

0
max_zoom int

Maximum zoom level

14
tile_format str

'mvt' or 'mlt'

'mvt'
**kwargs Any

Additional arguments

{}

Returns:

Type Description
Path

Path to the generated .pmtiles file

generate_xyz_tiles_from_geoparquet(input_path, output_dir, layer_name, min_zoom=0, max_zoom=14, property_cols=None, tile_format='mvt', extract_pmtiles=True, overwrite=True, quiet=False, **kwargs)

Generate XYZ .pbf tile directory from a GeoParquet file.

This is the main entry point for fast XYZ tile generation that works with folium + Leaflet.VectorGrid. It: 1. Uses freestiler to generate a PMTiles archive (FAST) 2. Extracts XYZ .pbf directories from the PMTiles (if extract_pmtiles=True)

The output directory structure is: {output_dir}/{layer_name}/{z}/{x}/{y}.pbf which is exactly what folium + Leaflet.VectorGrid expects.

For H3 cells with WKB geometry, this is the recommended approach. No geometry computation is needed - uses existing geometry from the file.

Parameters:

Name Type Description Default
input_path Union[str, Path]

Path to GeoParquet file

required
output_dir Union[str, Path]

Root output directory for XYZ tiles

required
layer_name str

Name of the layer (used for subdirectory name)

required
min_zoom int

Minimum zoom level (default: 0)

0
max_zoom int

Maximum zoom level (default: 14)

14
property_cols Optional[Sequence[str]]

Columns to include as properties

None
tile_format str

'mvt' or 'mlt' (default: 'mvt')

'mvt'
extract_pmtiles bool

If True (default), extract XYZ from PMTiles. If False, just generate PMTiles file and return its path.

True
overwrite bool

Overwrite existing output

True
quiet bool

Suppress progress messages

False
**kwargs Any

Additional arguments passed to freestile_file()

{}

Returns:

Type Description
Path

Path to the output directory containing XYZ tiles

Path

If extract_pmtiles=False, returns path to .pmtiles file

Example

Generate XYZ tiles for use with folium

generate_xyz_tiles_from_geoparquet( "census.parquet", "tiles", layer_name="census", max_zoom=12 )

Creates: tiles/census/0/0/0.pbf, tiles/census/1/0/0.pbf, etc.

For H3 cells with WKB geometry

generate_xyz_tiles_from_geoparquet( "h3_grid.parquet", "tiles", layer_name="h3_grid", min_zoom=0, max_zoom=14 )

build_fast_hierarchy_map(hierarchy, levels=None, tiles_dir='tiles', min_zoom=0, max_zoom=25, h3_geometry_type='polygon', **kwargs)

Build tiles for a GeoHierarchy using the fastest available method.

This function intelligently selects the best method for each level: - For levels with WKB geometry: Uses freestiler directly on GeoParquet (FASTEST) - For H3 cell levels WITHOUT geometry: Uses freestiler_h3 to compute geometry - All outputs are single .pmtiles files

Note: H3J/H3T formats are NOT used by default. They don't scale well. If you need H3J/H3T, import from h3_slow.py explicitly.

Parameters:

Name Type Description Default
hierarchy

A GeoHierarchy instance

required
levels Optional[Sequence[str]]

Level names to process (default: all)

None
tiles_dir str

Output directory for tiles

'tiles'
min_zoom int

Global minimum zoom

0
max_zoom int

Global maximum zoom

25
h3_geometry_type str

'polygon' or 'point' for H3 cells without geometry

'polygon'
**kwargs Any

Additional arguments

{}

Returns:

Type Description
Dict[str, Path]

Dictionary mapping level names to output .pmtiles paths

generate_h3j_from_h3_indices(pl_df, output_path, h3_index_col='h3_cell', property_cols=None, resolution=None, **kwargs)

Generate H3J file from Polars DataFrame with H3 indices.

H3J stores only the H3 indices and properties - NO geometry. Client-side rendering generates hexagon polygons from indices.

WARNING: This format is SLOW and doesn't scale well for large datasets. Use generate_pmtiles_from_geoparquet() with WKB geometry instead.

Parameters:

Name Type Description Default
pl_df

Polars DataFrame with H3 index column

required
output_path Union[str, Path]

Output .h3j file path

required
h3_index_col str

Name of the H3 index column

'h3_cell'
property_cols Optional[Sequence[str]]

Properties to include (default: all except h3_index_col)

None
resolution Optional[int]

H3 resolution (for metadata)

None
**kwargs Any

Additional metadata

{}

Returns:

Type Description
Path

Path to the generated .h3j file

Example

generate_h3j_from_h3_indices( pl_df, "h3_data.h3j", h3_index_col="h3_cell", property_cols=["population", "access_score"] )

generate_h3t_tiles_from_h3_indices(pl_df, output_dir, h3_index_col='h3_cell', property_cols=None, min_zoom=0, max_zoom=15, resolution=None, buffer_tiles=1)

Generate H3T tiled format from H3 indices using Polars.

H3T is the tiled version of H3J - each tile file contains only the H3 cells that intersect that tile.

WARNING: This format is SLOW and creates MANY files (doesn't scale well). Use generate_pmtiles_from_geoparquet() with WKB geometry instead.

Parameters:

Name Type Description Default
pl_df

Polars DataFrame with H3 index column

required
output_dir Union[str, Path]

Directory for {z}/{x}/{y}.h3t files

required
h3_index_col str

Name of the H3 index column

'h3_cell'
property_cols Optional[Sequence[str]]

Properties to include

None
min_zoom int

Minimum tile zoom

0
max_zoom int

Maximum tile zoom

15
resolution Optional[int]

H3 resolution

None
buffer_tiles int

Number of buffer tiles around each H3 cell

1

Returns:

Type Description
Path

Path to the output directory

prep_level_gdf(gdf, id_col, property_cols)

Reproject/trim a level's geometry to the columns needed for tiling.

This is kept for backward compatibility with the old API. For new code, use Polars DataFrames directly with generate_pmtiles_from_polars().

Parameters:

Name Type Description Default
gdf GeoDataFrame

The level's geometries.

required
id_col str

Id column kept on every feature.

required
property_cols Iterable[str]

Other columns kept on the features.

required

Returns:

Type Description
Tuple[GeoDataFrame, List[str]]

(gdf in EPSG:4326 with only the kept columns, list of kept columns).

write_level_tiles(gdf, level_name, tiles_dir, min_zoom, max_zoom, id_col, property_cols, buffer_frac=0.02, use_xyz=False, extract_xyz_from_pmtiles=True)

Generate vector tiles for a GeoDataFrame level using fast methods.

This is the main entry point for tile generation. It will: - Always generate .pmtiles files using freestiler (FASTEST, RECOMMENDED) - If use_xyz=True, also extract XYZ .pbf directories from the PMTiles for backward compatibility - Fall back to old Python-based method if freestiler not available

Note: PMTiles (.pmtiles) is now the PRIMARY format. For backward compatibility, XYZ PBF directories can be extracted from PMTiles by setting use_xyz=True. This ensures the existing Leaflet.VectorGrid code continues to work.

For H3 cells with WKB geometry, freestiler uses the existing geometry directly. For H3 cells without geometry, use generate_pmtiles_from_h3_with_centers() to avoid computing polygon boundaries.

Parameters:

Name Type Description Default
gdf GeoDataFrame

Level geometry (EPSG:4326), typically hierarchy.get_level(name).

required
level_name str

Name of the level; also used as the MVT layer name and the top-level directory under tiles_dir.

required
tiles_dir str

Root output directory for all levels' tiles.

required
min_zoom int

Minimum zoom (inclusive) to generate tiles for.

required
max_zoom int

Maximum zoom (inclusive) to generate tiles for.

required
id_col str

Feature id column to keep in tile properties.

required
property_cols Iterable[str]

Additional attribute columns to keep in tile properties (styling/popups read these client-side).

required
buffer_frac float

Tile buffer as a fraction of tile width (default: 2%). Note: Not used with freestiler-based generation.

0.02
use_xyz bool

If True, also extract XYZ .pbf directories from PMTiles for backward compatibility. If False (default), only generate .pmtiles files.

False
extract_xyz_from_pmtiles bool

If True (default) and use_xyz=True, extract XYZ from PMTiles.

True

Returns:

Type Description
List[Path]

List of paths to every generated tile file/directory.

List[Path]

If use_xyz=True: List with one Path to the layer's XYZ directory (per chunk, if chunked -- see below) plus every .pmtiles path.

List[Path]

If use_xyz=False: List of .pmtiles path(s). Normally a single {level_name}.pmtiles; for a level with more rows than H3_CHUNK_ROW_THRESHOLD, a list of {level_name}/chunk_{h3res4cell}.pmtiles paths instead (see module docstring / H3_CHUNK_ROW_THRESHOLD) -- freestiler exposes no merge/append primitive, so many small per-chunk files is the final on-disk format, not an intermediate one.

write_level_zoom_tiles(work, level_name, tiles_dir, z, keep_cols, buffer_frac=0.02)

Tile one level's already-prepped geometry at a single zoom level.

For backward compatibility with the old parallel processing API. In the fast implementation, we don't use this as we generate all zooms at once.

Parameters:

Name Type Description Default
work GeoDataFrame

Geometries prepared by :func:prep_level_gdf.

required
level_name str

Name of the level (tile sub-folder).

required
tiles_dir str

Root folder of the tiles.

required
z int

Zoom level to write.

required
keep_cols List[str]

Columns written on each feature.

required
buffer_frac float

Tile buffer as a fraction of the tile size.

0.02

Returns:

Type Description
List[Path]

Paths of the written tiles.