Skip to content

Geopandas

image image image

Add a GeoPandas GeoDataFrame to the map

The following notebook demonstrates how to add a GeoPandas GeoDataFrame to the map.

Uncomment the following line to install leafmap if needed.

1
# %pip install "leafmap[maplibre]"
1
2
import geopandas as gpd
import leafmap.maplibregl as leafmap

To run this notebook, you will need an API key from MapTiler. Once you have the API key, you can uncomment the following code block and replace YOUR_API_KEY with your actual API key. Then, run the code block code to set the API key as an environment variable.

1
2
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
m = leafmap.Map(center=[-100, 40], zoom=3, style="streets")
url = "https://github.com/opengeos/datasets/releases/download/us/us_states.geojson"
gdf = gpd.read_file(url)
paint = {
    "fill-color": "#3388ff",
    "fill-opacity": 0.8,
    "fill-outline-color": "#ffffff",
}
m.add_gdf(gdf, layer_type="fill", name="States", paint=paint)
m