{ "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/giswqs/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/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n", "\n", "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)." ] }, { "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'])\n", "\n", "# Checks whether this notebook is running on Google Colab\n", "try:\n", " import google.colab\n", " import geemap.eefolium as emap\n", "except:\n", " import geemap as emap\n", "\n", "# Authenticates and initializes Earth Engine\n", "import ee\n", "\n", "try:\n", " ee.Initialize()\n", "except Exception as e:\n", " ee.Authenticate()\n", " ee.Initialize() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an interactive map \n", "The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/giswqs/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 = emap.Map(center=[40,-100], zoom=4)\n", "Map.add_basemap('ROADMAP') # Add Google Map\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 = emap.Map(center=[40,-100], zoom=4)\n", "Map.add_basemap('ROADMAP') # 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", "# 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", "# Handle draw events\n", "def handle_draw(self, action, geo_json):\n", "\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", "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": { "anaconda-cloud": {}, "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": 1 }