Introduction to Google Earth Engine in Python
Notebook: https://geemap.org/workshops/G4G_2023
Earth Engine: https://earthengine.google.com
Geemap: https://geemap.org
Introduction (10 mins)
This notebook is for the workshop presented at the Geo for Good Summit 2023. Check out this link for more information about the workshop.
Abstract
This workshop provides an introduction to cloud-based geospatial analysis using the Earth Engine Python API. Attendees will learn the basics of Earth Engine data types and how to visualize, analyze, and export Earth Engine data in a Jupyter environment with geemap. In addition, attendees will learn how to develop and deploy interactive Earth Engine web apps with Python. Through practical examples and hands-on exercises, attendees will enhance their learning experience. During each hands-on session, attendees will walk through Jupyter Notebook examples on Google Colab with the instructors. At the end of each session, they will complete a hands-on exercise to apply the knowledge they have learned.
Prerequisites
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. To test whether you can use authenticate the Earth Engine Python API, please run this notebook on Google Colab.
It is recommended that attendees have a basic understanding of Python and Jupyter Notebook.
Familiarity with the Earth Engine JavaScript API is not required but will be helpful.
Attendees can use Google Colab to follow this workshop without installing anything on their computer.
Introduction to Earth Engine and geemap (15 mins)
Earth Engine is free for noncommercial and research use. For more than a decade, Earth Engine has enabled planetary-scale Earth data science and analysis by nonprofit organizations, research scientists, and other impact users.
With the launch of Earth Engine for commercial use, commercial customers will be charged for Earth Engine services. However, Earth Engine will remain free of charge for noncommercial use and research projects. Nonprofit organizations, academic institutions, educators, news media, Indigenous governments, and government researchers are eligible to use Earth Engine free of charge, just as they have done for over a decade.
The geemap Python package is built upon the Earth Engine Python API and open-source mapping libraries. It allows Earth Engine users to interactively manipulate, analyze, and visualize geospatial big data in a Jupyter environment. Since its creation in April 2020, geemap has received over 2,900 GitHub stars and is being used by over 1,000 projects on GitHub.
Google Colab and Earth Engine Python API authentication (5 mins)
Change Colab dark theme
Currently, ipywidgets does not work well with Colab dark theme. Some of the geemap widgets may not display properly in Colab dark theme.It is recommended that you change Colab to the light theme.

Install geemap
The geemap package is pre-installed in Google Colab and is updated to the latest minor or major release every few weeks. Some optional dependencies of geemap being used by this notebook are not pre-installed in Colab. Uncomment the following line to install geemap and some optional dependencies.
Import libraries
Import the earthengine-api and geemap.
Authenticate and initialize Earth Engine
You will need to create a Google Cloud Project and enable the Earth Engine API for the project. You can find detailed instructions here.
Creating interactive maps
Let's create an interactive map using the ipyleaflet plotting backend. The geemap.Map class inherits the ipyleaflet.Map class. Therefore, you can use the same syntax to create an interactive map as you would with ipyleaflet.Map.
To display it in a Jupyter notebook, simply ask for the object representation:
To customize the map, you can specify various keyword arguments, such as center ([lat, lon]), zoom, width, and height. The default width is 100%, which takes up the entire cell width of the Jupyter notebook. The height argument accepts a number or a string. If a number is provided, it represents the height of the map in pixels. If a string is provided, the string must be in the format of a number followed by px, e.g., 600px.
To hide a control, set control_name to False, e.g., draw_ctrl=False.
Adding basemaps
There are several ways to add basemaps to a map. You can specify the basemap to use in the basemap keyword argument when creating the map. Alternatively, you can add basemap layers to the map using the add_basemap method. Geemap has hundreds of built-in basemaps available that can be easily added to the map with only one line of code.
Create a map by specifying the basemap to use as follows. For example, the Esri.WorldImagery basemap represents the Esri world imagery basemap.
You can add as many basemaps as you like to the map. For example, the following code adds the OpenTopoMap basemap to the map above:
Print out the first 10 basemaps:
You can also change basemaps interactively using the basemap GUI.
Using Earth Engine data (30 mins)
Earth Engine data types (Image, ImageCollection, Geometry, Feature, FeatureCollection)
Earth Engine objects are server-side objects rather than client-side objects, which means that they are not stored locally on your computer. Similar to video streaming services (e.g., YouTube, Netflix, and Hulu), which store videos/movies on their servers, Earth Engine data are stored on the Earth Engine servers. We can stream geospatial data from Earth Engine on-the-fly without having to download the data just like we can watch videos from streaming services using a web browser without having to download the entire video to your computer.
Image: the fundamental raster data type in Earth Engine.
ImageCollection: a stack or time-series of images.
Geometry: the fundamental vector data type in Earth Engine.
Feature: a Geometry with attributes.
FeatureCollection: a set of features.
Image
Raster data in Earth Engine are represented as Image objects. Images are composed of one or more bands and each band has its own name, data type, scale, mask and projection. Each image has metadata stored as a set of properties.
Loading Earth Engine images
Visualizing Earth Engine images
ImageCollection
An ImageCollection is a stack or sequence of images. An ImageCollection can be loaded by passing an Earth Engine asset ID into the ImageCollection constructor. You can find ImageCollection IDs in the Earth Engine Data Catalog.
Loading image collections
For example, to load the image collection of the Sentinel-2 surface reflectance:
Visualizing image collections
To visualize an Earth Engine ImageCollection, we need to convert an ImageCollection to an Image by compositing all the images in the collection to a single image representing, for example, the min, max, median, mean or standard deviation of the images. For example, to create a median value image from a collection, use the collection.median() method. Let's create a median image from the Sentinel-2 surface reflectance collection:
Filtering image collections
FeatureCollection
A FeatureCollection is a collection of Features. A FeatureCollection is analogous to a GeoJSON FeatureCollection object, i.e., a collection of features with associated properties/attributes. Data contained in a shapefile can be represented as a FeatureCollection.
Loading feature collections
The Earth Engine Data Catalog hosts a variety of vector datasets (e.g,, US Census data, country boundaries, and more) as feature collections. You can find feature collection IDs by searching the data catalog. For example, to load the TIGER roads data by the U.S. Census Bureau:
Filtering feature collections
Visualizing feature collections
Earth Engine Data Catalog
The Earth Engine Data Catalog hosts a variety of geospatial datasets. As of October 2023, the catalog contains over 1,000 datasets with a total size of over 80 petabytes. Some notable datasets include: Landsat, Sentinel, MODIS, NAIP, etc. For a complete list of datasets in CSV or JSON formats, see the Earth Engine Datasets List.
Searching for datasets
The Earth Engine Data Catalog is searchable. You can search datasets by name, keyword, or tag. For example, enter "elevation" in the search box will filter the catalog to show only datasets containing "elevation" in their name, description, or tags. 52 datasets are returned for this search query. Scroll down the list to find the NASA SRTM Digital Elevation 30m dataset. On each dataset page, you can find the following information, including Dataset Availability, Dataset Provider, Earth Engine Snippet, Tags, Description, Code Example, and more (see {numref}ch03_gee_srtm). One important piece of information is the Image/ImageCollection/FeatureCollection ID of each dataset, which is essential for accessing the dataset through the Earth Engine JavaScript or Python APIs.

Using the datasets module
Converting Earth Engine JavaScripts to Python
Find some Earth Engine JavaScript code that you want to convert to Python. For example, you can grab some sample code from the Earth Engine Documentation.
Exercise 1 - Creating cloud-free imagery
Create a cloud-free imagery of Texas for the year of 2022. You can use either Landsat 9 or Sentinel-2 imagery. Relevant Earth Engine assets:

Break 1 (10 mins)
Visualizing Earth Engine data (30 mins)
Geemap Inspector tool, plotting tool, interactive GUI for data visualization
Using the inspector tool
Using the plotting tool
Set plotting options for Landsat.
Set plotting options for Hyperion.
Legends, color bars, and labels
Built-in legends
Add NLCD WMS layer and legend to the map.
Add NLCD Earth Engine layer and legend to the map.
Custom legends
Add a custom legend by specifying the colors and labels.
Add a custom legend by specifying a dictionary of colors and labels.
Creating color bars
Add a horizontal color bar.
Add a vertical color bar.
Make the color bar background transparent.
Split-panel map and linked maps
Split-panel maps
Create a split map with basemaps. Note that ipyleaflet has a bug with the SplitControl. You can't pan the map, which should be resolved in the next ipyleaflet release.
Create a split map with Earth Engine layers.
Linked maps
Create a 2x2 linked map for visualizing Sentinel-2 imagery with different band combinations. Note that this feature does not work properly with Colab. Panning one map would not pan other maps.
Timeseries inspector and time slider
Timeseries inspector
Check the available years of NLCD.
Create a timeseries inspector for NLCD. Note that ipyleaflet has a bug with the SplitControl. You can't pan the map, which should be resolved in the next ipyleaflet release.
Time slider
Note that this feature may not work properly with Colab. Restart Colab runtime if the time slider does not work.
Create a map for visualizing MODIS vegetation data.
Create a map for visualizing weather data.
Visualizing Sentinel-2 imagery
Exercise 2 - Creating land cover maps with a legend
Create a split map for visualizing NLCD land cover change in Texas between 2001 and 2019. Add the NLCD legend to the map. Relevant Earth Engine assets:

Analyzing Earth Engine data (30 mins)
Image descriptive statistics
Use a sample Landsat image.
Check image properties.
Check image property values.
Get specific image properties.
Get image properties with easy-to-read time format.
Compute image descriptive statistics.
Zonal statistics
Zonal statistics
Add Earth Engine data to the map.
Compute zonal statistics. In this case, we compute the mean elevation of each state and export the results to a CSV file.
Display the csv file as a table.
Compute zonal statistics of mean spectral values of each state.
Zonal statistics by group
Compute zonal statistics by group. In this case, we compute the area of each land cover type in each state and export the results to a CSV file.
Compute zonal statistics by group and convert the area unit from square meters to square kilometers.
Calculate the percentage of each land cover type in each state.
Zonal statistics with two images
The zonal statistics by zone algorithm is similar to the zonal statistics by group algorithm, but it takes an image as the zone input instead of a feature collection.
Computer the mean elevation of each land cover type.
Compute the standard deviation of each land cover type.
Exercise 3 - Zonal statistics
Find out which state has the highest mean temperature on in the United States on June 28, 2023. Relevant Earth Engine assets:

Coordinate grids and fishnets
Creating coordinate grids
Create a latitudinal grid with a 5-degree interval.
Create a longitudinal grid with a 5-degree interval.
Create a rectangular grid with a 10-degree interval.
Creating fishnets
Create a fishnet based on an Earth Engine geometry.
Use the drawing tools to draw a polygon on the map above. If no polygon is drawn, the default polygon will be used.
Create a fishnet based on a user-drawn polygon with a 2-degree interval.
Create a new map.
Draw a polygon on the map.
Create a fishnet based on a user-drawn polygon with specified number of rows and columns.
Land use and land cover change analysis
Forest cover mapping
We will use the Hansen Global Forest Change v1.10 (2000-2022) dataset.
Select the imagery for 2000.
Select the imagery for 2022.
Select the tree cover imagery for 2000.
Extract tree cover 2000 by using the threshold of 10%.
Forest loss and gain mapping
Visualize forest loss.
Compare forest loss and gain.
Zonal statistics by country
Compute zonal statistics to find out which country has the largest forest area in 2000.
Add a country boundary layer to the map.
Compute zonal statistics by country.
Create a pie chart to visualize the forest area by country.
Create a bar chart to visualize the forest area by country.
Calculate the forest loss area by country.
Create a bar chart to visualize the forest loss area by country.
Create a bar chart to visualize the forest loss area by country.
Exercise 4 - Analyzing forest cover gain and loss
Find out which US state has the largest forest gain and loss between 2000 and 2022. Create pie charts and bar charts to show the results. Relevant Earth Engine assets:

Break 2 (10 mins)
Exporting Earth Engine data (30 mins)
Exporting images
Add a Landsat image to the map.
Add a rectangle to the map.
To local drive
Check image projection.
Specify region, crs, and crs_transform.
To Google Drive
Exporting image collections
To local drive
To Google Drive
Exporting feature collections
To local drive
To Google Drive
Exercise 5 - Exporting images by a fishnet
Create a fishnet with a 4-degree interval based on the extent of [-112.5439, 34.0891, -85.0342, 49.6858]. Use the fishnet to download the Landsat 7 image tiles by the fishnet using the geemap.download_ee_image_tiles() and geemap.download_ee_image_tiles_parallel() functions. Relevant Earth Engine assets:
ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')

Creating satellite timelapse animations (30 mins)
Creating satellite timeseries
Use the Harmonized Sentinel-2 MSI dataset to create a timeseries for a given location.
Specify the location of interest and date range.
Create an annual composite.
Display the timeseries.
Creating satellite timelapse animations
NAIP timelapse
Landsat timelapse
Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used.
Sentinel-2 timelapse
Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used.
MODIS timelapse
MODIS vegetation indices
MODIS temperature
GOES timelapse
Exercise 6 - Creating timelapse animations
Use the geemap timelapse GUI to create a timelapse animation for any location of your choice. Share the timelapse on social media and use the hashtagd such as #EarthEngine and #geemap. See this example.

Break 3 (10 mins)
Building interactive web apps (30 mins)
This section is optional. We might not have enough time to cover this section.
Follow the instructions here to build an interactive Earth Engine web app with Solara and geemap. You need to sign up for a free Hugging Face account to create the web app. It is free and easy to sign up.
Exercise 7 - Building an interactive web app for visualizing land cover change
After following the instructions above, you should have a web app that looks like this:

The web app URL should look like this: https://giswqs-solara-geemap.hf.space/split-map.