Using geemap with Google Colab¶
Introduction¶
This notebook demonstrates how to use geemap with Google Colab. To use geemap and the Earth Engine Python API, you must register for an Earth Engine account and follow the instructions here to create a Cloud Project. Earth Engine is free for noncommercial and research use.
Install geemap¶
The geemap package is pre-installed in Google Colab and is updated to the latest minor or major release every few weeks. If you would like to install the latest version, you can uncomment and run the following command:
# %pip install -U geemap
Import libraries¶
Import the Earth Engine Python API.
import ee
ee.Authenticate()
ee.Initialize()
Core features¶
You can import the geemap package using the following conventions:
- To import only the core features of geemap, use:
import geemap.core as geemap
- To import all the features of geemap, use:
import geemap
Let's import the geemap package using the second option:
import geemap.core as geemap
Basemap selector¶
Select a basemap from the dropdown list and add it to the map.
m = geemap.Map()
m.add("basemap_selector")
m
Layer Manager¶
Toggle the checkbox to show or hide the layer. Drag and move the slider to change the transparency level of the layer.
m = geemap.Map(center=(40, -100), zoom=4)
dem = ee.Image("USGS/SRTMGL1_003")
states = ee.FeatureCollection("TIGER/2018/States")
vis_params = {
"min": 0,
"max": 4000,
"palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"],
}
m.add_layer(dem, vis_params, "SRTM DEM")
m.add_layer(states, {}, "US States")
m.add("layer_manager")
m
Inspector¶
Click on the map to query Earth Engine data at a specific location.
m = geemap.Map(center=(40, -100), zoom=4)
dem = ee.Image("USGS/SRTMGL1_003")
landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003")
states = ee.FeatureCollection("TIGER/2018/States")
vis_params = {
"min": 0,
"max": 4000,
"palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"],
}
m.add_layer(dem, vis_params, "SRTM DEM")
m.add_layer(
landsat7,
{"bands": ["B4", "B3", "B2"], "min": 20, "max": 200, "gamma": 2.0},
"Landsat 7",
)
m.add_layer(states, {}, "US States")
m.add("inspector")
m
m = geemap.Map(center=(40, -100), zoom=4)
dem = ee.Image("USGS/SRTMGL1_003")
vis_params = {
"min": 0,
"max": 4000,
"palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"],
}
m.add_layer(dem, vis_params, "SRTM DEM")
m.add("layer_editor", layer_dict=m.ee_layers["SRTM DEM"])
m
Multi-band image¶
m = geemap.Map(center=(40, -100), zoom=4)
landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003")
m.add_layer(
landsat7,
{"bands": ["B4", "B3", "B2"], "min": 20, "max": 200, "gamma": 2.0},
"Landsat 7",
)
m.add("layer_editor", layer_dict=m.ee_layers["Landsat 7"])
m
Feature collection¶
m = geemap.Map(center=(40, -100), zoom=4)
states = ee.FeatureCollection("TIGER/2018/States")
m.add_layer(states, {}, "US States")
m.add("layer_editor", layer_dict=m.ee_layers["US States"])
m
Draw control¶
You can draw shapes on the map using the draw control. The drawn features will be automatically converted to Earth Engine objects, which can be accessed in one of the following ways:
- To return the last drawn feature as an
ee.Geometry()
, use:m._draw_control.last_geometry
- To return all the drawn feature as an
ee.Feature()
, use:m._draw_control.last_feature
- To return all the drawn features as an
ee.FeatureCollection()
, use:m._draw_control.collection
m = geemap.Map(center=(40, -100), zoom=4)
dem = ee.Image("USGS/SRTMGL1_003")
vis_params = {
"min": 0,
"max": 4000,
"palette": "terrain",
}
m.add_layer(dem, vis_params, "SRTM DEM")
m.add("layer_manager")
m
Use the draw control to draw a polygon on the map.
geometry = m._draw_control.last_geometry
geometry
feature = m._draw_control.last_feature
feature
collection = m._draw_control.collection
collection
Clip the DEM data using the drawn polygon.
if geometry is not None:
image = dem.clipToCollection(collection)
m.layers[1].visible = False
m.add_layer(image, vis_params, "Clipped DEM")
m