Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/docs/workshops/FOSS4GNA_2024.ipynb
2313 views
Kernel: Python 3 (ipykernel)

image

Open Source Pipeline to Integrate Drone and Satellite Data

This notebook is designed for workshop presented at the FOSS4G NA 2024 conference on September 9, 2024. Click the "Open in Colab" button above to run this notebook interactively in the cloud.

Acknowledgments

This training material was partially supported by the National Agricultural Producers Data Cooperative (USDA award 2023-77039-41033; Sub-award 25-6231-0428-008).

Introduction

Recent advances in drone technology have revolutionized the remote sensing community by providing means to collect fine spatial and high temporal resolutions at affordable costs. As people are gaining access to increasingly larger volumes of drone and satellite geospatial data products, there is a growing need to extract relevant information from the vast amount of freely available geospatial data. However, the lack of specialized software packages tailored for processing such data makes it challenging to develop transdisciplinary research collaboration around them. This workshop aims to bridge the gap between big geospatial data and research scientists by providing training on an open-source online platform for managing big drone data known as Data to Science. Additionally, attendees will be introduced to powerful Python packages, namely Geemap and Leafmap, designed for the seamless integration and analysis of drone and satellite images in various applications. By participating in this workshop, attendees will acquire the skills necessary to efficiently search, visualize, and analyze geospatial data within a Jupyter environment, even with minimal coding experience. The workshop provides a hands-on learning experience through practical examples and interactive exercises, enabling participants to enhance their proficiency and gain valuable insights into leveraging geospatial data for various research purposes.

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 short course without installing anything on their computer.

Agenda

The main topics to be covered in this workshop include:

  • Create interactive maps

  • Visualize drone imagery from D2S

  • Visualize Earth Engine data

  • Explore Earth Engine Data Catalogs

  • Analyze Earth Engine data

  • Create timelapse animations

Introduction to Earth Engine and geemap

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 3,400 GitHub stars and is being used by over 3,000 projects on GitHub.

Google Colab and Earth Engine Python API authentication

image

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 code block to install geemap and some optional dependencies.

# %pip install -U "geemap[workshop]" leafmap d2spy

Note that some geemap features may not work properly in the Google Colab environmennt. If you are familiar with Anaconda or Miniconda, it is recommended to create a new conda environment to install geemap and its optional dependencies on your local computer.

conda create -n gee python=3.11 conda activate gee conda install -c conda-forge mamba mamba install -c conda-forge geemap pygis pip install d2spy

Import libraries

Import the necessary libraries for this workshop.

import ee import geemap import leafmap

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.

Login to Earth Engine JavaScript Code Editor at https://code.earthengine.google.com and click on th profile icon at the top right. Remember the project ID listed in the dialog that appears. Uncomment the following code block and replace YOUR_PROJECT_ID with your project ID.

# os.environ["EE_PROJECT_ID"] = "YOUR-PROJECT-ID"

Then, run the code block to authenticate and initialize the Earth Engine Python API.

geemap.ee_initialize(project=None)

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.

m = geemap.Map()

To display it in a Jupyter notebook, simply ask for the object representation:

m

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.

m = geemap.Map(center=[40, -100], zoom=4, height="600px") m

To hide a control, set control_name to False, e.g., draw_ctrl=False.

m = geemap.Map(data_ctrl=False, toolbar_ctrl=False, draw_ctrl=False) m

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.

m = geemap.Map(basemap="Esri.WorldImagery") m

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:

m.add_basemap("OpenTopoMap")

You can also add an XYZ tile layer to the map.

basemap_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" m.add_tile_layer(basemap_url, name="Hybrid", attribution="Google")

You can also change basemaps interactively using the basemap GUI.

m = geemap.Map() m.add("basemap_selector") m

Visualizing Drone Imagery from D2S

