Skip to content

Duckdb layer

image image image

Add DuckDB Vector Tiles

Uncomment the following line to install leafmap if needed.

1
# %pip install "leafmap[duckdb]"
1
import leafmap.maplibregl as leafmap

If you are running in a remote Jupyter environment, you need to configure leafmap with your JupyterHub URL. Uncomment the following line and replace https://your-jupyterhub-domain.com with your JupyterHub URL.

1
# leafmap.configure_jupyterhub("https://your-jupyterhub-domain.com")

Download a sample GeoParquet dataset from Source Coop.

1
2
url = "https://data.source.coop/giswqs/nwi/wetlands/RI_Wetlands.parquet"
fileapth = leafmap.download_file(url)

Example 1: Load data from vector file(creates temporary database)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
m = leafmap.Map(style="liberty", height="600px")
m.add_basemap("Esri.WorldImagery")
m.add_duckdb_layer(
    data=fileapth,  # Supports GeoParquet, GeoJSON, and GeoPackage, etc.
    layer_name="wetlands",
    layer_type="fill",
    paint={"fill-color": "#3388ff"},
    opacity=0.5,
    fit_bounds=True,
    use_view=False,  # For remote datasets, set use_view to True to avoid copying data to local
    min_zoom=None,  # For large datasets, set min_zoom to None to avoid zooming out too much
    quiet=False,
)
m
1
m.close_db_connections()

Example 2: Load from existing DuckDB database (no data loading)

1
2
url = "https://data.gishub.org/duckdb/nyc_data.db.zip"
leafmap.download_file(url)
1
db_path = "nyc_data.db"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#
m = leafmap.Map(
    center=[-73.9031255, 40.7127753], zoom=9, style="liberty", height="600px"
)
m.add_duckdb_layer(
    database_path=db_path,
    table_name="nyc_neighborhoods",
    layer_type="fill",
    paint={"fill-color": "#ff0000"},
    opacity=0.5,
    fit_bounds=False,
    src_crs="EPSG:26918",
    quiet=True,
)
m
1
m.close_db_connections()