Uncomment the following line to install geemap if needed.
In [ ]:
Copied!
# !pip install geemap
# !pip install geemap
How to add a draggable legend to the map¶
In [ ]:
Copied!
import ee
import geemap.foliumap as geemap
import ee
import geemap.foliumap as geemap
In [ ]:
Copied!
# geemap.update_package()
# geemap.update_package()
Add a builtin legend to the map¶
In [ ]:
Copied!
legends = geemap.builtin_legends
for legend in legends:
print(legend)
legends = geemap.builtin_legends
for legend in legends:
print(legend)
Available Land Cover Datasets in Earth Engine¶
https://developers.google.com/earth-engine/datasets/tags/landcover
National Land Cover Database (NLCD)¶
https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD
In [ ]:
Copied!
Map = geemap.Map()
landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover")
Map.addLayer(landcover, {}, "NLCD Land Cover")
Map.add_legend(builtin_legend="NLCD")
Map.addLayerControl()
Map
Map = geemap.Map()
landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover")
Map.addLayer(landcover, {}, "NLCD Land Cover")
Map.add_legend(builtin_legend="NLCD")
Map.addLayerControl()
Map
MODIS Land Cover Type Yearly Global 500m¶
https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1
In [ ]:
Copied!
Map = geemap.Map()
landcover = ee.Image("MODIS/006/MCD12Q1/2013_01_01").select("LC_Type1")
igbpLandCoverVis = {
"min": 1.0,
"max": 17.0,
"palette": [
"05450a",
"086a10",
"54a708",
"78d203",
"009900",
"c6b044",
"dcd159",
"dade48",
"fbff13",
"b6ff05",
"27ff87",
"c24f44",
"a5a5a5",
"ff6d4c",
"69fff8",
"f9ffa4",
"1c0dff",
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
Map.add_legend(builtin_legend="MODIS/006/MCD12Q1")
Map.addLayerControl()
Map
Map = geemap.Map()
landcover = ee.Image("MODIS/006/MCD12Q1/2013_01_01").select("LC_Type1")
igbpLandCoverVis = {
"min": 1.0,
"max": 17.0,
"palette": [
"05450a",
"086a10",
"54a708",
"78d203",
"009900",
"c6b044",
"dcd159",
"dade48",
"fbff13",
"b6ff05",
"27ff87",
"c24f44",
"a5a5a5",
"ff6d4c",
"69fff8",
"f9ffa4",
"1c0dff",
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
Map.add_legend(builtin_legend="MODIS/006/MCD12Q1")
Map.addLayerControl()
Map
In [ ]:
Copied!
Map = geemap.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 = geemap.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
Define a legend dictionary¶
In [ ]:
Copied!
Map = geemap.Map()
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",
}
landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover")
Map.addLayer(landcover, {}, "NLCD Land Cover")
Map.add_legend(title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map.addLayerControl()
Map
Map = geemap.Map()
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",
}
landcover = ee.Image("USGS/NLCD/NLCD2016").select("landcover")
Map.addLayer(landcover, {}, "NLCD Land Cover")
Map.add_legend(title="NLCD Land Cover Classification", legend_dict=legend_dict)
Map.addLayerControl()
Map
Convert an Earth Engine class table to legend¶
For example: MCD12Q1 Land Cover Type Yearly Global 500m
https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1
In [ ]:
Copied!
Map = geemap.Map()
ee_class_table = """
Value Color Description
0 1c0dff Water
1 05450a Evergreen needleleaf forest
2 086a10 Evergreen broadleaf forest
3 54a708 Deciduous needleleaf forest
4 78d203 Deciduous broadleaf forest
5 009900 Mixed forest
6 c6b044 Closed shrublands
7 dcd159 Open shrublands
8 dade48 Woody savannas
9 fbff13 Savannas
10 b6ff05 Grasslands
11 27ff87 Permanent wetlands
12 c24f44 Croplands
13 a5a5a5 Urban and built-up
14 ff6d4c Cropland/natural vegetation mosaic
15 69fff8 Snow and ice
16 f9ffa4 Barren or sparsely vegetated
254 ffffff Unclassified
"""
landcover = ee.Image("MODIS/006/MCD12Q1/2013_01_01").select("LC_Type1")
igbpLandCoverVis = {
"min": 1.0,
"max": 17.0,
"palette": [
"05450a",
"086a10",
"54a708",
"78d203",
"009900",
"c6b044",
"dcd159",
"dade48",
"fbff13",
"b6ff05",
"27ff87",
"c24f44",
"a5a5a5",
"ff6d4c",
"69fff8",
"f9ffa4",
"1c0dff",
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
legend_dict = geemap.legend_from_ee(ee_class_table)
Map.add_legend(title="MODIS Global Land Cover", legend_dict=legend_dict)
Map.addLayerControl()
Map
Map = geemap.Map()
ee_class_table = """
Value Color Description
0 1c0dff Water
1 05450a Evergreen needleleaf forest
2 086a10 Evergreen broadleaf forest
3 54a708 Deciduous needleleaf forest
4 78d203 Deciduous broadleaf forest
5 009900 Mixed forest
6 c6b044 Closed shrublands
7 dcd159 Open shrublands
8 dade48 Woody savannas
9 fbff13 Savannas
10 b6ff05 Grasslands
11 27ff87 Permanent wetlands
12 c24f44 Croplands
13 a5a5a5 Urban and built-up
14 ff6d4c Cropland/natural vegetation mosaic
15 69fff8 Snow and ice
16 f9ffa4 Barren or sparsely vegetated
254 ffffff Unclassified
"""
landcover = ee.Image("MODIS/006/MCD12Q1/2013_01_01").select("LC_Type1")
igbpLandCoverVis = {
"min": 1.0,
"max": 17.0,
"palette": [
"05450a",
"086a10",
"54a708",
"78d203",
"009900",
"c6b044",
"dcd159",
"dade48",
"fbff13",
"b6ff05",
"27ff87",
"c24f44",
"a5a5a5",
"ff6d4c",
"69fff8",
"f9ffa4",
"1c0dff",
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, igbpLandCoverVis, "MODIS Land Cover")
legend_dict = geemap.legend_from_ee(ee_class_table)
Map.add_legend(title="MODIS Global Land Cover", legend_dict=legend_dict)
Map.addLayerControl()
Map