{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/127_create_legend.ipynb)\n", "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/geemap-binder)\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install -U geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a built-in draggable legend. Specify the `output` parameter to save the legend as an HTML file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.create_legend(\n", " title='NLCD Land Cover Type',\n", " builtin_legend='NLCD',\n", " draggable=True,\n", " output='NLCD_legend.html',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a built-in non-draggable legend. If the `output` parameter is not specified, the legend will be returned as an HTML string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html = geemap.create_legend(\n", " title='NLCD Land Cover Type',\n", " builtin_legend='NLCD',\n", " draggable=False,\n", " position='bottomright',\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widget = geemap.show_html(html)\n", "widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widget.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a custom legend." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "legend_dict = {\n", " \"10 Trees\": \"006400\",\n", " \"20 Shrubland\": \"ffbb22\",\n", " \"30 Grassland\": \"ffff4c\",\n", " \"40 Cropland\": \"f096ff\",\n", " \"50 Built-up\": \"fa0000\",\n", " \"60 Barren / sparse vegetation\": \"b4b4b4\",\n", " \"70 Snow and ice\": \"f0f0f0\",\n", " \"80 Open water\": \"0064c8\",\n", " \"90 Herbaceous wetland\": \"0096a0\",\n", " \"95 Mangroves\": \"00cf75\",\n", " \"100 Moss and lichen\": \"fae6a0\",\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.create_legend(\n", " title='ESA Land Cover Type',\n", " legend_dict=legend_dict,\n", " draggable=False,\n", " output='ESA_legend.html',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Customize the legend by specifying the `style` parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = geemap.Map()\n", "m.add_basemap('ESA WorldCover 2021')\n", "\n", "style = {\n", " 'position': 'fixed',\n", " 'z-index': '9999',\n", " 'border': '2px solid grey',\n", " 'background-color': 'rgba(255, 255, 255, 0.8)',\n", " 'border-radius': '10px',\n", " 'padding': '5px',\n", " 'font-size': '14px',\n", " 'bottom': '20px',\n", " 'right': '5px',\n", "}\n", "\n", "m.add_legend(\n", " title='ESA Land Cover Type', legend_dict=legend_dict, draggable=False, style=style\n", ")\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a legend to the map." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = geemap.Map()\n", "m.add_basemap('ESA WorldCover 2021')\n", "m.add_legend(title='ESA Land Cover Type', builtin_legend='ESA_WorldCover')\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add legends to a split-view map. Make sure the `draggable` parameter is set to `False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(\n", " center=[40, -100],\n", " zoom=4,\n", " draw_control=False,\n", " measure_control=False,\n", " scale_control=False,\n", ")\n", "m.split_map(left_layer='ESA WorldCover 2021', right_layer='NLCD 2019 CONUS Land Cover')\n", "m.add_legend(\n", " title='ESA Land Cover Type',\n", " builtin_legend='ESA_WorldCover',\n", " draggable=False,\n", " position='bottomleft',\n", " style={'bottom': '5px'},\n", ")\n", "m.add_legend(\n", " title='NLCD Land Cover Type',\n", " builtin_legend='NLCD',\n", " draggable=False,\n", " position='bottomright',\n", ")\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }