Using ipyleaflet drawing tools to interact with Earth Engine data¶
Install Earth Engine API and geemap¶
Install the Earth Engine Python API and geemap. The geemap Python package is built upon the ipyleaflet and folium packages and implements several methods for interacting with Earth Engine data layers, such as Map.addLayer()
, Map.setCenter()
, and Map.centerObject()
.
The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its dependencies, including earthengine-api, folium, and ipyleaflet.
In [15]:
Copied!
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('geemap package not installed. Installing ...')
subprocess.check_call(["python", '-m', 'pip', 'install', '-U', 'geemap'])
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print('geemap package not installed. Installing ...')
subprocess.check_call(["python", '-m', 'pip', 'install', '-U', 'geemap'])
In [16]:
Copied!
import ee
import geemap
import ee
import geemap
Create an interactive map¶
The default basemap is Google Satellite
. Additional basemaps can be added using the Map.add_basemap()
function.
In [17]:
Copied!
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID') # Add Google Satellite
Map
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID') # Add Google Satellite
Map
Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…
Draw any shapes on the map above using the Draw Control.¶
In [18]:
Copied!
# Retrieves ee.Feature() of the last drawing object.
Map.draw_last_feature
# Retrieves ee.Feature() of the last drawing object.
Map.draw_last_feature
In [19]:
Copied!
# Retrieves ee.Feature() of all drawing objects.
Map.draw_features
# Retrieves ee.Feature() of all drawing objects.
Map.draw_features
Out[19]:
[]
Clipping Earth Engine Image layer with the Draw Control¶
In [20]:
Copied!
import ipywidgets as widgets
from ipyleaflet import WidgetControl
from geemap import geojson_to_ee
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID') # Add
# Add Earth Engine dataset
image = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Get basemap layers
base_layers = Map.layers
# Add Earth Engine DEM to map
Map.addLayer(image, vis_params, 'SRTM DEM')
# An empty list for storing drawing geometries
feat_list = []
# Get the DrawControl
dc = Map.draw_control
# Handle draw events
def handle_draw(self, action, geo_json):
geom = geojson_to_ee(geo_json, False)
feature = ee.Feature(geom)
feat_list.append(feature)
collection = ee.FeatureCollection(feat_list)
clip_image = image.clipToCollection(collection)
Map.layers = base_layers[:3]
Map.addLayer(clip_image, vis_params, 'SRTM DEM')
# Map.addLayer(ee.Image().paint(collection, 0, 2), {'palette': 'red'}, 'EE Geometry')
Map.addLayer(collection, {}, 'Drawing Features')
dc.on_draw(handle_draw)
# # Add a button to the map
# button = widgets.Button(description="Clear drawings")
# btn_control = WidgetControl(widget=button, position='bottomright')
# Map.add_control(btn_control)
# # Handle click event
# def on_button_clicked(b):
# dc.clear()
# button.on_click(on_button_clicked)
Map
import ipywidgets as widgets
from ipyleaflet import WidgetControl
from geemap import geojson_to_ee
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap('HYBRID') # Add
# Add Earth Engine dataset
image = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Get basemap layers
base_layers = Map.layers
# Add Earth Engine DEM to map
Map.addLayer(image, vis_params, 'SRTM DEM')
# An empty list for storing drawing geometries
feat_list = []
# Get the DrawControl
dc = Map.draw_control
# Handle draw events
def handle_draw(self, action, geo_json):
geom = geojson_to_ee(geo_json, False)
feature = ee.Feature(geom)
feat_list.append(feature)
collection = ee.FeatureCollection(feat_list)
clip_image = image.clipToCollection(collection)
Map.layers = base_layers[:3]
Map.addLayer(clip_image, vis_params, 'SRTM DEM')
# Map.addLayer(ee.Image().paint(collection, 0, 2), {'palette': 'red'}, 'EE Geometry')
Map.addLayer(collection, {}, 'Drawing Features')
dc.on_draw(handle_draw)
# # Add a button to the map
# button = widgets.Button(description="Clear drawings")
# btn_control = WidgetControl(widget=button, position='bottomright')
# Map.add_control(btn_control)
# # Handle click event
# def on_button_clicked(b):
# dc.clear()
# button.on_click(on_button_clicked)
Map
Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…
In [21]:
Copied!
# Print out the geojson of the last drawing object
dc.last_draw
# Print out the geojson of the last drawing object
dc.last_draw
Out[21]:
{'type': 'Feature', 'geometry': None}
Last update:
2022-06-27