{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "# How to add a draggable legend to the map" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap.foliumap as geemap" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# geemap.update_package()" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Add a builtin legend to the map" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "legends = geemap.builtin_legends\n", "for legend in legends:\n", " print(legend)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Available Land Cover Datasets in Earth Engine\n", "\n", "https://developers.google.com/earth-engine/datasets/tags/landcover" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "### National Land Cover Database (NLCD)\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "landcover = ee.Image(\"USGS/NLCD/NLCD2016\").select(\"landcover\")\n", "Map.addLayer(landcover, {}, \"NLCD Land Cover\")\n", "\n", "Map.add_legend(builtin_legend=\"NLCD\")\n", "\n", "Map.addLayerControl()\n", "\n", "Map" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### MODIS Land Cover Type Yearly Global 500m\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "landcover = ee.Image(\"MODIS/006/MCD12Q1/2013_01_01\").select(\"LC_Type1\")\n", "igbpLandCoverVis = {\n", " \"min\": 1.0,\n", " \"max\": 17.0,\n", " \"palette\": [\n", " \"05450a\",\n", " \"086a10\",\n", " \"54a708\",\n", " \"78d203\",\n", " \"009900\",\n", " \"c6b044\",\n", " \"dcd159\",\n", " \"dade48\",\n", " \"fbff13\",\n", " \"b6ff05\",\n", " \"27ff87\",\n", " \"c24f44\",\n", " \"a5a5a5\",\n", " \"ff6d4c\",\n", " \"69fff8\",\n", " \"f9ffa4\",\n", " \"1c0dff\",\n", " ],\n", "}\n", "\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, igbpLandCoverVis, \"MODIS Land Cover\")\n", "\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, igbpLandCoverVis, \"MODIS Land Cover\")\n", "Map.add_legend(builtin_legend=\"MODIS/006/MCD12Q1\")\n", "\n", "Map.addLayerControl()\n", "\n", "Map" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## Add customized legends for Earth Engine data\n", "\n", "There are three ways you can add customized legends for Earth Engine data\n", "\n", "1. Define legend labels and colors\n", "2. Define legend dictionary\n", "3. Convert Earth Engine class table to legend dictionary\n", "\n", "### Define legend keys and colors" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "labels = [\"One\", \"Two\", \"Three\", \"Four\", \"etc\"]\n", "# color can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n", "colors = [\"#8DD3C7\", \"#FFFFB3\", \"#BEBADA\", \"#FB8072\", \"#80B1D3\"]\n", "# colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68, 123)]\n", "\n", "Map.add_legend(title=\"Legend\", labels=labels, colors=colors)\n", "Map" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "### Define a legend dictionary" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "legend_dict = {\n", " \"11 Open Water\": \"466b9f\",\n", " \"12 Perennial Ice/Snow\": \"d1def8\",\n", " \"21 Developed, Open Space\": \"dec5c5\",\n", " \"22 Developed, Low Intensity\": \"d99282\",\n", " \"23 Developed, Medium Intensity\": \"eb0000\",\n", " \"24 Developed High Intensity\": \"ab0000\",\n", " \"31 Barren Land (Rock/Sand/Clay)\": \"b3ac9f\",\n", " \"41 Deciduous Forest\": \"68ab5f\",\n", " \"42 Evergreen Forest\": \"1c5f2c\",\n", " \"43 Mixed Forest\": \"b5c58f\",\n", " \"51 Dwarf Scrub\": \"af963c\",\n", " \"52 Shrub/Scrub\": \"ccb879\",\n", " \"71 Grassland/Herbaceous\": \"dfdfc2\",\n", " \"72 Sedge/Herbaceous\": \"d1d182\",\n", " \"73 Lichens\": \"a3cc51\",\n", " \"74 Moss\": \"82ba9e\",\n", " \"81 Pasture/Hay\": \"dcd939\",\n", " \"82 Cultivated Crops\": \"ab6c28\",\n", " \"90 Woody Wetlands\": \"b8d9eb\",\n", " \"95 Emergent Herbaceous Wetlands\": \"6c9fb8\",\n", "}\n", "\n", "landcover = ee.Image(\"USGS/NLCD/NLCD2016\").select(\"landcover\")\n", "Map.addLayer(landcover, {}, \"NLCD Land Cover\")\n", "\n", "Map.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n", "\n", "Map.addLayerControl()\n", "\n", "Map" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "### Convert an Earth Engine class table to legend\n", "\n", "For example: MCD12Q1 Land Cover Type Yearly Global 500m\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "ee_class_table = \"\"\"\n", "\n", "Value\tColor\tDescription\n", "0\t1c0dff\tWater\n", "1\t05450a\tEvergreen needleleaf forest\n", "2\t086a10\tEvergreen broadleaf forest\n", "3\t54a708\tDeciduous needleleaf forest\n", "4\t78d203\tDeciduous broadleaf forest\n", "5\t009900\tMixed forest\n", "6\tc6b044\tClosed shrublands\n", "7\tdcd159\tOpen shrublands\n", "8\tdade48\tWoody savannas\n", "9\tfbff13\tSavannas\n", "10\tb6ff05\tGrasslands\n", "11\t27ff87\tPermanent wetlands\n", "12\tc24f44\tCroplands\n", "13\ta5a5a5\tUrban and built-up\n", "14\tff6d4c\tCropland/natural vegetation mosaic\n", "15\t69fff8\tSnow and ice\n", "16\tf9ffa4\tBarren or sparsely vegetated\n", "254\tffffff\tUnclassified\n", "\n", "\"\"\"\n", "\n", "landcover = ee.Image(\"MODIS/006/MCD12Q1/2013_01_01\").select(\"LC_Type1\")\n", "igbpLandCoverVis = {\n", " \"min\": 1.0,\n", " \"max\": 17.0,\n", " \"palette\": [\n", " \"05450a\",\n", " \"086a10\",\n", " \"54a708\",\n", " \"78d203\",\n", " \"009900\",\n", " \"c6b044\",\n", " \"dcd159\",\n", " \"dade48\",\n", " \"fbff13\",\n", " \"b6ff05\",\n", " \"27ff87\",\n", " \"c24f44\",\n", " \"a5a5a5\",\n", " \"ff6d4c\",\n", " \"69fff8\",\n", " \"f9ffa4\",\n", " \"1c0dff\",\n", " ],\n", "}\n", "\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, igbpLandCoverVis, \"MODIS Land Cover\")\n", "\n", "legend_dict = geemap.legend_from_ee(ee_class_table)\n", "Map.add_legend(title=\"MODIS Global Land Cover\", legend_dict=legend_dict)\n", "\n", "Map.addLayerControl()\n", "\n", "Map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }