geohierarchy.maps.folium.fast_tiles¶
Fast vector tile generation using freestiler (Rust-powered).
This module provides the FASTEST methods for vector tile generation: - freestile_file() for GeoParquet files (10-100x faster) - freestile_query() for DuckDB-based tiling - H3 PMTiles generation from WKB geometry in GeoParquet
All outputs are PMTiles (.pmtiles) for optimal static hosting.
For H3 cells WITH WKB geometry: Use generate_pmtiles_from_geoparquet() directly. For H3 cells WITHOUT geometry: Use generate_pmtiles_from_h3_with_centers().
The old slow H3J/H3T implementation is in h3_slow.py (NOT RECOMMENDED).
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_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 )
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_pmtiles_from_h3_with_centers(pl_df, output_path, h3_index_col='h3_cell', layer_name='h3_cells', min_zoom=0, max_zoom=14, base_zoom=12, tile_format='mvt', geometry_type='polygon', **kwargs)
¶
Generate PMTiles from H3 indices by converting to polygons/points.
This creates a multi-resolution hexagon grid where: - Low zooms: Coarse hexagons (aggregated) - High zooms: Finer hexagons or individual cells - base_zoom and above: Individual H3 cells as polygons or points
Use this ONLY if your H3 data does NOT have WKB geometry. If your data HAS WKB geometry, use generate_pmtiles_from_geoparquet() instead (faster).
Note: This function computes geometry for each H3 cell, which can be slow for large datasets. For best performance, pre-compute WKB geometry and save as GeoParquet, then use generate_pmtiles_from_geoparquet().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pl_df
|
Polars DataFrame with H3 indices |
required | |
output_path
|
Union[str, Path]
|
Output .pmtiles file |
required |
h3_index_col
|
str
|
Name of H3 index column |
'h3_cell'
|
layer_name
|
str
|
Layer name |
'h3_cells'
|
min_zoom
|
int
|
Minimum zoom |
0
|
max_zoom
|
int
|
Maximum zoom |
14
|
base_zoom
|
int
|
Zoom where individual cells appear |
12
|
tile_format
|
str
|
'mvt' or 'mlt' |
'mvt'
|
geometry_type
|
str
|
'polygon' for hexagons, 'point' for centroids |
'polygon'
|
**kwargs
|
Any
|
Additional arguments to freestiler_h3 |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to .pmtiles file |
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 |