{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.show_youtube('NwnW_qOkNRw')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add builtin legends from geemap Python package\n", "\n", "https://github.com/giswqs/geemap/blob/master/geemap/legends.py\n", "\n", "### Available builtin legends:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "legends = geemap.builtin_legends\n", "for legend in legends:\n", " print(legend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Available Land Cover Datasets in Earth Engine\n", "\n", "https://developers.google.com/earth-engine/datasets/tags/landcover" ] }, { "cell_type": "markdown", "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, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.add_basemap('HYBRID')\n", "landcover = ee.Image('USGS/NLCD/NLCD2016').select('landcover')\n", "Map.addLayer(landcover, {}, 'NLCD Land Cover')\n", "Map.add_legend(builtin_legend='NLCD')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### National Wetlands Inventory (NWI)\n", "\n", "https://www.fws.gov/wetlands/data/mapper.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.add_basemap('HYBRID')\n", "Map.add_basemap('FWS NWI Wetlands')\n", "Map.add_legend(builtin_legend='NWI')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### MODIS Land Cover Type Yearly Global 500m\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.add_basemap('HYBRID')\n", "\n", "landcover = ee.Image('MODIS/051/MCD12Q1/2013_01_01') \\\n", " .select('Land_Cover_Type_1')\n", "\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, {}, 'MODIS Land Cover')\n", "Map.add_legend(builtin_legend='MODIS/051/MCD12Q1')\n", "\n", "Map" ] }, { "cell_type": "markdown", "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 keys and colors\n", "2. Defind 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, "metadata": { "scrolled": true }, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "legend_keys = ['One', 'Two', 'Three', 'Four', 'ect']\n", " #colorS can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n", "legend_colors = ['#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3']\n", "# legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)]\n", "\n", "Map.add_legend(legend_keys=legend_keys, legend_colors=legend_colors, position='bottomleft')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define a legend dictionary" ] }, { "cell_type": "code", "execution_count": null, "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(legend_title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert an Earth Engine class table to legend\n", "\n", "For example: MCD12Q1.051 Land Cover Type Yearly Global 500m\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1" ] }, { "cell_type": "code", "execution_count": null, "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/051/MCD12Q1/2013_01_01') \\\n", " .select('Land_Cover_Type_1')\n", "Map.setCenter(6.746, 46.529, 2)\n", "Map.addLayer(landcover, {}, 'MODIS Land Cover')\n", "\n", "legend_dict = geemap.legend_from_ee(ee_class_table)\n", "Map.add_legend(legend_title=\"MODIS Global Land Cover\", legend_dict=legend_dict)\n", "\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }