06 legend
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import leafmap
import leafmap
List available built-in legends.
In [3]:
Copied!
legends = leafmap.builtin_legends
for legend in legends:
print(legend)
legends = leafmap.builtin_legends
for legend in legends:
print(legend)
NLCD NWI MODIS/051/MCD12Q1 GLOBCOVER JAXA/PALSAR MODIS/006/MCD12Q1 Oxford AAFC/ACI COPERNICUS/CORINE/V20/100m COPERNICUS/Landcover/100m/Proba-V/Global USDA/NASS/CDL ESA_WorldCover
National Land Cover Database (NLCD)
https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD
Create an interactive map.
In [4]:
Copied!
Map = leafmap.Map()
Map = leafmap.Map()
Add a WMS layer and built-in legend to the map.
In [5]:
Copied!
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
Map.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
Map.add_legend(builtin_legend="NLCD")
Map
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
Map.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
Map.add_legend(builtin_legend="NLCD")
Map
Out[5]:
Add National Wetlands Inventory to the map.
In [6]:
Copied!
Map = leafmap.Map(google_map="HYBRID")
url1 = "https://www.fws.gov/wetlands/arcgis/services/Wetlands/MapServer/WMSServer?"
Map.add_wms_layer(
url1, layers="1", format="image/png", transparent=True, name="NWI Wetlands Vector"
)
url2 = "https://www.fws.gov/wetlands/arcgis/services/Wetlands_Raster/ImageServer/WMSServer?"
Map.add_wms_layer(
url2, layers="0", format="image/png", transparent=True, name="NWI Wetlands Raster"
)
Map.add_legend(builtin_legend="NWI")
Map
Map = leafmap.Map(google_map="HYBRID")
url1 = "https://www.fws.gov/wetlands/arcgis/services/Wetlands/MapServer/WMSServer?"
Map.add_wms_layer(
url1, layers="1", format="image/png", transparent=True, name="NWI Wetlands Vector"
)
url2 = "https://www.fws.gov/wetlands/arcgis/services/Wetlands_Raster/ImageServer/WMSServer?"
Map.add_wms_layer(
url2, layers="0", format="image/png", transparent=True, name="NWI Wetlands Raster"
)
Map.add_legend(builtin_legend="NWI")
Map
Out[6]:
Add custom legends
There are two ways you can add custom legends:
- Define legend labels and colors
- Define legend dictionary
Define legend keys and colors
In [7]:
Copied!
Map = leafmap.Map()
labels = ["One", "Two", "Three", "Four", "etc"]
# color can be defined using either hex code or RGB (0-255, 0-255, 0-255)
colors = ["#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3"]
# colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68, 123)]
Map.add_legend(title="Legend", labels=labels, colors=colors)
Map
Map = leafmap.Map()
labels = ["One", "Two", "Three", "Four", "etc"]
# color can be defined using either hex code or RGB (0-255, 0-255, 0-255)
colors = ["#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3"]
# colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68, 123)]
Map.add_legend(title="Legend", labels=labels, colors=colors)
Map
Out[7]:
Define a legend dictionary.
In [8]:
Copied!
Map = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
Map.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
legend_dict = {
"11 Open Water": "466b9f",
"12 Perennial Ice/Snow": "d1def8",
"21 Developed, Open Space": "dec5c5",
"22 Developed, Low Intensity": "d99282",
"23 Developed, Medium Intensity": "eb0000",
"24 Developed High Intensity": "ab0000",
"31 Barren Land (Rock/Sand/Clay)": "b3ac9f",
"41 Deciduous Forest": "68ab5f",
"42 Evergreen Forest": "1c5f2c",
"43 Mixed Forest": "b5c58f",
"51 Dwarf Scrub": "af963c",
"52 Shrub/Scrub": "ccb879",
"71 Grassland/Herbaceous": "dfdfc2",
"72 Sedge/Herbaceous": "d1d182",
"73 Lichens": "a3cc51",
"74 Moss": "82ba9e",
"81 Pasture/Hay": "dcd939",
"82 Cultivated Crops": "ab6c28",
"90 Woody Wetlands": "b8d9eb",
"95 Emergent Herbaceous Wetlands": "6c9fb8",
}
Map.add_legend(title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map
Map = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
Map.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
legend_dict = {
"11 Open Water": "466b9f",
"12 Perennial Ice/Snow": "d1def8",
"21 Developed, Open Space": "dec5c5",
"22 Developed, Low Intensity": "d99282",
"23 Developed, Medium Intensity": "eb0000",
"24 Developed High Intensity": "ab0000",
"31 Barren Land (Rock/Sand/Clay)": "b3ac9f",
"41 Deciduous Forest": "68ab5f",
"42 Evergreen Forest": "1c5f2c",
"43 Mixed Forest": "b5c58f",
"51 Dwarf Scrub": "af963c",
"52 Shrub/Scrub": "ccb879",
"71 Grassland/Herbaceous": "dfdfc2",
"72 Sedge/Herbaceous": "d1d182",
"73 Lichens": "a3cc51",
"74 Moss": "82ba9e",
"81 Pasture/Hay": "dcd939",
"82 Cultivated Crops": "ab6c28",
"90 Woody Wetlands": "b8d9eb",
"95 Emergent Herbaceous Wetlands": "6c9fb8",
}
Map.add_legend(title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map
Out[8]: