{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", "
View source on GitHubNotebook Viewer Run in Google Colab
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using ipyleaflet drawing tools to interact with Earth Engine data\n", "\n", "* [Video demo](https://i.imgur.com/SzauIsZ.gifv)\n", "\n", "## Install Earth Engine API and geemap\n", "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/gee-community/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n", "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/gee-community/geemap#dependencies), including earthengine-api, folium, and ipyleaflet." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Installs geemap package\n", "import subprocess\n", "\n", "try:\n", " import geemap\n", "except ImportError:\n", " print('geemap package not installed. Installing ...')\n", " subprocess.check_call([\"python\", '-m', 'pip', 'install', '-U', 'geemap'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an interactive map \n", "The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap('HYBRID') # Add Google Satellite\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Draw any shapes on the map above using the Draw Control." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Retrieves ee.Feature() of the last drawing object.\n", "Map.draw_last_feature" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Retrieves ee.Feature() of all drawing objects.\n", "Map.draw_features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clipping Earth Engine Image layer with the Draw Control" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "from ipyleaflet import WidgetControl\n", "from geemap import geojson_to_ee\n", "\n", "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap('HYBRID') # Add\n", "\n", "# Add Earth Engine dataset\n", "image = ee.Image('USGS/SRTMGL1_003')\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Get basemap layers\n", "base_layers = Map.layers\n", "\n", "# Add Earth Engine DEM to map\n", "Map.addLayer(image, vis_params, 'SRTM DEM')\n", "\n", "# An empty list for storing drawing geometries\n", "feat_list = []\n", "\n", "# Get the DrawControl\n", "dc = Map.draw_control\n", "\n", "\n", "# Handle draw events\n", "def handle_draw(self, action, geo_json):\n", " geom = geojson_to_ee(geo_json, False)\n", " feature = ee.Feature(geom)\n", " feat_list.append(feature)\n", " collection = ee.FeatureCollection(feat_list)\n", " clip_image = image.clipToCollection(collection)\n", "\n", " Map.layers = base_layers[:3]\n", " Map.addLayer(clip_image, vis_params, 'SRTM DEM')\n", " # Map.addLayer(ee.Image().paint(collection, 0, 2), {'palette': 'red'}, 'EE Geometry')\n", " Map.addLayer(collection, {}, 'Drawing Features')\n", "\n", "\n", "dc.on_draw(handle_draw)\n", "\n", "# # Add a button to the map\n", "# button = widgets.Button(description=\"Clear drawings\")\n", "# btn_control = WidgetControl(widget=button, position='bottomright')\n", "# Map.add_control(btn_control)\n", "\n", "# # Handle click event\n", "# def on_button_clicked(b):\n", "# dc.clear()\n", "\n", "# button.on_click(on_button_clicked)\n", "\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Print out the geojson of the last drawing object\n", "dc.last_draw" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }