38 plotly
Uncomment the following line to install leafmap if needed.
# !pip install leafmap
import leafmap.plotlymap as leafmap
If you run into an error saying "FigureWidget - 'mapbox._derived' Value Error" (source), uncomment the following line and run it.
# leafmap.fix_widget_error()
Create an interactive map using default settings.
m = leafmap.Map()
m
Change default setting when creating a map.
Can be one of string from "open-street-map", "carto-positron", "carto-darkmatter", "stamen-terrain", "stamen-toner" or "stamen-watercolor" .
m = leafmap.Map(center=(40, -100), zoom=3, basemap="stamen-terrain", height=500)
m
Set map center and zoom level.
m = leafmap.Map(basemap="stamen-watercolor")
m.set_center(lat=20, lon=0, zoom=2)
m
Print out available basemaps.
# leafmap.basemaps.keys()
Add a basemap.
m = leafmap.Map()
m.add_basemap("OpenTopoMap")
m
Add XYZ tile layer.
m = leafmap.Map()
tile_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}"
m.add_tile_layer(tile_url, name="Google Satellite", attribution="Google", opacity=1.0)
m
Add a mapbox tile layer. You will need a mapbox token. The map style can be Can be "basic", "streets", "outdoors", "light", "dark", "satellite", or "satellite-streets".
import os
# os.environ["MAPBOX_TOKEN"] = "your-mapbox-token"
m = leafmap.Map()
m.add_mapbox_layer(style="streets")
m
Remove the modebar in the upper-right corner.
m = leafmap.Map(basemap="stamen-toner")
m
m.clear_controls()
Add more buttons to the modebar.
m = leafmap.Map(basemap="carto-positron")
controls = [
"drawline",
"drawopenpath",
"drawclosedpath",
"drawcircle",
"drawrect",
"eraseshape",
]
m.add_controls(controls)
m
Add Cloud Optimized GeoTIFF.
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Fire (pre-event)")
m
Add a STAC item via HTTP URL.
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="False color")
m
Add a STAC item from Microsoft Planetary Computer.
collection = "landsat-8-c2-l2"
item = "LC08_L2SP_047027_20201204_02_T1"
m = leafmap.Map()
m.add_stac_layer(
collection=collection,
item=item,
bands=["SR_B7", "SR_B5", "SR_B4"],
titiler_endpoint="pc",
)
m
Add a heat map.
url = "https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv"
m = leafmap.Map(basemap="stamen-terrain")
m.add_heatmap(
url, latitude="Latitude", longitude="Longitude", z="Magnitude", name="Earthquake"
)
m
Add a choropleth map.
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
m = leafmap.Map(basemap="stamen-terrain")
m.add_choropleth_map(url, name="Pop", z="POP_EST", colorscale="Viridis")
m