The Data to Science (D2S) platform (https://ps2.d2s.org) hosts a large collection of drone imagery that can be accessed through the D2S API (https://py.d2s.org). To visualize drone imagery from D2S, you need to sign up for a free account on the D2S platform and obtain an API key.

Login method #1

Login procedure using d2spy Auth module.

from d2spy.auth import Auth from d2spy.workspace import Workspace # Replace with URL to a D2S instance d2s_url = "https://ps2.d2s.org" # Login with your email address auth = Auth(d2s_url) auth.login(email="[email protected]")
# Check for API key user = auth.get_current_user() api_key = user.api_access_token if not api_key: print( "No API key. Please request one from the D2S profile page and re-run this cell." )

Login method #2

Login and connect to your D2S workspace in one go using the d2spy

from d2spy.workspace import Workspace # Replace with URL to a D2S instance d2s_url = "https://ps2.d2s.org" # Login and connect to workspace with your email address workspace = Workspace.connect(d2s_url, "[email protected]")
# Check for API key api_key = workspace.api_key if not api_key: print( "No API key. Please request one from the D2S profile page and re-run this cell." )
import os from datetime import date os.environ["D2S_API_KEY"] = api_key

Choosing a project to work with

The Workspace get_projects method will retrieve a collection of the projects your account can currently access on the D2S instance.

# Workspace session (Uncomment below line if using Login Method #1) # workspace = Workspace(d2s_url, auth.session) # Get list of all your projects projects = workspace.get_projects() projects

The projects variable is a ProjectCollection. The collection can be filtered by either the project descriptions or titles using the methods filter_by_title or filter_by_name.

# Example of creating new collection of only projects with the keyword "Test" in the title filtered_projects = projects.filter_by_title("Purdue") print(filtered_projects)

Now you can choose a specific project to work with. In this case, the filtered projects returned only one project, so we will use that project.

project = filtered_projects[0]

get_project_boundary method of the Project class will retrieve a GeoJSON object of the project boundary.

# Get project boundary as Python dictionary in GeoJSON structure project_boundary = project.get_project_boundary() print(project_boundary)

The Project get_flights method will retrieve a list of flights associated with the project.

# Get list of all flights for a project flights = project.get_flights() # Print first flight object (if one exists) for flight in flights: print(flight)

The flights variable is a FlightCollection. The collection can be filtered by the acquisition date using the method filter_by_date. This method will return all flights with an acquisition date between the provided start and end dates.

# Example of creating new collection of only flights from Jan 2015 - Dec 2024 filtered_flights = flights.filter_by_date( start_date=date(2015, 1, 1), end_date=date(2024, 12, 31) ) for flight in filtered_flights: print(flight)

Now, we can choose a flight from the filtered flight. Let's choose the 2018 flight.

flight = filtered_flights[0]

The Flight get_data_products method will retrieve a list of data products associated with the flight.

# Get list of data products from a flight data_products = flight.get_data_products() for data_product in data_products: print(data_product)

The data_products variable is a DataProductCollection. The collection can be filtered by data type using the method filter_by_data_type. This method will return all data products that match the requested data type.

# Example of creating new collection of data products with the "ortho" data type ortho_data_products = data_products.filter_by_data_type("ortho") print(ortho_data_products)

Now we can grab the ortho URL to display it using geemap.

m = geemap.Map() basemap_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" m.add_tile_layer(basemap_url, name="Google Hybrid", attribution="Google", shown=False) ortho_data = ortho_data_products[0] ortho_url = ortho_data.url ortho_url = leafmap.d2s_tile(ortho_url) m.add_cog_layer(ortho_url, name="RGB Imagery") m

Similarly, you can visualize the Digital Surface Model (DSM) from D2S using the code below.

# Example of creating new collection of data products with the "dsm" data type dsm_data_products = data_products.filter_by_data_type("dsm") print(dsm_data_products)
dsm_data = dsm_data_products[0] dsm_url = dsm_data.url dsm_url = leafmap.d2s_tile(dsm_url) m.add_cog_layer(dsm_url, colormap_name="terrain", name="DSM")

Add a colorbar to the map.

vis_params = {"palette": "terrain", "min": 125, "max": 250} m.add_colorbar(vis_params, label="Elevation (m)")

Add the project boundary to the map.

m.add_geojson(project_boundary, layer_name="Purdue Campus")

Add LiDAR DEM hillshade, DTM, and NDHM to the map.

m = geemap.Map() basemap_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" m.add_tile_layer(basemap_url, name="Google Hybrid", attribution="Google", shown=False) hs_data_products = data_products.filter_by_data_type("DTM HS") hs_data = hs_data_products[0] hs_url = hs_data.url hs_url = leafmap.d2s_tile(hs_url) dtm_data_products = data_products.filter_by_data_type("DTM") dtm_data = dtm_data_products[0] dtm_url = dtm_data.url dtm_url = leafmap.d2s_tile(dtm_url) ndhm_data_products = data_products.filter_by_data_type("NDHM") ndhm_data = ndhm_data_products[0] ndhm_url = ndhm_data.url ndhm_url = leafmap.d2s_tile(ndhm_url) m.add_cog_layer(hs_url, name="Hillshade") m.add_cog_layer(dtm_url, colormap_name="terrain", name="DTM", opacity=0.5) m.add_cog_layer(ndhm_url, colormap_name="terrain", name="CHM", shown=False) m.add_colorbar(vis_params, label="Elevation (m)") m

Retrieve the Ortho data product for the 2023 flight.

flight_2023 = filtered_flights[1] data_products = flight_2023.get_data_products() ortho_data_products = data_products.filter_by_data_type("ortho") ortho_data = ortho_data_products[0] ortho_url_2123 = ortho_data.url ortho_url_2023 = leafmap.d2s_tile(ortho_url_2123)

Create a split map for comparing the 2018 and 2023 ortho images.

from ipyleaflet import TileLayer m = geemap.Map() left_layer = TileLayer(url=geemap.cog_tile(ortho_url), name="2018 Ortho") right_layer = TileLayer(url=geemap.cog_tile(ortho_url_2023), name="2023 Ortho") m.split_map(left_layer, right_layer) m.set_center(-86.9048, 40.4247, 15) m

Using Earth Engine data

Earth Engine data types

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

image = ee.Image("USGS/SRTMGL1_003") image

Visualizing Earth Engine images

m = geemap.Map() image = ee.Image("USGS/SRTMGL1_003") vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], # 'terrain' } m.add_layer(image, vis_params, "SRTM") m
roi = ee.FeatureCollection(geemap.geojson_to_ee(project_boundary)) roi
m.add_layer(roi, {}, "Purdue Campus") m.centerObject(roi, 15)
hillshade = ee.Terrain.hillshade(image).clipToCollection(roi) m.add_layer(hillshade, {}, "SRTM Hillshade")

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:

collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")

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:

m = geemap.Map() collection = ( ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") .filterDate("2023-01-01", "2024-08-01") .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 5)) ) image = collection.median().clipToCollection(roi) vis = { "min": 0.0, "max": 2500, "bands": ["B4", "B3", "B2"], } m.add_layer(image, vis, "Sentinel-2") m.centerObject(roi, 15) m

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:

m = geemap.Map() fc = ee.FeatureCollection("TIGER/2016/Roads").filterBounds(roi) m.add_layer(fc, {}, "Census roads") m.centerObject(roi, 15) m
m = geemap.Map(center=[40, -100], zoom=4) countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM0") fc = countries.filter(ee.Filter.eq("shapeName", "United States")) m.add_layer(fc, {}, "USA") m
m = geemap.Map(center=[40, -100], zoom=4) countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM1") fc = countries.filter(ee.Filter.eq("shapeGroup", "USA")) m.add_layer(fc, {}, "USA") m

Visualizing feature collections

m = geemap.Map() countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM1") fc = countries.filter(ee.Filter.eq("shapeGroup", "USA")) style = {"color": "000000ff", "width": 2, "lineType": "solid", "fillColor": "FF000000"} m.add_layer(fc.style(**style), {}, "USA") m.center_object(fc, 4) m
m = geemap.Map() countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM1") fc = countries.filter(ee.Filter.eq("shapeGroup", "USA")) style = {"color": "0000ffff", "width": 2, "lineType": "solid", "fillColor": "FF000080"} m.add_layer(fc.style(**style), {}, "USA") m.center_object(fc, 4) m

Earth Engine Data Catalog

The Earth Engine Data Catalog hosts a variety of geospatial datasets. As of September 2024, the catalog contains over 1,100 datasets with a total size of over 100 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. 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.

m = geemap.Map() m
m = geemap.Map() 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
m = geemap.Map() counties = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM0") fc = counties.filter(ee.Filter.eq("shapeName", "United States")) dem = ee.Image("USGS/SRTMGL1_003").clipToCollection(fc) vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } m.add_layer(fc, {}, "United States") m.add_layer(dem, vis_params, "SRTM DEM") m.center_object(fc, 4) m

Visualizing Earth Engine data

Using the inspector tool

Inspect pixel values and vector features using the inspector tool.

m = geemap.Map() countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM1") fc = countries.filter(ee.Filter.eq("shapeGroup", "USA")) dem = ee.Image("USGS/SRTMGL1_003") landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003").select( ["B1", "B2", "B3", "B4", "B5", "B7"] ) vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } m.add_layer( landsat7, {"bands": ["B4", "B3", "B2"], "min": 20, "max": 200, "gamma": 2.0}, "Landsat 7", ) m.add_layer(dem, vis_params, "SRTM DEM") m.add_layer(fc, {}, "United States") m.add("inspector") m.center_object(fc, 4) m

Using the plotting tool

Plot spectral profiles of pixels using the plotting tool.

m = geemap.Map(center=[40, -100], zoom=4) landsat7 = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003").select( ["B1", "B2", "B3", "B4", "B5", "B7"] ) landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} m.add_layer(landsat7, landsat_vis, "Landsat") hyperion = ee.ImageCollection("EO1/HYPERION").filter( ee.Filter.date("2016-01-01", "2017-03-01") ) hyperion_vis = { "min": 1000.0, "max": 14000.0, "gamma": 2.5, } m.add_layer(hyperion, hyperion_vis, "Hyperion") m.add_plot_gui() m.center_object(fc, 4) m

Set plotting options for Landsat.

m.set_plot_options(add_marker_cluster=True, overlay=True)

Set plotting options for Hyperion.

m.set_plot_options(add_marker_cluster=True, plot_type="bar")

Legends, color bars, and labels

Built-in legends

from geemap.legends import builtin_legends
for legend in builtin_legends: print(legend)
m = geemap.Map() m.add_basemap("Esri.WorldImagery") dataset = ee.ImageCollection("ESA/WorldCover/v200").first() visualization = {"bands": ["Map"]} m.add_layer(dataset, visualization, "Landcover") m.add_layer(fc, {}, "United States") m.add_legend(title="Land Cover Type", builtin_legend="ESA_WorldCover") m.center_object(fc, 4) m

Custom legends

Add a custom legend by specifying a dictionary of colors and labels.

m = geemap.Map() m.add_basemap("Esri.WorldImagery") dataset = ee.ImageCollection("ESA/WorldCover/v200").first() visualization = {"bands": ["Map"]} m.add_layer(dataset, visualization, "Landcover") m.add_layer(fc, {}, "United States") legend_dict = { "10 Trees": "006400", "20 Shrubland": "ffbb22", "30 Grassland": "ffff4c", "40 Cropland": "f096ff", "50 Built-up": "fa0000", "60 Barren / sparse vegetation": "b4b4b4", "70 Snow and ice": "f0f0f0", "80 Open water": "0064c8", "90 Herbaceous wetland": "0096a0", "95 Mangroves": "00cf75", "100 Moss and lichen": "fae6a0", } m.add_legend(title="Land Cover Type", legend_dict=legend_dict) m.center_object(fc, 4) m

Creating color bars

Add a horizontal color bar.

m = geemap.Map() countries = ee.FeatureCollection("WM/geoLab/geoBoundaries/600/ADM0") fc = countries.filter(ee.Filter.eq("shapeName", "United States")) dem = ee.Image("USGS/SRTMGL1_003").clipToCollection(fc) vis_params = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } m.add_layer(dem, vis_params, "SRTM DEM") m.add_colorbar(vis_params, label="Elevation (m)", layer_name="SRTM DEM") m.center_object(fc, 4) m

Add a vertical color bar.

m.add_colorbar( vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical", max_width="100px", )

Make the color bar background transparent.

m.add_colorbar( vis_params, label="Elevation (m)", layer_name="SRTM DEM", orientation="vertical", max_width="100px", transparent_bg=True, )

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.

m = geemap.Map() m.split_map(left_layer="Esri.WorldTopoMap", right_layer="OpenTopoMap") m.center_object(roi, 6) m

Create a split map with Earth Engine layers.

m = geemap.Map() esa_2020 = ee.ImageCollection("ESA/WorldCover/v100").first() esa_2021 = ee.ImageCollection("ESA/WorldCover/v200").first() visualization = {"bands": ["Map"]} left_layer = geemap.ee_tile_layer(esa_2020, visualization, "Land Cover 2020") right_layer = geemap.ee_tile_layer(esa_2021, visualization, "Land Cover 2021") m.split_map( left_layer, right_layer, left_label="Land Cover 2020", right_label="Land Cover 2021" ) m.add_legend(title="Land Cover Type", builtin_legend="ESA_WorldCover") m.center_object(fc, 4) m

Timeseries inspector and time slider

Timeseries inspector

Check the available years of NLCD.

m = geemap.Map(center=[40, -100], zoom=4) collection = ee.ImageCollection("USGS/NLCD_RELEASES/2019_REL/NLCD").select("landcover") vis_params = {"bands": ["landcover"]} years = collection.aggregate_array("system:index").getInfo() years

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 a future ipyleaflet release.

m.ts_inspector( left_ts=collection, right_ts=collection, left_names=years, right_names=years, left_vis=vis_params, right_vis=vis_params, width="80px", ) m

Generate the NAIP timeseries.

naip_col = geemap.naip_timeseries(roi, RGBN=True)
m = geemap.Map() vis_params = {"bands": ["N", "R", "G"]} m.ts_inspector(naip_col, left_vis=vis_params, date_format="YYYY", width="80px") m.center_object(roi, 15) m

Time slider

Note that this feature may not work properly with in the Colab environment. Restart Colab runtime if the time slider does not work.

Create a map for visualizing MODIS vegetation data.

m = geemap.Map() collection = ( ee.ImageCollection("MODIS/MCD43A4_006_NDVI") .filter(ee.Filter.date("2022-06-01", "2022-07-01")) .select("NDVI") ) vis_params = { "min": 0.0, "max": 1.0, "palette": "ndvi", } m.add_time_slider(collection, vis_params, time_interval=2) m._toolbar.toggle_layers(False) m

Create a map for visualizing weather data.

m = geemap.Map() collection = ( ee.ImageCollection("NOAA/GFS0P25") .filterDate("2018-12-22", "2018-12-23") .limit(24) .select("temperature_2m_above_ground") ) vis_params = { "min": -40.0, "max": 35.0, "palette": ["blue", "purple", "cyan", "green", "yellow", "red"], } labels = [str(n).zfill(2) + ":00" for n in range(0, 24)] m.add_time_slider(collection, vis_params, labels=labels, time_interval=1, opacity=0.8) m._toolbar.toggle_layers(False) m

Visualizing Sentinel-2 imagery

m = geemap.Map() collection = ( ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") .filterBounds(roi) .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 0.5) ) vis_params = {"min": 0, "max": 4000, "bands": ["B8", "B4", "B3"]} m.center_object(roi, 14) m.add_time_slider(collection, vis_params, region=roi) m._toolbar.toggle_layers(False) m

Analyzing Earth Engine data

Zonal statistics

m = geemap.Map(center=[40, -100], zoom=4) # Add NASA SRTM dem = ee.Image("USGS/SRTMGL1_003") dem_vis = { "min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00", "D8D8D8", "F5F5F5"], } m.add_layer(dem, dem_vis, "SRTM DEM") # Add 5-year Landsat TOA composite landsat = ee.Image("LANDSAT/LE7_TOA_5YEAR/1999_2003") landsat_vis = {"bands": ["B4", "B3", "B2"], "gamma": 1.4} m.add_layer(landsat, landsat_vis, "Landsat", False) # Add US Census States fc = ee.FeatureCollection("TIGER/2018/States") style = {"fillColor": "00000000"} m.add_layer(fc.style(**style), {}, "US States") m
out_dem_stats = "dem_stats.csv" geemap.zonal_stats(dem, fc, out_dem_stats, stat_type="MEAN", return_fc=False)
df = geemap.csv_to_df(out_dem_stats).sort_values(by=["mean"]) df.head(10)
geemap.bar_chart(out_dem_stats, "STUSPS", "mean", title="Mean Elevation (m)")
out_landsat_stats = "landsat_stats.csv" geemap.zonal_stats( landsat, fc, out_landsat_stats, stat_type="MEAN", return_fc=False, )
df = geemap.csv_to_df(out_landsat_stats) df.head()

Zonal statistics by group

m = geemap.Map() m.add_basemap("Esri.WorldImagery") fc = ee.FeatureCollection("TIGER/2018/States") dataset = ee.ImageCollection("ESA/WorldCover/v200").first().clipToCollection(fc) visualization = {"bands": ["Map"]} m.add_layer(dataset, visualization, "Landcover") m.add_legend(title="Land Cover Type", builtin_legend="ESA_WorldCover") m.add_layer(fc, {}, "United States") m.center_object(fc, 4) m
m = geemap.Map(center=[40, -100], zoom=4) # Add NLCD data dataset = ee.Image("USGS/NLCD_RELEASES/2021_REL/NLCD/2021") landcover = dataset.select("landcover") m.add_layer(landcover, {}, "NLCD 2021") # Add US census states states = ee.FeatureCollection("TIGER/2018/States") style = {"fillColor": "00000000"} m.add_layer(states.style(**style), {}, "US States") # Add NLCD legend m.add_legend(title="NLCD Land Cover", builtin_legend="NLCD") m
nlcd_stats = "nlcd_stats.csv" geemap.zonal_stats_by_group( landcover, states, nlcd_stats, stat_type="SUM", denominator=1e6, decimal_places=2, )
geemap.csv_to_df(nlcd_stats)
nlcd_stats = "nlcd_stats_pct.csv" geemap.zonal_stats_by_group( landcover, states, nlcd_stats, stat_type="PERCENTAGE", denominator=1e6, decimal_places=2, )
geemap.csv_to_df(nlcd_stats)

Creating timelapse animations

Landsat timelapse

m = geemap.Map(center=[40, -100], zoom=4) m.add_basemap("Esri.WorldImagery") m
roi = m.user_roi if roi is None: roi = ee.Geometry.BBox(-86.9561, 40.4042, -86.8876, 40.4345) m.add_layer(roi) m.center_object(roi)
timelapse = geemap.landsat_timelapse( roi, out_gif="West_Lafayette.gif", start_year=1988, end_year=2024, start_date="01-01", end_date="12-31", bands=["SWIR1", "NIR", "Red"], frames_per_second=5, title="River Dynamics", progress_bar_color="blue", mp4=True, ) geemap.show_image(timelapse)
roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497) timelapse = geemap.landsat_timelapse( roi, out_gif="hong_kong.gif", start_year=1990, end_year=2022, start_date="01-01", end_date="12-31", bands=["SWIR1", "NIR", "Red"], frames_per_second=3, title="Hong Kong", ) geemap.show_image(timelapse)
roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581) timelapse = geemap.landsat_timelapse( roi, out_gif="las_vegas.gif", start_year=1984, end_year=2023, bands=["NIR", "Red", "Green"], frames_per_second=5, title="Las Vegas, NV", font_color="blue", ) geemap.show_image(timelapse)

Sentinel-2 timelapse

m = geemap.Map(center=[-0.4315, -76.5748], zoom=13) m
roi = m.user_roi if roi is None: roi = ee.Geometry.BBox(-86.9561, 40.4042, -86.8876, 40.4345) m.add_layer(roi) m.center_object(roi)
timelapse = geemap.sentinel2_timelapse( roi, out_gif="sentinel2.gif", start_year=2017, end_year=2024, start_date="01-01", end_date="12-31", frequency="year", bands=["SWIR1", "NIR", "Red"], frames_per_second=3, title="Sentinel-2 Timelapse", ) geemap.show_image(timelapse)

MODIS vegetation indices

Map = geemap.Map() Map
roi = Map.user_roi if roi is None: roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446) Map.addLayer(roi) Map.centerObject(roi)
timelapse = geemap.modis_ndvi_timelapse( roi, out_gif="ndvi.gif", data="Terra", band="NDVI", start_date="2000-01-01", end_date="2022-12-31", frames_per_second=3, title="MODIS NDVI Timelapse", overlay_data="countries", ) geemap.show_image(timelapse)

GOES timelapse

roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411) start_date = "2022-01-15T03:00:00" end_date = "2022-01-15T07:00:00" data = "GOES-17" scan = "full_disk"
timelapse = geemap.goes_timelapse( roi, "goes.gif", start_date, end_date, data, scan, framesPerSecond=5 ) geemap.show_image(timelapse)
roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088) start_date = "2021-10-24T14:00:00" end_date = "2021-10-25T01:00:00" data = "GOES-17" scan = "full_disk"
timelapse = geemap.goes_timelapse( roi, "hurricane.gif", start_date, end_date, data, scan, framesPerSecond=5 ) geemap.show_image(timelapse)
roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490) start_date = "2020-09-05T15:00:00" end_date = "2020-09-06T02:00:00" data = "GOES-17" scan = "full_disk"
timelapse = geemap.goes_fire_timelapse( roi, "fire.gif", start_date, end_date, data, scan, framesPerSecond=5 ) geemap.show_image(timelapse)

Exercise - 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 hashtag such as #EarthEngine and #geemap. See this example.

m = geemap.Map() m.add_gui("timelapse") m