diff --git a/docs/workshops/HGAC_2024.ipynb b/docs/workshops/HGAC_2024.ipynb new file mode 100644 index 0000000000..e4d58400ad --- /dev/null +++ b/docs/workshops/HGAC_2024.ipynb @@ -0,0 +1,5358 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/workshops/HGAC_2024.ipynb)\n", + "\n", + "**Cloud Computing with Earth Engine and Geemap**\n", + "\n", + "- Notebook: \n", + "- Earth Engine: \n", + "- Geemap: \n", + "\n", + "## Introduction (10 mins)\n", + "\n", + "This notebook is for the workshop presented at the [Houston Area GIS Day Workshops](https://www.h-gac.com/events/houston-area-gis-day-workshops).\n", + "\n", + "Participants will dive into Google Earth Engine and Geemap to create interactive maps and analyze large-scale geospatial datasets. Topics include:\n", + "\n", + "* Building interactive maps\n", + "* Visualizing and analyzing Earth Engine datasets\n", + "* Exploring Earth Engine Data Catalogs\n", + "* Exporting data for further use\n", + "* Creating interactive charts from Earth Engine datasets\n", + "* Creating stunning timelapse animations\n", + "\n", + "## Prerequisites\n", + "\n", + "- To use geemap and the Earth Engine Python API, you must [register](https://code.earthengine.google.com/register) for an Earth Engine account and follow the instructions [here](https://docs.google.com/document/d/1ZGSmrNm6_baqd8CHt33kIBWOlvkh-HLr46bODgJN1h0/edit?usp=sharing) to create a Cloud Project. Earth Engine is free for [noncommercial and research use](https://earthengine.google.com/noncommercial). To test whether you can use authenticate the Earth Engine Python API, please run [this notebook](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/geemap_colab.ipynb) on Google Colab.\n", + "\n", + "- It is recommended that attendees have a basic understanding of Python and Jupyter Notebook.\n", + "- Familiarity with the Earth Engine JavaScript API is not required but will be helpful.\n", + "- Attendees can use Google Colab to follow this workshop without installing anything on their computer.\n", + "\n", + "## Introduction to Google Earth Engine\n", + "\n", + "### Google Earth Engine Overview\n", + "\n", + "[Google Earth Engine](https://earthengine.google.com) is a cloud-computing platform that enables scientific analysis and visualization of large-scale geospatial datasets. It provides a rich data catalog and processing capabilities, allowing users to analyze satellite imagery and geospatial data at planetary scale.\n", + "\n", + "Earth Engine is free for [noncommercial and research use](https://earthengine.google.com/noncommercial). Nonprofit organizations, research institutions, educators, Indigenous governments, and government researchers can continue using Earth Engine for free, as they have for more than a decade. However, [commercial users](https://earthengine.google.com/commercial) may require a paid license.\n", + "\n", + "### Installing Geemap\n", + "\n", + "The geemap package simplifies the use of Google Earth Engine in Python, offering an intuitive API for visualization and analysis in Jupyter notebooks. In Google Colab, geemap is pre-installed, but you may need to install additional dependencies for certain features. To install the latest version with optional dependencies, use the following command:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install -U \"geemap[workshop]\"" + ] + }, + { + "cell_type": "markdown", + "id": "2", + "metadata": {}, + "source": [ + "### Import Libraries\n", + "\n", + "To start, import the necessary libraries for working with Google Earth Engine (GEE) and geemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "### Authenticate and Initialize Earth Engine\n", + "\n", + "To authenticate and initialize your Earth Engine environment, you’ll need to create a Google Cloud Project and enable the [Earth Engine API](https://console.cloud.google.com/apis/api/earthengine.googleapis.com) for the project if you have not done so already. You can find detailed setup instructions [here](https://developers.google.com/earth-engine/guides/access#a-role-in-a-cloud-project)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_initialize(project=None)" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "### Creating Interactive Maps\n", + "\n", + "Let’s create an interactive map using the `ipyleaflet` plotting backend. The `geemap.Map` class inherits from the `ipyleaflet.Map` class, so the syntax is similar to creating an interactive map with `ipyleaflet.Map`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "To display the map in a Jupyter notebook, simply enter the map object:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "m" + ] + }, + { + "cell_type": "markdown", + "id": "12", + "metadata": {}, + "source": [ + "To customize the map’s display, you can set various keyword arguments, such as `center` (latitude and longitude), `zoom`, `width`, and `height`. By default, the `width` is `100%`, filling the entire width of the Jupyter notebook cell. The `height` parameter can be a number (in pixels) or a string with a pixel format, e.g., `600px`.\n", + "\n", + "#### Example Maps\n", + "\n", + "**Map of the Contiguous United States**\n", + "\n", + " To center the map on the contiguous United States, use:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4, height=\"600px\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "**Map of the state of Tennessee**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[35.746512, -86.209818], zoom=8)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "**Map of the city of Knoxville, TN**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[35.959111, -83.909463], zoom=13)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "18", + "metadata": {}, + "source": [ + "To hide specific map controls, set the corresponding control argument to `False`, such as `draw_ctrl=False` to hide the drawing control." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(data_ctrl=False, toolbar_ctrl=False, draw_ctrl=False)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "20", + "metadata": {}, + "source": [ + "### Adding Basemaps\n", + "\n", + "There are several ways to add basemaps to a map in geemap. You can specify a basemap in the `basemap` keyword argument when creating the map, or you can add additional basemap layers using the `add_basemap` method. `Geemap` provides access to hundreds of built-in basemaps, making it easy to add layers to a map with a single line of code.\n", + "\n", + "To create a map with a specific basemap, use the `basemap` argument as shown below. For example, `Esri.WorldImagery` provides an Esri world imagery basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Esri.WorldImagery\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "22", + "metadata": {}, + "source": [ + "#### Adding Multiple Basemaps\n", + "\n", + "You can add multiple basemaps to a map by calling `add_basemap` multiple times. For example, the following code adds the `Esri.WorldTopoMap` and `OpenTopoMap` basemaps to the existing map:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [ + "m.add_basemap(\"Esri.WorldTopoMap\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "m.add_basemap(\"OpenTopoMap\")" + ] + }, + { + "cell_type": "markdown", + "id": "25", + "metadata": {}, + "source": [ + "#### Listing Available Basemaps\n", + "\n", + "To view the first 10 available basemaps, use:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26", + "metadata": {}, + "outputs": [], + "source": [ + "basemaps = list(geemap.basemaps.keys())\n", + "len(geemap.basemaps)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27", + "metadata": {}, + "outputs": [], + "source": [ + "basemaps[:10]" + ] + }, + { + "cell_type": "markdown", + "id": "28", + "metadata": {}, + "source": [ + "### Google Basemaps\n", + "\n", + "Due to licensing restrictions, Google basemaps are not included in geemap by default. However, users can add Google basemaps manually at their own discretion using the following URLs:\n", + "\n", + "```text\n", + " ROADMAP: https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}\n", + " SATELLITE: https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}\n", + " TERRAIN: https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}\n", + " HYBRID: https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}\n", + "```\n", + "\n", + "For example, to add Google Satellite as a tile layer:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "url = \"https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}\"\n", + "m.add_tile_layer(url, name=\"Google Satellite\", attribution=\"Google\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "30", + "metadata": {}, + "source": [ + "You can also add text to the map using the `add_text` method. The text will be displayed at the specified location on the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31", + "metadata": {}, + "outputs": [], + "source": [ + "m.add_text(text=\"Hello from Earth Engine\", position=\"bottomright\")" + ] + }, + { + "cell_type": "markdown", + "id": "32", + "metadata": {}, + "source": [ + "## Introduction to Interactive Maps and Tools\n", + "\n", + "### Basemap Selector\n", + "\n", + "The basemap selector allows you to choose from various basemaps via a dropdown menu. Adding it to your map provides easy access to different map backgrounds." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add(\"basemap_selector\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "34", + "metadata": {}, + "source": [ + "### Layer Manager\n", + "\n", + "The layer manager provides control over layer visibility and transparency. It enables toggling layers on and off and adjusting transparency with a slider, making it easy to customize map visuals." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", + "m.add_layer(states, {}, \"US States\")\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "36", + "metadata": {}, + "source": [ + "### Inspector Tool\n", + "\n", + "The inspector tool allows you to click on the map to query Earth Engine data at specific locations. This is helpful for examining the properties of datasets directly on the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", + "m.add_layer(\n", + " landsat7,\n", + " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n", + " \"Landsat 7\",\n", + ")\n", + "m.add_layer(states, {}, \"US States\")\n", + "m.add(\"inspector\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "38", + "metadata": {}, + "source": [ + "### Layer Editor\n", + "\n", + "With the layer editor, you can adjust visualization parameters of Earth Engine data for better clarity and focus. It supports single-band images, multi-band images, and feature collections.\n", + "\n", + "#### Single-Band image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", + "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"SRTM DEM\"])\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "40", + "metadata": {}, + "source": [ + "#### Multi-Band image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "m.add_layer(\n", + " landsat7,\n", + " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n", + " \"Landsat 7\",\n", + ")\n", + "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"Landsat 7\"])\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "42", + "metadata": {}, + "source": [ + "#### Feature Collection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "m.add_layer(states, {}, \"US States\")\n", + "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"US States\"])\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "44", + "metadata": {}, + "source": [ + "### Draw Control\n", + "\n", + "The draw control feature allows you to draw shapes directly on the map, converting them automatically into Earth Engine objects. Access the drawn features as follows:\n", + "\n", + "- To return the last drawn feature as an `ee.Geometry()`, use `m.user_roi`\n", + "- To return all drawn features as an `ee.FeatureCollection()`, use `m.user_rois`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "45", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": \"terrain\",\n", + "}\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46", + "metadata": {}, + "outputs": [], + "source": [ + "if m.user_roi is not None:\n", + " image = dem.clip(m.user_roi)\n", + " m.layers[1].visible = False\n", + " m.add_layer(image, vis_params, \"Clipped DEM\")" + ] + }, + { + "cell_type": "markdown", + "id": "47", + "metadata": {}, + "source": [ + "## The Earth Engine Data Catalog\n", + "\n", + "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets) hosts an extensive collection of geospatial datasets. Currently, he catalog includes over [1,000 datasets](https://github.com/opengeos/Earth-Engine-Catalog/blob/master/gee_catalog.tsv) with a combined size exceeding 100 petabytes. Notable datasets include Landsat, Sentinel, MODIS, and NAIP. For a comprehensive list of datasets in CSV or JSON format, refer to the [Earth Engine Datasets List](https://github.com/giswqs/Earth-Engine-Catalog/blob/master/gee_catalog.tsv).\n", + "\n", + "### Searching Datasets on the Earth Engine Website\n", + "\n", + "To browse datasets directly on the Earth Engine website:\n", + "\n", + "- View the full catalog: https://developers.google.com/earth-engine/datasets/catalog\n", + "- Search by tags: https://developers.google.com/earth-engine/datasets/tags\n", + "\n", + "### Searching Datasets Within Geemap\n", + "\n", + "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets/catalog) can also be searched directly from `geemap`. Use keywords to filter datasets by name, tag, or description. For instance, searching for \"elevation\" will display only datasets containing \"elevation\" in their metadata, returning 52 datasets. You can scroll through the results to locate the [NASA SRTM Digital Elevation 30m](https://developers.google.com/earth-engine/datasets/catalog/USGS_SRTMGL1_003#description) dataset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "49", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/B3rf4QN.jpg)\n", + "\n", + "Each dataset page contains detailed information such as availability, provider, Earth Engine snippet, tags, description, and example code. The Image, ImageCollection, or FeatureCollection ID is essential for accessing the dataset in Earth Engine’s JavaScript or Python APIs.\n", + "\n", + "### Using the Datasets Module in Geemap\n", + "\n", + "Geemap offers a built-in `datasets` module to access specific datasets programmatically. For example, to access the USGS GAP Alaska dataset:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50", + "metadata": {}, + "outputs": [], + "source": [ + "from geemap.datasets import DATA" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "dataset = ee.Image(DATA.USGS_GAP_AK_2001)\n", + "m.add_layer(dataset, {}, \"GAP Alaska\")\n", + "m.centerObject(dataset, zoom=4)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "52", + "metadata": {}, + "source": [ + "To retrieve metadata for a specific dataset, use the `get_metadata` function:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53", + "metadata": {}, + "outputs": [], + "source": [ + "from geemap.datasets import get_metadata\n", + "\n", + "get_metadata(DATA.USGS_GAP_AK_2001)" + ] + }, + { + "cell_type": "markdown", + "id": "54", + "metadata": {}, + "source": [ + "## Earth Engine Data Types\n", + "\n", + "Earth Engine objects are server-side entities, meaning they are stored and processed on Earth Engine’s servers rather than locally. This setup is comparable to streaming services (e.g., YouTube or Netflix) where the data remains on the provider’s servers, allowing us to access and process geospatial data in real time without downloading it to our local devices.\n", + "\n", + "- **Image**: The core raster data type in Earth Engine, representing single images or scenes.\n", + "- **ImageCollection**: A collection or sequence of images, often used for time series analysis.\n", + "- **Geometry**: The fundamental vector data type, including shapes like points, lines, and polygons.\n", + "- **Feature**: A Geometry with associated attributes, used to add descriptive data to vector shapes.\n", + "- **FeatureCollection**: A collection of Features, similar to a shapefile with attribute data.\n", + "\n", + "## Earth Engine Raster Data\n", + "\n", + "### Image\n", + "\n", + "In Earth Engine, raster data is represented as **Image** objects. Each Image is composed of bands, with each band having its own name, data type, scale, mask, and projection. Metadata for each image is stored as a set of properties.\n", + "\n", + "#### Loading Earth Engine Images\n", + "\n", + "To load images, use the Earth Engine asset ID within the `ee.Image` constructor. Asset IDs can be found in the Earth Engine Data Catalog. For example, to load the NASA SRTM Digital Elevation dataset:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55", + "metadata": {}, + "outputs": [], + "source": [ + "image = ee.Image(\"USGS/SRTMGL1_003\")\n", + "image" + ] + }, + { + "cell_type": "markdown", + "id": "56", + "metadata": {}, + "source": [ + "#### Visualizing Earth Engine Images\n", + "\n", + "To visualize an image, you can specify visualization parameters such as minimum and maximum values and color palettes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[21.79, 70.87], zoom=3)\n", + "image = ee.Image(\"USGS/SRTMGL1_003\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 6000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"], # 'terrain'\n", + "}\n", + "m.add_layer(image, vis_params, \"SRTM\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "58", + "metadata": {}, + "source": [ + "### ImageCollection\n", + "\n", + "An **ImageCollection** represents a sequence of images, often used for temporal data like satellite image time series. ImageCollections are created by passing an Earth Engine asset ID into the `ImageCollection` constructor. Asset IDs are available in the [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets).\n", + "\n", + "#### Loading Image Collections\n", + "\n", + "To load an ImageCollection, such as the Sentinel-2 surface reflectance collection:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59", + "metadata": {}, + "outputs": [], + "source": [ + "collection = ee.ImageCollection(\"COPERNICUS/S2_SR_HARMONIZED\")" + ] + }, + { + "cell_type": "markdown", + "id": "60", + "metadata": {}, + "source": [ + "#### Filtering Image Collections\n", + "\n", + "You can filter ImageCollections by location and time. For example, to filter images covering a specific location with low cloud cover:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61", + "metadata": {}, + "outputs": [], + "source": [ + "geometry = ee.Geometry.Point([-83.909463, 35.959111])\n", + "images = collection.filterBounds(geometry)\n", + "images.size()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62", + "metadata": {}, + "outputs": [], + "source": [ + "images.first()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63", + "metadata": {}, + "outputs": [], + "source": [ + "images = (\n", + " collection.filterBounds(geometry)\n", + " .filterDate(\"2024-07-01\", \"2024-10-01\")\n", + " .filter(ee.Filter.lt(\"CLOUDY_PIXEL_PERCENTAGE\", 5))\n", + ")\n", + "images.size()" + ] + }, + { + "cell_type": "markdown", + "id": "64", + "metadata": {}, + "source": [ + "To view the filtered collection on a map:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "image = images.first()\n", + "\n", + "vis = {\n", + " \"min\": 0.0,\n", + " \"max\": 3000,\n", + " \"bands\": [\"B4\", \"B3\", \"B2\"],\n", + "}\n", + "\n", + "m.add_layer(image, vis, \"Sentinel-2\")\n", + "m.centerObject(image, 8)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "66", + "metadata": {}, + "source": [ + "#### Visualizing Image Collections\n", + "\n", + "To visualize an **ImageCollection** as a single composite image, you need to aggregate the collection. For example, using the `collection.median()` method creates an image representing the median value across the collection." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "image = images.median()\n", + "\n", + "vis = {\n", + " \"min\": 0.0,\n", + " \"max\": 3000,\n", + " \"bands\": [\"B8\", \"B4\", \"B3\"],\n", + "}\n", + "\n", + "m.add_layer(image, vis, \"Sentinel-2\")\n", + "m.centerObject(geometry, 8)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "68", + "metadata": {}, + "source": [ + "## Earth Engine Vector Data\n", + "\n", + "A **FeatureCollection** is a collection of Features. It functions similarly to a GeoJSON FeatureCollection, where features include associated properties or attributes. For example, a shapefile’s data can be represented as a FeatureCollection.\n", + "\n", + "### Loading Feature Collections\n", + "\n", + "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets) includes various vector datasets, such as U.S. Census data and country boundaries, as feature collections. Feature Collection IDs are accessible by searching the data catalog. For example, to load the [TIGER roads data](https://developers.google.com/earth-engine/datasets/catalog/TIGER_2016_Roads) by the U.S. Census Bureau:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "fc = ee.FeatureCollection(\"TIGER/2016/Roads\")\n", + "m.set_center(-83.909463, 35.959111, 12)\n", + "m.add_layer(fc, {}, \"Census roads\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "70", + "metadata": {}, + "source": [ + "### Filtering Feature Collections\n", + "\n", + "The `filter` method allows you to filter a FeatureCollection based on certain attribute values. For instance, we can filter for specific states using the `eq` filter to select \"Tennessee\":" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "71", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.eq(\"NAME\", \"Tennessee\"))\n", + "m.add_layer(fc, {}, \"Tennessee\")\n", + "m.center_object(fc, 7)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "72", + "metadata": {}, + "source": [ + "To retrieve properties of the first feature in the collection, use:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73", + "metadata": {}, + "outputs": [], + "source": [ + "feat = fc.first()\n", + "feat.toDictionary()" + ] + }, + { + "cell_type": "markdown", + "id": "74", + "metadata": {}, + "source": [ + "You can also convert a FeatureCollection to a Pandas DataFrame for easier analysis with:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_df(fc)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.inList(\"NAME\", [\"California\", \"Oregon\", \"Washington\"]))\n", + "m.add_layer(fc, {}, \"West Coast\")\n", + "m.center_object(fc, 5)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77", + "metadata": {}, + "outputs": [], + "source": [ + "region = m.user_roi\n", + "if region is None:\n", + " region = ee.Geometry.BBox(-88.40, 29.88, -77.90, 35.39)\n", + "\n", + "fc = ee.FeatureCollection(\"TIGER/2018/States\").filterBounds(region)\n", + "m.add_layer(fc, {}, \"Southeastern U.S.\")\n", + "m.center_object(fc, 6)" + ] + }, + { + "cell_type": "markdown", + "id": "78", + "metadata": {}, + "source": [ + "A FeatureCollection can be used to clip an image. The `clipToCollection` method clips an image to the geometry of a feature collection. For example, to clip a Landsat image to the boundary of France:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4)\n", + "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "countries = ee.FeatureCollection(\"FAO/GAUL_SIMPLIFIED_500m/2015/level0\")\n", + "fc = countries.filter(ee.Filter.eq(\"ADM0_NAME\", \"Germany\"))\n", + "image = landsat7.clipToCollection(fc)\n", + "m.add_layer(\n", + " image,\n", + " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n", + " \"Landsat 7\",\n", + ")\n", + "m.center_object(fc, 6)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "80", + "metadata": {}, + "source": [ + "### Visualizing Feature Collections\n", + "\n", + "Once loaded, feature collections can be visualized on an interactive map. For example, visualizing U.S. state boundaries from the census data:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "m.add_layer(states, {}, \"US States\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "82", + "metadata": {}, + "source": [ + "Feature collections can also be styled with additional parameters. To apply a custom style, specify options like color and line width:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "style = {\"color\": \"0000ffff\", \"width\": 2, \"lineType\": \"solid\", \"fillColor\": \"FF000080\"}\n", + "m.add_layer(states.style(**style), {}, \"US States\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "84", + "metadata": {}, + "source": [ + "Using `add_styled_vector`, you can apply a color palette to style different features by attribute:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "vis_params = {\n", + " \"color\": \"000000\",\n", + " \"colorOpacity\": 1,\n", + " \"pointSize\": 3,\n", + " \"pointShape\": \"circle\",\n", + " \"width\": 2,\n", + " \"lineType\": \"solid\",\n", + " \"fillColorOpacity\": 0.66,\n", + "}\n", + "palette = [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"]\n", + "m.add_styled_vector(\n", + " states, column=\"NAME\", palette=palette, layer_name=\"Styled vector\", **vis_params\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "86", + "metadata": {}, + "source": [ + "## More Tools for Visualizing Earth Engine Data\n", + "\n", + "### Using the Plotting Tool\n", + "\n", + "The plotting tool in geemap enables visualization of Earth Engine data layers. In this example, Landsat 7 and Hyperion data are added with specific visualization parameters, allowing for comparison of these datasets. The plot GUI is then added to facilitate detailed exploration of the data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "\n", + "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\").select(\n", + " [\"B1\", \"B2\", \"B3\", \"B4\", \"B5\", \"B7\"]\n", + ")\n", + "\n", + "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n", + "m.add_layer(landsat7, landsat_vis, \"Landsat\")\n", + "\n", + "hyperion = ee.ImageCollection(\"EO1/HYPERION\").filter(\n", + " ee.Filter.date(\"2016-01-01\", \"2017-03-01\")\n", + ")\n", + "\n", + "hyperion_vis = {\n", + " \"min\": 1000.0,\n", + " \"max\": 14000.0,\n", + " \"gamma\": 2.5,\n", + "}\n", + "m.add_layer(hyperion, hyperion_vis, \"Hyperion\")\n", + "m.add_plot_gui()\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "88", + "metadata": {}, + "source": [ + "Set the plotting options for Landsat to add marker clusters and overlays, enhancing the interactivity of plotted data on the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89", + "metadata": {}, + "outputs": [], + "source": [ + "m.set_plot_options(add_marker_cluster=True, overlay=True)" + ] + }, + { + "cell_type": "markdown", + "id": "90", + "metadata": {}, + "source": [ + "Adjust the plotting options for Hyperion data by setting a bar plot type with marker clusters, suitable for visualizing Hyperion’s data values in bar format." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91", + "metadata": {}, + "outputs": [], + "source": [ + "m.set_plot_options(add_marker_cluster=True, plot_type=\"bar\")" + ] + }, + { + "cell_type": "markdown", + "id": "92", + "metadata": {}, + "source": [ + "### Legends\n", + "\n", + "#### Built-in Legends\n", + "\n", + "Geemap provides built-in legends that can be easily added to maps for better interpretability of data classes. Here, an NLCD legend is added to both a WMS layer and an Earth Engine land cover layer, giving quick visual reference to the data's classification scheme." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93", + "metadata": {}, + "outputs": [], + "source": [ + "from geemap.legends import builtin_legends" + ] + }, + { + "cell_type": "markdown", + "id": "94", + "metadata": {}, + "source": [ + "Print out all available built-in legends, which can be used with various data layers to enhance map readability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95", + "metadata": {}, + "outputs": [], + "source": [ + "for legend in builtin_legends:\n", + " print(legend)" + ] + }, + { + "cell_type": "markdown", + "id": "96", + "metadata": {}, + "source": [ + "Add an NLCD WMS layer along with its corresponding legend, which appears as an overlay, providing users with an informative legend display." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "97", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "m.add_basemap(\"NLCD 2021 CONUS Land Cover\")\n", + "m.add_legend(builtin_legend=\"NLCD\", max_width=\"100px\", height=\"455px\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "98", + "metadata": {}, + "source": [ + "Add an Earth Engine layer for NLCD land cover and display its legend, specifying title, legend type, and dimensions for user convenience." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "\n", + "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2021_REL/NLCD/2021\")\n", + "landcover = nlcd.select(\"landcover\")\n", + "\n", + "m.add_layer(landcover, {}, \"NLCD Land Cover 2021\")\n", + "m.add_legend(\n", + " title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\", height=\"455px\"\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "100", + "metadata": {}, + "source": [ + "#### Custom Legends\n", + "\n", + "Create a custom legend by defining unique labels and colors for each class, allowing for flexible map customization. This example uses a color palette for labeling several map categories." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "101", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(add_google_map=False)\n", + "\n", + "keys = [\"One\", \"Two\", \"Three\", \"Four\", \"etc\"]\n", + "\n", + "# colors can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n", + "colors = [\"#8DD3C7\", \"#FFFFB3\", \"#BEBADA\", \"#FB8072\", \"#80B1D3\"]\n", + "# legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)]\n", + "\n", + "m.add_legend(keys=keys, colors=colors, position=\"bottomright\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "102", + "metadata": {}, + "source": [ + "Define a custom legend using a dictionary that links specific colors to labels. This approach provides flexibility to represent specific categories in the data, such as various land cover types." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "103", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "\n", + "legend_dict = {\n", + " \"11 Open Water\": \"466b9f\",\n", + " \"12 Perennial Ice/Snow\": \"d1def8\",\n", + " \"21 Developed, Open Space\": \"dec5c5\",\n", + " \"22 Developed, Low Intensity\": \"d99282\",\n", + " \"23 Developed, Medium Intensity\": \"eb0000\",\n", + " \"24 Developed High Intensity\": \"ab0000\",\n", + " \"31 Barren Land (Rock/Sand/Clay)\": \"b3ac9f\",\n", + " \"41 Deciduous Forest\": \"68ab5f\",\n", + " \"42 Evergreen Forest\": \"1c5f2c\",\n", + " \"43 Mixed Forest\": \"b5c58f\",\n", + " \"51 Dwarf Scrub\": \"af963c\",\n", + " \"52 Shrub/Scrub\": \"ccb879\",\n", + " \"71 Grassland/Herbaceous\": \"dfdfc2\",\n", + " \"72 Sedge/Herbaceous\": \"d1d182\",\n", + " \"73 Lichens\": \"a3cc51\",\n", + " \"74 Moss\": \"82ba9e\",\n", + " \"81 Pasture/Hay\": \"dcd939\",\n", + " \"82 Cultivated Crops\": \"ab6c28\",\n", + " \"90 Woody Wetlands\": \"b8d9eb\",\n", + " \"95 Emergent Herbaceous Wetlands\": \"6c9fb8\",\n", + "}\n", + "\n", + "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2021_REL/NLCD/2021\")\n", + "landcover = nlcd.select(\"landcover\")\n", + "\n", + "m.add_layer(landcover, {}, \"NLCD Land Cover 2021\")\n", + "m.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "104", + "metadata": {}, + "source": [ + "### Color Bars\n", + "\n", + "Add a horizontal color bar representing elevation data. This example demonstrates how to add an SRTM elevation layer and overlay a color bar for quick visual reference of elevation values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "105", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", + "m.add_colorbar(vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "106", + "metadata": {}, + "source": [ + "Add a vertical color bar for elevation data, adjusting its orientation and dimensions to fit the map layout." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "107", + "metadata": {}, + "outputs": [], + "source": [ + "m.add_colorbar(\n", + " vis_params,\n", + " label=\"Elevation (m)\",\n", + " layer_name=\"SRTM DEM\",\n", + " orientation=\"vertical\",\n", + " max_width=\"100px\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "108", + "metadata": {}, + "source": [ + "Make the color bar’s background transparent, which improves visual integration with the map when adding color bars for data layers." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "109", + "metadata": {}, + "outputs": [], + "source": [ + "m.add_colorbar(\n", + " vis_params,\n", + " label=\"Elevation (m)\",\n", + " layer_name=\"SRTM DEM\",\n", + " orientation=\"vertical\",\n", + " max_width=\"100px\",\n", + " transparent_bg=True,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "110", + "metadata": {}, + "source": [ + "### Split-panel Maps\n", + "\n", + "Create a split-panel map with basemaps, allowing users to compare two different basemaps side by side." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "111", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.split_map(left_layer=\"Esri.WorldTopoMap\", right_layer=\"OpenTopoMap\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "112", + "metadata": {}, + "source": [ + "Use Earth Engine layers in a split-panel map to compare NLCD data from 2001 and 2021. This layout is effective for examining changes between datasets across time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "113", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=(40, -100), zoom=4, height=600)\n", + "\n", + "nlcd_2001 = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2001\").select(\"landcover\")\n", + "nlcd_2021 = ee.Image(\"USGS/NLCD_RELEASES/2021_REL/NLCD/2021\").select(\"landcover\")\n", + "\n", + "left_layer = geemap.ee_tile_layer(nlcd_2001, {}, \"NLCD 2001\")\n", + "right_layer = geemap.ee_tile_layer(nlcd_2021, {}, \"NLCD 2021\")\n", + "\n", + "m.split_map(left_layer, right_layer)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "114", + "metadata": {}, + "source": [ + "### Linked Maps\n", + "\n", + "Set up a 2x2 grid of linked maps showing Sentinel-2 imagery in different band combinations, ideal for comparing multiple visual perspectives. Note that this feature may not work in Colab." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "115", + "metadata": {}, + "outputs": [], + "source": [ + "image = (\n", + " ee.ImageCollection(\"COPERNICUS/S2_SR_HARMONIZED\")\n", + " .filterDate(\"2024-07-01\", \"2024-10-01\")\n", + " .filter(ee.Filter.lt(\"CLOUDY_PIXEL_PERCENTAGE\", 5))\n", + " .map(lambda img: img.divide(10000))\n", + " .median()\n", + ")\n", + "\n", + "vis_params = [\n", + " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n", + " {\"bands\": [\"B8\", \"B11\", \"B4\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n", + " {\"bands\": [\"B8\", \"B4\", \"B3\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n", + " {\"bands\": [\"B12\", \"B12\", \"B4\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n", + "]\n", + "\n", + "labels = [\n", + " \"Natural Color (B4/B3/B2)\",\n", + " \"Land/Water (B8/B11/B4)\",\n", + " \"Color Infrared (B8/B4/B3)\",\n", + " \"Vegetation (B12/B11/B4)\",\n", + "]\n", + "\n", + "geemap.linked_maps(\n", + " rows=2,\n", + " cols=2,\n", + " height=\"300px\",\n", + " center=[35.959111, -83.909463],\n", + " zoom=12,\n", + " ee_objects=[image],\n", + " vis_params=vis_params,\n", + " labels=labels,\n", + " label_position=\"topright\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "116", + "metadata": {}, + "source": [ + "### Timeseries Inspector\n", + "\n", + "Retrieve the available years in the NLCD collection for setting up a timeseries. This step helps confirm available time points for inspecting changes over time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "117", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "collection = ee.ImageCollection(\"USGS/NLCD_RELEASES/2019_REL/NLCD\").select(\"landcover\")\n", + "vis_params = {\"bands\": [\"landcover\"]}\n", + "years = collection.aggregate_array(\"system:index\").getInfo()\n", + "years" + ] + }, + { + "cell_type": "markdown", + "id": "118", + "metadata": {}, + "source": [ + "Use a timeseries inspector to compare changes in NLCD data across different years. This tool is ideal for temporal data visualization, showing how land cover changes in an interactive format." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "119", + "metadata": {}, + "outputs": [], + "source": [ + "m.ts_inspector(\n", + " left_ts=collection,\n", + " right_ts=collection,\n", + " left_names=years,\n", + " right_names=years,\n", + " left_vis=vis_params,\n", + " right_vis=vis_params,\n", + " width=\"80px\",\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "120", + "metadata": {}, + "source": [ + "### Time Slider\n", + "\n", + "Create a time slider to explore MODIS vegetation data, setting the slider to visualize NDVI values over a specific period. This feature allows users to track vegetation changes month-by-month." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "121", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "collection = (\n", + " ee.ImageCollection(\"MODIS/MCD43A4_006_NDVI\")\n", + " .filter(ee.Filter.date(\"2018-06-01\", \"2018-07-01\"))\n", + " .select(\"NDVI\")\n", + ")\n", + "vis_params = {\n", + " \"min\": 0.0,\n", + " \"max\": 1.0,\n", + " \"palette\": \"ndvi\",\n", + "}\n", + "\n", + "m.add_time_slider(collection, vis_params, time_interval=2)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "122", + "metadata": {}, + "source": [ + "Add a time slider for visualizing NOAA weather data over a 24-hour period, using color-coding to show temperature variations. The slider enables temporal exploration of hourly data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "123", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "collection = (\n", + " ee.ImageCollection(\"NOAA/GFS0P25\")\n", + " .filterDate(\"2018-12-22\", \"2018-12-23\")\n", + " .limit(24)\n", + " .select(\"temperature_2m_above_ground\")\n", + ")\n", + "\n", + "vis_params = {\n", + " \"min\": -40.0,\n", + " \"max\": 35.0,\n", + " \"palette\": [\"blue\", \"purple\", \"cyan\", \"green\", \"yellow\", \"red\"],\n", + "}\n", + "\n", + "labels = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n", + "m.add_time_slider(collection, vis_params, labels=labels, time_interval=1, opacity=0.8)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "124", + "metadata": {}, + "source": [ + "Add a time slider to visualize Sentinel-2 imagery with specific bands and cloud cover filtering. This feature enables temporal analysis of imagery data, allowing users to explore seasonal and other changes over time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "125", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "collection = (\n", + " ee.ImageCollection(\"COPERNICUS/S2_SR_HARMONIZED\")\n", + " .filterBounds(ee.Geometry.Point([-83.909463, 35.959111]))\n", + " .filterMetadata(\"CLOUDY_PIXEL_PERCENTAGE\", \"less_than\", 10)\n", + " .filter(ee.Filter.calendarRange(6, 8, \"month\"))\n", + ")\n", + "\n", + "vis_params = {\"min\": 0, \"max\": 4000, \"bands\": [\"B8\", \"B4\", \"B3\"]}\n", + "\n", + "m.add_time_slider(collection, vis_params)\n", + "m.set_center(-83.909463, 35.959111, 12)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "126", + "metadata": {}, + "source": [ + "## Processing of Vector Data\n", + "\n", + "### From GeoJSON\n", + "\n", + "Load GeoJSON data into an Earth Engine FeatureCollection. This example retrieves countries data from a remote URL, converting it to a FeatureCollection and visualizing it with a specified style." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "127", + "metadata": {}, + "outputs": [], + "source": [ + "in_geojson = \"https://github.com/gee-community/geemap/blob/master/examples/data/countries.geojson\"\n", + "m = geemap.Map()\n", + "fc = geemap.geojson_to_ee(in_geojson)\n", + "m.add_layer(fc.style(**{\"color\": \"ff0000\", \"fillColor\": \"00000000\"}), {}, \"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "128", + "metadata": {}, + "source": [ + "### From Shapefile\n", + "\n", + "Download and load a shapefile of country boundaries. The shapefile is converted to a FeatureCollection for visualization on the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "129", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/gee-community/geemap/blob/master/examples/data/countries.zip\"\n", + "geemap.download_file(url, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "130", + "metadata": {}, + "outputs": [], + "source": [ + "in_shp = \"countries.shp\"\n", + "fc = geemap.shp_to_ee(in_shp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "131", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_layer(fc, {}, \"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "132", + "metadata": {}, + "source": [ + "### From GeoDataFrame\n", + "\n", + "Read a shapefile into a GeoDataFrame using geopandas, then convert the GeoDataFrame to an Earth Engine FeatureCollection for mapping." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "133", + "metadata": {}, + "outputs": [], + "source": [ + "import geopandas as gpd\n", + "\n", + "gdf = gpd.read_file(in_shp)\n", + "fc = geemap.gdf_to_ee(gdf)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "134", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_layer(fc, {}, \"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "135", + "metadata": {}, + "source": [ + "### To GeoJSON\n", + "\n", + "Filter U.S. state data to select Tennessee and save it as a GeoJSON file, which can be shared or used in other GIS tools." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "136", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.eq(\"NAME\", \"Tennessee\"))\n", + "m.add_layer(fc, {}, \"Tennessee\")\n", + "m.center_object(fc, 7)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "137", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_geojson(fc, filename=\"Tennessee.geojson\")" + ] + }, + { + "cell_type": "markdown", + "id": "138", + "metadata": {}, + "source": [ + "### To Shapefile\n", + "\n", + "Export the filtered Tennessee FeatureCollection to a shapefile format for offline use or compatibility with desktop GIS software." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "139", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_shp(fc, filename=\"Tennessee.shp\")" + ] + }, + { + "cell_type": "markdown", + "id": "140", + "metadata": {}, + "source": [ + "### To GeoDataFrame\n", + "\n", + "Convert an Earth Engine FeatureCollection to a GeoDataFrame for further analysis in Python or use in interactive maps." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "141", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.ee_to_gdf(fc)\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "142", + "metadata": {}, + "outputs": [], + "source": [ + "gdf.explore()" + ] + }, + { + "cell_type": "markdown", + "id": "143", + "metadata": {}, + "source": [ + "### To DataFrame\n", + "\n", + "Transform an Earth Engine FeatureCollection into a pandas DataFrame, which can then be used for data analysis in Python." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "144", + "metadata": {}, + "outputs": [], + "source": [ + "df = geemap.ee_to_df(fc)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "145", + "metadata": {}, + "source": [ + "### To CSV\n", + "\n", + "Export the FeatureCollection data to a CSV file, useful for spreadsheet applications and data reporting." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "146", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_csv(fc, filename=\"Indiana.csv\")" + ] + }, + { + "cell_type": "markdown", + "id": "147", + "metadata": {}, + "source": [ + "## Processing of Raster Data\n", + "\n", + "### Extract Pixel Values\n", + "\n", + "#### Extracting Values to Points\n", + "\n", + "Load and visualize SRTM DEM and Landsat 7 imagery. Points from a shapefile of U.S. cities are added, and the tool extracts DEM values to these points, saving the results in a shapefile." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "148", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "\n", + "m.add_layer(\n", + " landsat7,\n", + " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2},\n", + " \"Landsat 7\",\n", + ")\n", + "m.add_layer(dem, vis_params, \"SRTM DEM\", True, 1)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "149", + "metadata": {}, + "outputs": [], + "source": [ + "in_shp = \"us_cities.shp\"\n", + "url = \"https://github.com/giswqs/data/raw/main/us/us_cities.zip\"\n", + "geemap.download_file(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150", + "metadata": {}, + "outputs": [], + "source": [ + "in_fc = geemap.shp_to_ee(in_shp)\n", + "m.add_layer(in_fc, {}, \"Cities\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "151", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.extract_values_to_points(in_fc, dem, out_fc=\"dem.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "152", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.shp_to_gdf(\"dem.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "153", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.extract_values_to_points(in_fc, landsat7, \"landsat.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "154", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_df(\"landsat.csv\")" + ] + }, + { + "cell_type": "markdown", + "id": "155", + "metadata": {}, + "source": [ + "#### Extracting Pixel Values Along a Transect\n", + "\n", + "Visualize SRTM DEM with a terrain basemap. Define a line transect, either interactively or manually, and extract elevation values along this line. Display the elevation profile in a line chart, then export it as a CSV file for further analysis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "156", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_basemap(\"TERRAIN\")\n", + "\n", + "image = ee.Image(\"USGS/SRTMGL1_003\")\n", + "vis_params = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "m.add_layer(image, vis_params, \"SRTM DEM\", True, 0.5)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157", + "metadata": {}, + "outputs": [], + "source": [ + "line = m.user_roi\n", + "if line is None:\n", + " line = ee.Geometry.LineString(\n", + " [[-120.2232, 36.3148], [-118.9269, 36.7121], [-117.2022, 36.7562]]\n", + " )\n", + " m.add_layer(line, {}, \"ROI\")\n", + "m.centerObject(line)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "158", + "metadata": {}, + "outputs": [], + "source": [ + "reducer = \"mean\"\n", + "transect = geemap.extract_transect(\n", + " image, line, n_segments=100, reducer=reducer, to_pandas=True\n", + ")\n", + "transect" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "159", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.line_chart(\n", + " data=transect,\n", + " x=\"distance\",\n", + " y=\"mean\",\n", + " markers=True,\n", + " x_label=\"Distance (m)\",\n", + " y_label=\"Elevation (m)\",\n", + " height=400,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "160", + "metadata": {}, + "outputs": [], + "source": [ + "transect.to_csv(\"transect.csv\")" + ] + }, + { + "cell_type": "markdown", + "id": "161", + "metadata": {}, + "source": [ + "### Zonal Statistics\n", + "\n", + "#### Zonal Statistics with an Image and a Feature Collection\n", + "\n", + "This section demonstrates the use of zonal statistics to calculate the mean elevation values within U.S. state boundaries using NASA’s SRTM DEM data and a 5-year Landsat composite. The `geemap.zonal_stats` function exports results to CSV files for analysis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "162", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "\n", + "# Add NASA SRTM\n", + "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", + "dem_vis = {\n", + " \"min\": 0,\n", + " \"max\": 4000,\n", + " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", + "}\n", + "m.add_layer(dem, dem_vis, \"SRTM DEM\")\n", + "\n", + "# Add 5-year Landsat TOA composite\n", + "landsat = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n", + "m.add_layer(landsat, landsat_vis, \"Landsat\", False)\n", + "\n", + "# Add US Census States\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "style = {\"fillColor\": \"00000000\"}\n", + "m.add_layer(states.style(**style), {}, \"US States\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "163", + "metadata": {}, + "outputs": [], + "source": [ + "out_dem_stats = \"dem_stats.csv\"\n", + "geemap.zonal_stats(\n", + " dem, states, out_dem_stats, statistics_type=\"MEAN\", scale=1000, return_fc=False\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "164", + "metadata": {}, + "outputs": [], + "source": [ + "out_landsat_stats = \"landsat_stats.csv\"\n", + "geemap.zonal_stats(\n", + " landsat,\n", + " states,\n", + " out_landsat_stats,\n", + " statistics_type=\"MEAN\",\n", + " scale=1000,\n", + " return_fc=False,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "165", + "metadata": {}, + "source": [ + "#### Zonal Statistics by Group\n", + "\n", + "Here, zonal statistics are applied to NLCD land cover data, calculating the area of each land cover type within each U.S. state. The results are saved to CSV files as both raw totals and percentages. This provides insights into the spatial distribution of land cover categories across states." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "166", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "\n", + "# Add NLCD data\n", + "dataset = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n", + "landcover = dataset.select(\"landcover\")\n", + "m.add_layer(landcover, {}, \"NLCD 2019\")\n", + "\n", + "# Add US census states\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "style = {\"fillColor\": \"00000000\"}\n", + "m.add_layer(states.style(**style), {}, \"US States\")\n", + "\n", + "# Add NLCD legend\n", + "m.add_legend(title=\"NLCD Land Cover\", builtin_legend=\"NLCD\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "167", + "metadata": {}, + "outputs": [], + "source": [ + "nlcd_stats = \"nlcd_stats.csv\"\n", + "\n", + "geemap.zonal_stats_by_group(\n", + " landcover,\n", + " states,\n", + " nlcd_stats,\n", + " statistics_type=\"SUM\",\n", + " denominator=1e6,\n", + " decimal_places=2,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "168", + "metadata": {}, + "outputs": [], + "source": [ + "nlcd_stats = \"nlcd_stats_pct.csv\"\n", + "\n", + "geemap.zonal_stats_by_group(\n", + " landcover,\n", + " states,\n", + " nlcd_stats,\n", + " statistics_type=\"PERCENTAGE\",\n", + " denominator=1e6,\n", + " decimal_places=2,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "169", + "metadata": {}, + "source": [ + "#### Zonal Statistics with Two Images\n", + "\n", + "This example calculates the mean elevation values within different NLCD land cover types using DEM and NLCD data. The `geemap.image_stats_by_zone` function provides summary statistics (e.g., mean and standard deviation), which can be exported to CSV files for further analysis or visualization." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "170", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "dem = ee.Image(\"USGS/3DEP/10m\")\n", + "vis = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n", + "m.add_layer(dem, vis, \"DEM\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "171", + "metadata": {}, + "outputs": [], + "source": [ + "landcover = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select(\"landcover\")\n", + "m.add_layer(landcover, {}, \"NLCD 2019\")\n", + "m.add_legend(title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "172", + "metadata": {}, + "outputs": [], + "source": [ + "stats = geemap.image_stats_by_zone(dem, landcover, reducer=\"MEAN\")\n", + "stats" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "173", + "metadata": {}, + "outputs": [], + "source": [ + "stats.to_csv(\"mean.csv\", index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "174", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.image_stats_by_zone(dem, landcover, out_csv=\"std.csv\", reducer=\"STD\")" + ] + }, + { + "cell_type": "markdown", + "id": "175", + "metadata": {}, + "source": [ + "### Map Algebra\n", + "\n", + "This example demonstrates basic map algebra by computing the Normalized Difference Vegetation Index (NDVI) for a 5-year Landsat composite and the Enhanced Vegetation Index (EVI) for a Landsat 8 image. These indices are visualized with color scales to highlight areas of vegetation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "176", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "# Load a 5-year Landsat 7 composite 1999-2003.\n", + "landsat_1999 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", + "\n", + "# Compute NDVI.\n", + "ndvi_1999 = (\n", + " landsat_1999.select(\"B4\")\n", + " .subtract(landsat_1999.select(\"B3\"))\n", + " .divide(landsat_1999.select(\"B4\").add(landsat_1999.select(\"B3\")))\n", + ")\n", + "\n", + "vis = {\"min\": 0, \"max\": 1, \"palette\": \"ndvi\"}\n", + "m.add_layer(ndvi_1999, vis, \"NDVI\")\n", + "m.add_colorbar(vis, label=\"NDVI\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "177", + "metadata": {}, + "outputs": [], + "source": [ + "# Load a Landsat 8 image.\n", + "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\")\n", + "\n", + "# Compute the EVI using an expression.\n", + "evi = image.expression(\n", + " \"2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))\",\n", + " {\n", + " \"NIR\": image.select(\"B5\"),\n", + " \"RED\": image.select(\"B4\"),\n", + " \"BLUE\": image.select(\"B2\"),\n", + " },\n", + ")\n", + "\n", + "# Define a map centered on San Francisco Bay.\n", + "m = geemap.Map(center=[37.4675, -122.1363], zoom=9)\n", + "\n", + "vis = {\"min\": 0, \"max\": 1, \"palette\": \"ndvi\"}\n", + "m.add_layer(evi, vis, \"EVI\")\n", + "m.add_colorbar(vis, label=\"EVI\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "178", + "metadata": {}, + "source": [ + "## Working with Local Geospatial Data\n", + "\n", + "### Raster Data\n", + "\n", + "Single-band and multi-band raster data can be loaded from local files. Here, a digital elevation model (DEM) is loaded and displayed with a terrain color scheme, and another raster is displayed with a false-color composite to highlight different features.\n", + "\n", + "#### Single-Band Raster Data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "179", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/srtm90.tif\"\n", + "filename = \"dem.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "markdown", + "id": "180", + "metadata": {}, + "source": [ + "#### Multi-Band Raster Data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "181", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, cmap=\"terrain\", layer_name=\"DEM\")\n", + "vis_params = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n", + "m.add_colorbar(vis_params, label=\"Elevation (m)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "182", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/cog.tif\"\n", + "filename = \"cog.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "183", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, indexes=[4, 1, 2], layer_name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "184", + "metadata": {}, + "source": [ + "### Vector Data\n", + "\n", + "Various vector data formats can be loaded and visualized with geemap, including GeoJSON, Shapefile, GeoDataFrame, and GeoPackage formats. GeoJSON data is styled dynamically with custom colors based on attributes, while Shapefiles and GeoDataFrames allow for simple, structured addition of geographic features to the map.\n", + "\n", + "#### GeoJSON" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "185", + "metadata": {}, + "outputs": [], + "source": [ + "in_geojson = (\n", + " \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n", + ")\n", + "m = geemap.Map()\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", info_mode=\"on_hover\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "186", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_basemap(\"CartoDB.DarkMatter\")\n", + "callback = lambda feat: {\"color\": feat[\"properties\"][\"color\"], \"weight\": 2}\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", style_callback=callback)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "187", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.geojson\"\n", + "m = geemap.Map()\n", + "m.add_geojson(\n", + " url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "188", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "m = geemap.Map()\n", + "\n", + "\n", + "def random_color(feature):\n", + " return {\n", + " \"color\": \"black\",\n", + " \"weight\": 3,\n", + " \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n", + " }\n", + "\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "189", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "style = {\n", + " \"stroke\": True,\n", + " \"color\": \"#0000ff\",\n", + " \"weight\": 2,\n", + " \"opacity\": 1,\n", + " \"fill\": True,\n", + " \"fillColor\": \"#0000ff\",\n", + " \"fillOpacity\": 0.1,\n", + "}\n", + "\n", + "hover_style = {\"fillOpacity\": 0.7}\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "190", + "metadata": {}, + "source": [ + "#### Shapefile" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "191", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.zip\"\n", + "geemap.download_file(url, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "192", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "in_shp = \"countries.shp\"\n", + "m.add_shp(in_shp, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "193", + "metadata": {}, + "source": [ + "#### GeoDataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "194", + "metadata": {}, + "outputs": [], + "source": [ + "import geopandas as gpd\n", + "\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "gdf = gpd.read_file(\"countries.shp\")\n", + "m.add_gdf(gdf, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "195", + "metadata": {}, + "source": [ + "#### GeoPackage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "196", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "data = \"https://github.com/opengeos/datasets/releases/download/world/countries.gpkg\"\n", + "m.add_vector(data, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "197", + "metadata": {}, + "source": [ + "#### CSV to Vector\n", + "\n", + "Geemap enables easy conversion from CSV files to vector data formats like GeoJSON, Shapefile, and GeoPackage. The data from a CSV is visualized on a map, where cities are displayed with markers styled by region, and a legend is added for clear reference." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "198", + "metadata": {}, + "outputs": [], + "source": [ + "data = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + "geemap.csv_to_df(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "199", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_geojson(\n", + " data, \"cities.geojson\", latitude=\"latitude\", longitude=\"longitude\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "200", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_shp(data, \"cities.shp\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "201", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_vector(data, \"cities.gpkg\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "202", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.csv_to_gdf(data, latitude=\"latitude\", longitude=\"longitude\")\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "203", + "metadata": {}, + "outputs": [], + "source": [ + "cities = (\n", + " \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + ")\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_points_from_xy(cities, x=\"longitude\", y=\"latitude\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "204", + "metadata": {}, + "outputs": [], + "source": [ + "regions = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_regions.geojson\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "205", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_geojson(regions, layer_name=\"US Regions\")\n", + "m.add_points_from_xy(\n", + " cities,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " layer_name=\"US Cities\",\n", + " color_column=\"region\",\n", + " icon_names=[\"gear\", \"map\", \"leaf\", \"globe\"],\n", + " spin=True,\n", + " add_legend=True,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "206", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_circle_markers_from_xy(\n", + " data,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " radius=8,\n", + " color=\"blue\",\n", + " fill_color=\"black\",\n", + " fill_opacity=0.5,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "207", + "metadata": {}, + "source": [ + "## Accessing Cloud Optimized GeoTIFFs\n", + "\n", + "### Cloud Optimized GeoTIFFs (COG)\n", + "\n", + "This section shows how to add Cloud Optimized GeoTIFF (COG) layers to a map using URLs, which allows for efficient loading and visualization of large raster files stored on the cloud. In this example, a pre-event image of the 2020 California wildfire is added to a map, allowing for remote sensing analysis without local file storage." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "208", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "url = \"https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif\"\n", + "m.add_cog_layer(url, name=\"Fire (pre-event)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "209", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_center(url)" + ] + }, + { + "cell_type": "markdown", + "id": "210", + "metadata": {}, + "source": [ + "The COG functions also allow inspection of data, such as viewing the bounds and available bands in the GeoTIFF file. The pre- and post-event images can be compared in a split map view to analyze changes due to the fire." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "211", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "212", + "metadata": {}, + "outputs": [], + "source": [ + "url2 = \"https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-14/pine-gulch-fire20/10300100AAC8DD00.tif\"\n", + "m.add_cog_layer(url2, name=\"Fire (post-event)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "213", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[39.4568, -108.5107], zoom=12)\n", + "m.split_map(left_layer=url2, right_layer=url)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "214", + "metadata": {}, + "source": [ + "### SpatioTemporal Asset Catalog (STAC)\n", + "\n", + "STAC collections can be accessed and visualized similarly, enabling dynamic mapping of spatiotemporal data. By specifying STAC URLs, you can view dataset bounds, center the map on a dataset, and select specific bands to add as layers. This example shows adding panchromatic and false-color imagery for enhanced visual analysis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "215", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://tinyurl.com/22vptbws\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "216", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bounds(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "217", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_center(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "218", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "219", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_stac_layer(url, bands=[\"pan\"], name=\"Panchromatic\")\n", + "m.add_stac_layer(url, bands=[\"B3\", \"B2\", \"B1\"], name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "220", + "metadata": {}, + "source": [ + "## Exporting Earth Engine Data\n", + "\n", + "### Exporting Images\n", + "\n", + "This section demonstrates exporting Earth Engine images. First, a Landsat image is added to the map for visualization, and a rectangular region of interest (ROI) is specified. Exporting options include saving the image locally with specified scale and region or exporting to Google Drive. It’s possible to define custom CRS and transformation settings when saving the image.\n", + "\n", + "\n", + "Add a Landsat image to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "221", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n", + " [\"B5\", \"B4\", \"B3\"]\n", + ")\n", + "\n", + "vis_params = {\"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n", + "\n", + "m.center_object(image)\n", + "m.add_layer(image, vis_params, \"Landsat\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "222", + "metadata": {}, + "source": [ + "Add a rectangle to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "223", + "metadata": {}, + "outputs": [], + "source": [ + "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n", + "fc = ee.FeatureCollection(region)\n", + "style = {\"color\": \"ffff00ff\", \"fillColor\": \"00000000\"}\n", + "m.add_layer(fc.style(**style), {}, \"ROI\")" + ] + }, + { + "cell_type": "markdown", + "id": "224", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "225", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)" + ] + }, + { + "cell_type": "markdown", + "id": "226", + "metadata": {}, + "source": [ + "Check image projection." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "227", + "metadata": {}, + "outputs": [], + "source": [ + "projection = image.select(0).projection().getInfo()\n", + "projection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "228", + "metadata": {}, + "outputs": [], + "source": [ + "crs = projection[\"crs\"]\n", + "crs_transform = projection[\"transform\"]" + ] + }, + { + "cell_type": "markdown", + "id": "229", + "metadata": {}, + "source": [ + "Specify region, crs, and crs_transform." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "230", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(\n", + " image,\n", + " filename=\"landsat_crs.tif\",\n", + " crs=crs,\n", + " crs_transform=crs_transform,\n", + " region=region,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "231", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "232", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_to_drive(\n", + " image, description=\"landsat\", folder=\"export\", region=region, scale=30\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "233", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.download_ee_image(image, \"landsat.tif\", scale=90)" + ] + }, + { + "cell_type": "markdown", + "id": "234", + "metadata": {}, + "source": [ + "### Exporting Image Collections\n", + "\n", + "Image collections, like time series data, can be filtered and exported as multiple images. Here, a National Agriculture Imagery Program (NAIP) collection is filtered by date and location. The collection can then be saved locally or sent to Google Drive, making it easier to handle large datasets." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "235", + "metadata": {}, + "outputs": [], + "source": [ + "point = ee.Geometry.Point(-99.2222, 46.7816)\n", + "collection = (\n", + " ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n", + " .filterBounds(point)\n", + " .filterDate(\"2008-01-01\", \"2018-01-01\")\n", + " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "236", + "metadata": {}, + "outputs": [], + "source": [ + "collection.aggregate_array(\"system:index\")" + ] + }, + { + "cell_type": "markdown", + "id": "237", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "238", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection(collection, out_dir=\".\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "239", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "240", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "241", + "metadata": {}, + "source": [ + "### Exporting Feature Collections\n", + "\n", + "Feature collections, such as state boundaries, are exportable in multiple formats (e.g., Shapefile, GeoJSON, and CSV). This example exports the Alaska state boundary as different vector formats both locally and to Google Drive. Additionally, exported data can be directly loaded into a GeoDataFrame for further manipulation in Python." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "242", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.eq(\"NAME\", \"Alaska\"))\n", + "m.add_layer(fc, {}, \"Alaska\")\n", + "m.center_object(fc, 4)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "243", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "244", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_shp(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "245", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "246", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_geojson(fc, filename=\"Alaska.geojson\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "247", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_csv(fc, filename=\"Alaska.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "248", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.ee_to_gdf(fc)\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "249", + "metadata": {}, + "outputs": [], + "source": [ + "df = geemap.ee_to_df(fc)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "250", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "251", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector_to_drive(\n", + " fc, description=\"Alaska\", fileFormat=\"SHP\", folder=\"export\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "252", + "metadata": {}, + "source": [ + "## Creating Timelapse Animations\n", + "\n", + "### Landsat Timelapse\n", + "\n", + "The Landsat timelapse tool creates animations of changes over time. Here, we use a defined region of interest (ROI) to generate timelapse GIFs for Las Vegas (1984-2023):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "253", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "254", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"las_vegas.gif\",\n", + " start_year=1984,\n", + " end_year=2023,\n", + " bands=[\"NIR\", \"Red\", \"Green\"],\n", + " frames_per_second=5,\n", + " title=\"Las Vegas, NV\",\n", + " font_color=\"blue\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "255", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "256", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"hong_kong.gif\",\n", + " start_year=1990,\n", + " end_year=2022,\n", + " start_date=\"01-01\",\n", + " end_date=\"12-31\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Hong Kong\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "257", + "metadata": {}, + "source": [ + "### Sentinel-2 Timelapse\n", + "\n", + "This example generates a timelapse of Sentinel-2 data for a selected ROI, with options to draw a custom ROI on the map. The timelapse spans 2017-2023, focusing on the June to September period each year." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "258", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[41.718934, -86.894547], zoom=12)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "259", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "260", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-87.0492, 41.6545, -86.7903, 41.7604)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "261", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.sentinel2_timelapse(\n", + " roi,\n", + " out_gif=\"sentinel2.gif\",\n", + " start_year=2017,\n", + " end_year=2023,\n", + " start_date=\"06-01\",\n", + " end_date=\"09-01\",\n", + " frequency=\"year\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Sentinel-2 Timelapse\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "262", + "metadata": {}, + "source": [ + "### MODIS Timelapse\n", + "\n", + "The MODIS timelapse can showcase NDVI (vegetation index) or ocean color data over time. In the vegetation example, we visualize NDVI from 2000-2022 with country overlays. The temperature example animates Aqua satellite data for monthly temperature trends from 2018-2020 with continent overlays." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "263", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "264", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "265", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ndvi_timelapse(\n", + " roi,\n", + " out_gif=\"ndvi.gif\",\n", + " data=\"Terra\",\n", + " band=\"NDVI\",\n", + " start_date=\"2000-01-01\",\n", + " end_date=\"2022-12-31\",\n", + " frames_per_second=3,\n", + " title=\"MODIS NDVI Timelapse\",\n", + " overlay_data=\"countries\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "266", + "metadata": {}, + "source": [ + "MODIS temperature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "267", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "268", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-171.21, -57.13, 177.53, 79.99)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "269", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ocean_color_timelapse(\n", + " satellite=\"Aqua\",\n", + " start_date=\"2018-01-01\",\n", + " end_date=\"2020-12-31\",\n", + " roi=roi,\n", + " frequency=\"month\",\n", + " out_gif=\"temperature.gif\",\n", + " overlay_data=\"continents\",\n", + " overlay_color=\"yellow\",\n", + " overlay_opacity=0.5,\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "270", + "metadata": {}, + "source": [ + "### GOES Timelapse\n", + "\n", + "GOES timelapse generation can animate atmospheric phenomena in near real-time. We create animations for different ROIs, including hurricane tracking and fire events using GOES-17 data with custom time windows and frame rates." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "271", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411)\n", + "start_date = \"2022-01-15T03:00:00\"\n", + "end_date = \"2022-01-15T07:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "272", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"goes.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "273", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088)\n", + "start_date = \"2021-10-24T14:00:00\"\n", + "end_date = \"2021-10-25T01:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "274", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"hurricane.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "275", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490)\n", + "start_date = \"2020-09-05T15:00:00\"\n", + "end_date = \"2020-09-06T02:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "276", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_fire_timelapse(\n", + " roi, \"fire.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "277", + "metadata": {}, + "source": [ + "## Charting\n", + "\n", + "### Charting Features\n", + "\n", + "#### Import libraries\n", + "\n", + "To create charts for Earth Engine features, import required libraries like `calendar`, `ee`, `geemap`, and `chart`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "278", + "metadata": {}, + "outputs": [], + "source": [ + "import calendar\n", + "import ee\n", + "import geemap\n", + "from geemap import chart" + ] + }, + { + "cell_type": "markdown", + "id": "279", + "metadata": {}, + "source": [ + "Initialize Earth Engine using `geemap.ee_initialize()`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "280", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_initialize()" + ] + }, + { + "cell_type": "markdown", + "id": "281", + "metadata": {}, + "source": [ + "#### feature_by_feature\n", + "\n", + "This function allows feature plotting along the x-axis, with values from selected properties represented along the y-axis. Ecoregions data is used to chart average monthly temperature across regions, where months serve as series columns." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "282", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "features = ecoregions.select(\"[0-9][0-9]_tmean|label\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "283", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_df(features)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "284", + "metadata": {}, + "outputs": [], + "source": [ + "x_property = \"label\"\n", + "y_properties = [str(x).zfill(2) + \"_tmean\" for x in range(1, 13)]\n", + "\n", + "labels = calendar.month_abbr[1:] # a list of month labels, e.g. ['Jan', 'Feb', ...]\n", + "\n", + "colors = [\n", + " \"#604791\",\n", + " \"#1d6b99\",\n", + " \"#39a8a7\",\n", + " \"#0f8755\",\n", + " \"#76b349\",\n", + " \"#f0af07\",\n", + " \"#e37d05\",\n", + " \"#cf513e\",\n", + " \"#96356f\",\n", + " \"#724173\",\n", + " \"#9c4f97\",\n", + " \"#696969\",\n", + "]\n", + "title = \"Average Monthly Temperature by Ecoregion\"\n", + "x_label = \"Ecoregion\"\n", + "y_label = \"Temperature\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "285", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.feature_by_feature(\n", + " features,\n", + " x_property,\n", + " y_properties,\n", + " colors=colors,\n", + " labels=labels,\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "286", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/MZa99Vf.png)" + ] + }, + { + "cell_type": "markdown", + "id": "287", + "metadata": {}, + "source": [ + "#### feature.by_property\n", + "\n", + "Displays average precipitation by month for each ecoregion. Properties represent precipitation values for each month, allowing for visual comparison across regions by month." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "288", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "features = ecoregions.select(\"[0-9][0-9]_ppt|label\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "289", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_df(features)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "290", + "metadata": {}, + "outputs": [], + "source": [ + "keys = [str(x).zfill(2) + \"_ppt\" for x in range(1, 13)]\n", + "values = calendar.month_abbr[1:] # a list of month labels, e.g. ['Jan', 'Feb', ...]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "291", + "metadata": {}, + "outputs": [], + "source": [ + "x_properties = dict(zip(keys, values))\n", + "series_property = \"label\"\n", + "title = \"Average Ecoregion Precipitation by Month\"\n", + "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "292", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.feature_by_property(\n", + " features,\n", + " x_properties,\n", + " series_property,\n", + " title=title,\n", + " colors=colors,\n", + " x_label=\"Month\",\n", + " y_label=\"Precipitation (mm)\",\n", + " legend_location=\"top-left\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "293", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/6RhuUc7.png)" + ] + }, + { + "cell_type": "markdown", + "id": "294", + "metadata": {}, + "source": [ + "#### feature_groups\n", + "\n", + "Plots groups of features based on property values, showing average January temperature for ecoregions divided into \"Warm\" and \"Cold\" categories." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "295", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "features = ecoregions.select(\"[0-9][0-9]_ppt|label\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "296", + "metadata": {}, + "outputs": [], + "source": [ + "features = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "x_property = \"label\"\n", + "y_property = \"01_tmean\"\n", + "series_property = \"warm\"\n", + "title = \"Average January Temperature by Ecoregion\"\n", + "colors = [\"#cf513e\", \"#1d6b99\"]\n", + "labels = [\"Warm\", \"Cold\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "297", + "metadata": {}, + "outputs": [], + "source": [ + "chart.feature_groups(\n", + " features,\n", + " x_property,\n", + " y_property,\n", + " series_property,\n", + " title=title,\n", + " colors=colors,\n", + " x_label=\"Ecoregion\",\n", + " y_label=\"January Temperature (°C)\",\n", + " legend_location=\"top-right\",\n", + " labels=labels,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "298", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/YFZlJtc.png)" + ] + }, + { + "cell_type": "markdown", + "id": "299", + "metadata": {}, + "source": [ + "#### feature_histogram\n", + "\n", + "Generates a histogram displaying July precipitation distribution across a region, showing pixel count for different precipitation levels, useful for understanding data spread." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "300", + "metadata": {}, + "outputs": [], + "source": [ + "source = ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands()\n", + "region = ee.Geometry.Rectangle(-123.41, 40.43, -116.38, 45.14)\n", + "features = source.sample(region, 5000)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "301", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_df(features.limit(5).select([\"07_ppt\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "302", + "metadata": {}, + "outputs": [], + "source": [ + "property = \"07_ppt\"\n", + "title = \"July Precipitation Distribution for NW USA\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "303", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.feature_histogram(\n", + " features,\n", + " property,\n", + " max_buckets=None,\n", + " title=title,\n", + " x_label=\"Precipitation (mm)\",\n", + " y_label=\"Pixel Count\",\n", + " colors=[\"#1d6b99\"],\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "304", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/ErIp7Oy.png)\n", + "\n", + "### Charting Images\n", + "\n", + "#### image_by_region\n", + "\n", + "Displays monthly temperature for each ecoregion using the `image_by_region` function, aggregating data to visualize average temperature by month across regions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "305", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "image = (\n", + " ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands().select(\"[0-9][0-9]_tmean\")\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "306", + "metadata": {}, + "outputs": [], + "source": [ + "labels = calendar.month_abbr[1:] # a list of month labels, e.g. ['Jan', 'Feb', ...]\n", + "title = \"Average Monthly Temperature by Ecoregion\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "307", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_by_region(\n", + " image,\n", + " ecoregions,\n", + " reducer=\"mean\",\n", + " scale=500,\n", + " x_property=\"label\",\n", + " title=title,\n", + " x_label=\"Ecoregion\",\n", + " y_label=\"Temperature\",\n", + " labels=labels,\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "308", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/y4rp3dK.png)" + ] + }, + { + "cell_type": "markdown", + "id": "309", + "metadata": {}, + "source": [ + "#### image_regions\n", + "\n", + "\n", + "Generates a chart showing average monthly precipitation across regions, using properties to represent months and comparing precipitation levels." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "310", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "image = (\n", + " ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands().select(\"[0-9][0-9]_ppt\")\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "311", + "metadata": {}, + "outputs": [], + "source": [ + "keys = [str(x).zfill(2) + \"_ppt\" for x in range(1, 13)]\n", + "values = calendar.month_abbr[1:] # a list of month labels, e.g. ['Jan', 'Feb', ...]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "312", + "metadata": {}, + "outputs": [], + "source": [ + "x_properties = dict(zip(keys, values))\n", + "title = \"Average Ecoregion Precipitation by Month\"\n", + "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "313", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_regions(\n", + " image,\n", + " ecoregions,\n", + " reducer=\"mean\",\n", + " scale=500,\n", + " series_property=\"label\",\n", + " x_labels=x_properties,\n", + " title=title,\n", + " colors=colors,\n", + " x_label=\"Month\",\n", + " y_label=\"Precipitation (mm)\",\n", + " legend_location=\"top-left\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "314", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/5WJVCNY.png)" + ] + }, + { + "cell_type": "markdown", + "id": "315", + "metadata": {}, + "source": [ + "#### image_by_class\n", + "\n", + "Displays spectral signatures of ecoregions by wavelength, showing reflectance values across spectral bands for comparison." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "316", + "metadata": {}, + "outputs": [], + "source": [ + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "\n", + "image = (\n", + " ee.ImageCollection(\"MODIS/061/MOD09A1\")\n", + " .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n", + " .select(\"sur_refl_b0[0-7]\")\n", + " .mean()\n", + " .select([2, 3, 0, 1, 4, 5, 6])\n", + ")\n", + "\n", + "wavelengths = [469, 555, 655, 858, 1240, 1640, 2130]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "317", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_by_class(\n", + " image,\n", + " class_band=\"label\",\n", + " region=ecoregions,\n", + " reducer=\"MEAN\",\n", + " scale=500,\n", + " x_labels=wavelengths,\n", + " title=\"Ecoregion Spectral Signatures\",\n", + " x_label=\"Wavelength (nm)\",\n", + " y_label=\"Reflectance (x1e4)\",\n", + " colors=[\"#f0af07\", \"#0f8755\", \"#76b349\"],\n", + " legend_location=\"top-left\",\n", + " interpolation=\"basis\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "318", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/XqYHvBV.png)" + ] + }, + { + "cell_type": "markdown", + "id": "319", + "metadata": {}, + "source": [ + "#### image_histogram\n", + "\n", + "Generates histograms to visualize MODIS surface reflectance distribution across red, NIR, and SWIR bands, providing insights into data distribution by band." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "320", + "metadata": {}, + "outputs": [], + "source": [ + "image = (\n", + " ee.ImageCollection(\"MODIS/061/MOD09A1\")\n", + " .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n", + " .select([\"sur_refl_b01\", \"sur_refl_b02\", \"sur_refl_b06\"])\n", + " .mean()\n", + ")\n", + "\n", + "region = ee.Geometry.Rectangle([-112.60, 40.60, -111.18, 41.22])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "321", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_histogram(\n", + " image,\n", + " region,\n", + " scale=500,\n", + " max_buckets=200,\n", + " min_bucket_width=1.0,\n", + " max_raw=1000,\n", + " max_pixels=int(1e6),\n", + " title=\"MODIS SR Reflectance Histogram\",\n", + " labels=[\"Red\", \"NIR\", \"SWIR\"],\n", + " colors=[\"#cf513e\", \"#1d6b99\", \"#f0af07\"],\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "322", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/mY4yoYH.png)\n", + "\n", + "### Charting Image Collections\n", + "\n", + "#### image_series\n", + "\n", + "Creates a time series chart of vegetation indices (NDVI and EVI) for a forest region, helping to track vegetation health over time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "323", + "metadata": {}, + "outputs": [], + "source": [ + "# Define the forest feature collection.\n", + "forest = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n", + " ee.Filter.eq(\"label\", \"Forest\")\n", + ")\n", + "\n", + "# Load MODIS vegetation indices data and subset a decade of images.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n", + " .select([\"NDVI\", \"EVI\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "324", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Average Vegetation Index Value by Date for Forest\"\n", + "x_label = \"Year\"\n", + "y_label = \"Vegetation index (x1e4)\"\n", + "colors = [\"#e37d05\", \"#1d6b99\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "325", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_series(\n", + " veg_indices,\n", + " region=forest,\n", + " reducer=ee.Reducer.mean(),\n", + " scale=500,\n", + " x_property=\"system:time_start\",\n", + " chart_type=\"LineChart\",\n", + " x_cols=\"date\",\n", + " y_cols=[\"NDVI\", \"EVI\"],\n", + " colors=colors,\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + " legend_location=\"right\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "326", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/r9zSJh6.png)" + ] + }, + { + "cell_type": "markdown", + "id": "327", + "metadata": {}, + "source": [ + "#### image_series_by_region\n", + "\n", + "Shows NDVI time series by region, comparing desert, forest, and grassland areas. Each region has a unique series for easy comparison." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "328", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection.\n", + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "\n", + "# Load MODIS vegetation indices data and subset a decade of images.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n", + " .select([\"NDVI\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "329", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Average NDVI Value by Date\"\n", + "x_label = \"Date\"\n", + "y_label = \"NDVI (x1e4)\"\n", + "x_cols = \"index\"\n", + "y_cols = [\"Desert\", \"Forest\", \"Grassland\"]\n", + "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "330", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_series_by_region(\n", + " veg_indices,\n", + " regions=ecoregions,\n", + " reducer=ee.Reducer.mean(),\n", + " band=\"NDVI\",\n", + " scale=500,\n", + " x_property=\"system:time_start\",\n", + " series_property=\"label\",\n", + " chart_type=\"LineChart\",\n", + " x_cols=x_cols,\n", + " y_cols=y_cols,\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + " colors=colors,\n", + " stroke_width=3,\n", + " legend_location=\"bottom-left\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "331", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/rnILSfI.png)" + ] + }, + { + "cell_type": "markdown", + "id": "332", + "metadata": {}, + "source": [ + "#### image_doy_series\n", + "\n", + "Plots average vegetation index by day of year for a specific region, showing seasonal vegetation patterns over a decade." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "333", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection and subset the grassland feature.\n", + "grassland = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n", + " ee.Filter.eq(\"label\", \"Grassland\")\n", + ")\n", + "\n", + "# Load MODIS vegetation indices data and subset a decade of images.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n", + " .select([\"NDVI\", \"EVI\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "334", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n", + "x_label = \"Day of Year\"\n", + "y_label = \"Vegetation Index (x1e4)\"\n", + "colors = [\"#f0af07\", \"#0f8755\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "335", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_doy_series(\n", + " image_collection=veg_indices,\n", + " region=grassland,\n", + " scale=500,\n", + " chart_type=\"LineChart\",\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + " colors=colors,\n", + " stroke_width=5,\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "336", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/F0z088e.png)" + ] + }, + { + "cell_type": "markdown", + "id": "337", + "metadata": {}, + "source": [ + "#### image_doy_series_by_year\n", + "\n", + "Compares NDVI by day of year across two different years, showing how vegetation patterns vary between 2012 and 2019." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "338", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection and subset the grassland feature.\n", + "grassland = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n", + " ee.Filter.eq(\"label\", \"Grassland\")\n", + ")\n", + "\n", + "# Load MODIS vegetation indices data and subset years 2012 and 2019.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(\n", + " ee.Filter.Or(\n", + " ee.Filter.date(\"2012-01-01\", \"2013-01-01\"),\n", + " ee.Filter.date(\"2019-01-01\", \"2020-01-01\"),\n", + " )\n", + " )\n", + " .select([\"NDVI\", \"EVI\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "339", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n", + "x_label = \"Day of Year\"\n", + "y_label = \"Vegetation Index (x1e4)\"\n", + "colors = [\"#e37d05\", \"#1d6b99\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "340", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.doy_series_by_year(\n", + " veg_indices,\n", + " band_name=\"NDVI\",\n", + " region=grassland,\n", + " scale=500,\n", + " chart_type=\"LineChart\",\n", + " colors=colors,\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + " stroke_width=5,\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "341", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/ui6zpbl.png)" + ] + }, + { + "cell_type": "markdown", + "id": "342", + "metadata": {}, + "source": [ + "#### image_doy_series_by_region\n", + "\n", + "Visualizes average NDVI by day of year across regions, showcasing seasonal changes for desert, forest, and grassland ecoregions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "343", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection and subset the grassland feature.\n", + "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n", + "\n", + "# Load MODIS vegetation indices data and subset a decade of images.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n", + " .select([\"NDVI\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "344", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n", + "x_label = \"Day of Year\"\n", + "y_label = \"Vegetation Index (x1e4)\"\n", + "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "345", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.image_doy_series_by_region(\n", + " veg_indices,\n", + " \"NDVI\",\n", + " ecoregions,\n", + " region_reducer=\"mean\",\n", + " scale=500,\n", + " year_reducer=ee.Reducer.mean(),\n", + " start_day=1,\n", + " end_day=366,\n", + " series_property=\"label\",\n", + " stroke_width=5,\n", + " chart_type=\"LineChart\",\n", + " title=title,\n", + " x_label=x_label,\n", + " y_label=y_label,\n", + " colors=colors,\n", + " legend_location=\"right\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "346", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/eGqGoRs.png)\n", + "\n", + "### Charting Array and List\n", + "\n", + "#### Scatter Plot\n", + "\n", + "Displays relationships between spectral bands, plotting red reflectance against NIR and SWIR reflectance to examine band correlations." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "347", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection and subset the forest feature.\n", + "forest = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n", + " ee.Filter.eq(\"label\", \"Forest\")\n", + ")\n", + "\n", + "# Define a MODIS surface reflectance composite.\n", + "modisSr = (\n", + " ee.ImageCollection(\"MODIS/061/MOD09A1\")\n", + " .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n", + " .select(\"sur_refl_b0[0-7]\")\n", + " .mean()\n", + ")\n", + "\n", + "# Reduce MODIS reflectance bands by forest region; get a dictionary with\n", + "# band names as keys, pixel values as lists.\n", + "pixel_vals = modisSr.reduceRegion(\n", + " **{\"reducer\": ee.Reducer.toList(), \"geometry\": forest.geometry(), \"scale\": 2000}\n", + ")\n", + "\n", + "# Convert NIR and SWIR value lists to an array to be plotted along the y-axis.\n", + "y_values = pixel_vals.toArray([\"sur_refl_b02\", \"sur_refl_b06\"])\n", + "\n", + "\n", + "# Get the red band value list; to be plotted along the x-axis.\n", + "x_values = ee.List(pixel_vals.get(\"sur_refl_b01\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "348", + "metadata": {}, + "outputs": [], + "source": [ + "title = \"Relationship Among Spectral Bands for Forest Pixels\"\n", + "colors = [\"rgba(29,107,153,0.4)\", \"rgba(207,81,62,0.4)\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "349", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.array_values(\n", + " y_values,\n", + " axis=1,\n", + " x_labels=x_values,\n", + " series_names=[\"NIR\", \"SWIR\"],\n", + " chart_type=\"ScatterChart\",\n", + " colors=colors,\n", + " title=title,\n", + " x_label=\"Red reflectance (x1e4)\",\n", + " y_label=\"NIR & SWIR reflectance (x1e4)\",\n", + " default_size=15,\n", + " xlim=(0, 800),\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "350", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/zkPlZIO.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "351", + "metadata": {}, + "outputs": [], + "source": [ + "x = ee.List(pixel_vals.get(\"sur_refl_b01\"))\n", + "y = ee.List(pixel_vals.get(\"sur_refl_b06\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "352", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.array_values(\n", + " y,\n", + " x_labels=x,\n", + " series_names=[\"SWIR\"],\n", + " chart_type=\"ScatterChart\",\n", + " colors=[\"rgba(207,81,62,0.4)\"],\n", + " title=title,\n", + " x_label=\"Red reflectance (x1e4)\",\n", + " y_label=\"SWIR reflectance (x1e4)\",\n", + " default_size=15,\n", + " xlim=(0, 800),\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "353", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/WHUHjH6.png)" + ] + }, + { + "cell_type": "markdown", + "id": "354", + "metadata": {}, + "source": [ + " #### Transect Line Plot\n", + "\n", + "Plots elevation along a transect line across a specific region, providing a profile view of terrain elevation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "355", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a line across the Olympic Peninsula, USA.\n", + "transect = ee.Geometry.LineString([[-122.8, 47.8], [-124.5, 47.8]])\n", + "\n", + "# Define a pixel coordinate image.\n", + "lat_lon_img = ee.Image.pixelLonLat()\n", + "\n", + "# Import a digital surface model and add latitude and longitude bands.\n", + "elev_img = ee.Image(\"USGS/SRTMGL1_003\").select(\"elevation\").addBands(lat_lon_img)\n", + "\n", + "# Reduce elevation and coordinate bands by transect line; get a dictionary with\n", + "# band names as keys, pixel values as lists.\n", + "elev_transect = elev_img.reduceRegion(\n", + " reducer=ee.Reducer.toList(),\n", + " geometry=transect,\n", + " scale=1000,\n", + ")\n", + "\n", + "# Get longitude and elevation value lists from the reduction dictionary.\n", + "lon = ee.List(elev_transect.get(\"longitude\"))\n", + "elev = ee.List(elev_transect.get(\"elevation\"))\n", + "\n", + "# Sort the longitude and elevation values by ascending longitude.\n", + "lon_sort = lon.sort(lon)\n", + "elev_sort = elev.sort(lon)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "356", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.array_values(\n", + " elev_sort,\n", + " x_labels=lon_sort,\n", + " series_names=[\"Elevation\"],\n", + " chart_type=\"AreaChart\",\n", + " colors=[\"#1d6b99\"],\n", + " title=\"Elevation Profile Across Longitude\",\n", + " x_label=\"Longitude\",\n", + " y_label=\"Elevation (m)\",\n", + " stroke_width=5,\n", + " fill=\"bottom\",\n", + " fill_opacities=[0.4],\n", + " ylim=(0, 2500),\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "357", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/k3XRita.png)" + ] + }, + { + "cell_type": "markdown", + "id": "358", + "metadata": {}, + "source": [ + "#### Metadata Scatter Plot\n", + "\n", + "Visualizes Landsat image metadata by plotting cloud cover against geometric RMSE, useful for quality assessment of image collections." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "359", + "metadata": {}, + "outputs": [], + "source": [ + "# Import a Landsat 8 collection and filter to a single path/row.\n", + "col = ee.ImageCollection(\"LANDSAT/LC08/C02/T1_L2\").filter(\n", + " ee.Filter.expression(\"WRS_PATH == 45 && WRS_ROW == 30\")\n", + ")\n", + "\n", + "# Reduce image properties to a series of lists; one for each selected property.\n", + "propVals = col.reduceColumns(\n", + " reducer=ee.Reducer.toList().repeat(2),\n", + " selectors=[\"CLOUD_COVER\", \"GEOMETRIC_RMSE_MODEL\"],\n", + ").get(\"list\")\n", + "\n", + "# Get selected image property value lists; to be plotted along x and y axes.\n", + "x = ee.List(ee.List(propVals).get(0))\n", + "y = ee.List(ee.List(propVals).get(1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "360", + "metadata": {}, + "outputs": [], + "source": [ + "colors = [geemap.hex_to_rgba(\"#96356f\", 0.4)]\n", + "print(colors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "361", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.array_values(\n", + " y,\n", + " x_labels=x,\n", + " series_names=[\"RMSE\"],\n", + " chart_type=\"ScatterChart\",\n", + " colors=colors,\n", + " title=\"Landsat 8 Image Collection Metadata (045030)\",\n", + " x_label=\"Cloud cover (%)\",\n", + " y_label=\"Geometric RMSE (m)\",\n", + " default_size=15,\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "362", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/3COY3xd.png)" + ] + }, + { + "cell_type": "markdown", + "id": "363", + "metadata": {}, + "source": [ + "#### Mapped Function Scatter & Line Plot\n", + "\n", + "Plots a sine function as a line chart, mapping mathematical functions onto chart axes for visualization." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "364", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "start = -2 * math.pi\n", + "end = 2 * math.pi\n", + "points = ee.List.sequence(start, end, None, 50)\n", + "\n", + "\n", + "def sin_func(val):\n", + " return ee.Number(val).sin()\n", + "\n", + "\n", + "values = points.map(sin_func)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "365", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.array_values(\n", + " values,\n", + " points,\n", + " chart_type=\"LineChart\",\n", + " colors=[\"#39a8a7\"],\n", + " title=\"Sine Function\",\n", + " x_label=\"radians\",\n", + " y_label=\"sin(x)\",\n", + " marker=\"circle\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "366", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/7qcxvey.png)\n", + "\n", + "### Charting Data Table\n", + "\n", + "#### Manual DataTable chart\n", + "\n", + "Creates a chart from a manually created DataTable, such as US state populations." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "367", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "368", + "metadata": {}, + "outputs": [], + "source": [ + "data = {\n", + " \"State\": [\"CA\", \"NY\", \"IL\", \"MI\", \"OR\"],\n", + " \"Population\": [37253956, 19378102, 12830632, 9883640, 3831074],\n", + "}\n", + "\n", + "df = pd.DataFrame(data)\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "369", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.Chart(\n", + " df,\n", + " x_cols=[\"State\"],\n", + " y_cols=[\"Population\"],\n", + " chart_type=\"ColumnChart\",\n", + " colors=[\"#1d6b99\"],\n", + " title=\"State Population (US census, 2010)\",\n", + " x_label=\"State\",\n", + " y_label=\"Population\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "370", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/vuxNmuh.png)" + ] + }, + { + "cell_type": "markdown", + "id": "371", + "metadata": {}, + "source": [ + "#### Computed DataTable chart\n", + "\n", + "Computes a DataTable from MODIS vegetation indices data for a forest area, then charts NDVI and EVI time series." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "372", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the example feature collection and subset the forest feature.\n", + "forest = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n", + " ee.Filter.eq(\"label\", \"Forest\")\n", + ")\n", + "\n", + "# Load MODIS vegetation indices data and subset a decade of images.\n", + "veg_indices = (\n", + " ee.ImageCollection(\"MODIS/061/MOD13A1\")\n", + " .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n", + " .select([\"NDVI\", \"EVI\"])\n", + ")\n", + "\n", + "# Build a feature collection where each feature has a property that represents\n", + "# a DataFrame row.\n", + "\n", + "\n", + "def aggregate(img):\n", + " # Reduce the image to the mean of pixels intersecting the forest ecoregion.\n", + " stat = img.reduceRegion(\n", + " **{\"reducer\": ee.Reducer.mean(), \"geometry\": forest, \"scale\": 500}\n", + " )\n", + "\n", + " # Extract the reduction results along with the image date.\n", + " date = geemap.image_date(img)\n", + " evi = stat.get(\"EVI\")\n", + " ndvi = stat.get(\"NDVI\")\n", + "\n", + " # Make a list of observation attributes to define a row in the DataTable.\n", + " row = ee.List([date, evi, ndvi])\n", + "\n", + " # Return the row as a property of an ee.Feature.\n", + " return ee.Feature(None, {\"row\": row})\n", + "\n", + "\n", + "reduction_table = veg_indices.map(aggregate)\n", + "\n", + "# Aggregate the 'row' property from all features in the new feature collection\n", + "# to make a server-side 2-D list (DataTable).\n", + "data_table_server = reduction_table.aggregate_array(\"row\")\n", + "\n", + "# Define column names and properties for the DataTable. The order should\n", + "# correspond to the order in the construction of the 'row' property above.\n", + "column_header = ee.List([[\"Date\", \"EVI\", \"NDVI\"]])\n", + "\n", + "# Concatenate the column header to the table.\n", + "data_table_server = column_header.cat(data_table_server)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "373", + "metadata": {}, + "outputs": [], + "source": [ + "data_table = chart.DataTable(data_table_server, date_column=\"Date\")\n", + "data_table.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "374", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.Chart(\n", + " data_table,\n", + " chart_type=\"LineChart\",\n", + " x_cols=\"Date\",\n", + " y_cols=[\"EVI\", \"NDVI\"],\n", + " colors=[\"#e37d05\", \"#1d6b99\"],\n", + " title=\"Average Vegetation Index Value by Date for Forest\",\n", + " x_label=\"Date\",\n", + " y_label=\"Vegetation index (x1e4)\",\n", + " stroke_width=3,\n", + " legend_location=\"right\",\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "375", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/PWei7QC.png)" + ] + }, + { + "cell_type": "markdown", + "id": "376", + "metadata": {}, + "source": [ + "#### Interval chart\n", + "\n", + "Displays annual NDVI time series with variance, showing inter-annual vegetation change and variability across the year." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "377", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a point to extract an NDVI time series for.\n", + "geometry = ee.Geometry.Point([-121.679, 36.479])\n", + "\n", + "# Define a band of interest (NDVI), import the MODIS vegetation index dataset,\n", + "# and select the band.\n", + "band = \"NDVI\"\n", + "ndvi_col = ee.ImageCollection(\"MODIS/061/MOD13Q1\").select(band)\n", + "\n", + "# Map over the collection to add a day of year (doy) property to each image.\n", + "\n", + "\n", + "def set_doy(img):\n", + " doy = ee.Date(img.get(\"system:time_start\")).getRelative(\"day\", \"year\")\n", + " # Add 8 to day of year number so that the doy label represents the middle of\n", + " # the 16-day MODIS NDVI composite.\n", + " return img.set(\"doy\", ee.Number(doy).add(8))\n", + "\n", + "\n", + "ndvi_col = ndvi_col.map(set_doy)\n", + "\n", + "# Join all coincident day of year observations into a set of image collections.\n", + "distinct_doy = ndvi_col.filterDate(\"2013-01-01\", \"2014-01-01\")\n", + "filter = ee.Filter.equals(**{\"leftField\": \"doy\", \"rightField\": \"doy\"})\n", + "join = ee.Join.saveAll(\"doy_matches\")\n", + "join_col = ee.ImageCollection(join.apply(distinct_doy, ndvi_col, filter))\n", + "\n", + "# Calculate the absolute range, interquartile range, and median for the set\n", + "# of images composing each coincident doy observation group. The result is\n", + "# an image collection with an image representative per unique doy observation\n", + "# with bands that describe the 0, 25, 50, 75, 100 percentiles for the set of\n", + "# coincident doy images.\n", + "\n", + "\n", + "def cal_percentiles(img):\n", + " doyCol = ee.ImageCollection.fromImages(img.get(\"doy_matches\"))\n", + "\n", + " return doyCol.reduce(\n", + " ee.Reducer.percentile([0, 25, 50, 75, 100], [\"p0\", \"p25\", \"p50\", \"p75\", \"p100\"])\n", + " ).set({\"doy\": img.get(\"doy\")})\n", + "\n", + "\n", + "comp = ee.ImageCollection(join_col.map(cal_percentiles))\n", + "\n", + "# Extract the inter-annual NDVI doy percentile statistics for the\n", + "# point of interest per unique doy representative. The result is\n", + "# is a feature collection where each feature is a doy representative that\n", + "# contains a property (row) describing the respective inter-annual NDVI\n", + "# variance, formatted as a list of values.\n", + "\n", + "\n", + "def order_percentiles(img):\n", + " stats = ee.Dictionary(\n", + " img.reduceRegion(\n", + " **{\"reducer\": ee.Reducer.first(), \"geometry\": geometry, \"scale\": 250}\n", + " )\n", + " )\n", + "\n", + " # Order the percentile reduction elements according to how you want columns\n", + " # in the DataTable arranged (x-axis values need to be first).\n", + " row = ee.List(\n", + " [\n", + " img.get(\"doy\"),\n", + " stats.get(band + \"_p50\"),\n", + " stats.get(band + \"_p0\"),\n", + " stats.get(band + \"_p25\"),\n", + " stats.get(band + \"_p75\"),\n", + " stats.get(band + \"_p100\"),\n", + " ]\n", + " )\n", + "\n", + " # Return the row as a property of an ee.Feature.\n", + " return ee.Feature(None, {\"row\": row})\n", + "\n", + "\n", + "reduction_table = comp.map(order_percentiles)\n", + "\n", + "# Aggregate the 'row' properties to make a server-side 2-D array (DataTable).\n", + "data_table_server = reduction_table.aggregate_array(\"row\")\n", + "\n", + "# Define column names and properties for the DataTable. The order should\n", + "# correspond to the order in the construction of the 'row' property above.\n", + "column_header = ee.List([[\"DOY\", \"median\", \"p0\", \"p25\", \"p75\", \"p100\"]])\n", + "\n", + "# Concatenate the column header to the table.\n", + "data_table_server = column_header.cat(data_table_server)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "378", + "metadata": {}, + "outputs": [], + "source": [ + "df = chart.DataTable(data_table_server)\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "379", + "metadata": {}, + "outputs": [], + "source": [ + "fig = chart.Chart(\n", + " df,\n", + " chart_type=\"IntervalChart\",\n", + " x_cols=\"DOY\",\n", + " y_cols=[\"p0\", \"p25\", \"median\", \"p75\", \"p100\"],\n", + " title=\"Annual NDVI Time Series with Inter-Annual Variance\",\n", + " x_label=\"Day of Year\",\n", + " y_label=\"Vegetation index (x1e4)\",\n", + " stroke_width=1,\n", + " fill=\"between\",\n", + " fill_colors=[\"#b6d1c6\", \"#83b191\", \"#83b191\", \"#b6d1c6\"],\n", + " fill_opacities=[0.6] * 4,\n", + " labels=[\"p0\", \"p25\", \"median\", \"p75\", \"p100\"],\n", + " display_legend=True,\n", + " legend_location=\"top-right\",\n", + " ylim=(0, 10000),\n", + ")\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "380", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/i8ZrGPR.png)" + ] + }, + { + "cell_type": "markdown", + "id": "381", + "metadata": {}, + "source": [ + "## Exercises\n", + "\n", + "### Exercise 1: Visualizing DEM Data\n", + "\n", + "Find a DEM dataset in the [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets) and clip it to a specific area (e.g., your country, state, or city). Display it with an appropriate color palette. For example, the sample map below shows the DEM of the state of Colorado.\n", + "\n", + "![](https://i.imgur.com/OLeSt7n.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "382", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "383", + "metadata": {}, + "source": [ + "### Exercise 2: Cloud-Free Composite with Sentinel-2 or Landsat\n", + "\n", + "Use Sentinel-2 or Landsat-9 data to create a cloud-free composite for a specific year in a region of your choice.\n", + "\n", + "Use [Sentinel-2](https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR_HARMONIZED) or [Landsat-9 data](https://developers.google.com/earth-engine/datasets/catalog/landsat-9) data to create a cloud-free composite for a specific year in a region of your choice. Display the imagery on the map with a proper band combination. For example, the sample map below shows a cloud-free false-color composite of Sentinel-2 imagery of the year 2021 for the state of Colorado.\n", + "\n", + "![](https://i.imgur.com/xkxpkS1.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "384", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "385", + "metadata": {}, + "source": [ + "### Exercise 3: Visualizing NAIP Imagery\n", + "\n", + "Use [NAIP](https://developers.google.com/earth-engine/datasets/catalog/USDA_NAIP_DOQQ) imagery to create a cloud-free imagery for a U.S. county of your choice. For example, the sample map below shows a cloud-free true-color composite of NAIP imagery for Knox County, Tennessee. Keep in mind that there might be some counties with the same name in different states, so make sure to select the correct county for the selected state.\n", + "\n", + "![](https://i.imgur.com/iZSGqGS.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "386", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "387", + "metadata": {}, + "source": [ + "### Exercise 4: Visualizing Watershed Boundaries\n", + "\n", + "Visualize the [USGS Watershed Boundary Dataset](https://developers.google.com/earth-engine/datasets/catalog/USGS_WBD_2017_HUC04) with outline color only, no fill color.\n", + "\n", + "![](https://i.imgur.com/PLlNFq3.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "388", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "389", + "metadata": {}, + "source": [ + "### Exercise 5: Visualizing Land Cover Change\n", + "\n", + "Use the [USGS National Land Cover Database](https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD_RELEASES_2019_REL_NLCD) and [US Census States](https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States) to create a split-panel map for visualizing land cover change (2001-2019) for a US state of your choice. Make sure you add the NLCD legend to the map.\n", + "\n", + "![](https://i.imgur.com/Au7Q5Ln.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "390", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "391", + "metadata": {}, + "source": [ + "### Exercise 6: Creating a Landsat Timelapse Animation\n", + "\n", + "Generate a timelapse animation using Landsat data to show changes over time for a selected region.\n", + "\n", + "![Spain](https://github.com/user-attachments/assets/f12839c0-1c30-404d-b0ab-0fa12ce12d24)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "392", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "geo", + "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.12.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/mkdocs.yml b/mkdocs.yml index 6eb57e0951..9e0f7ade27 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -153,6 +153,7 @@ nav: - workshops/Taiwan_2024.ipynb - workshops/FOSS4GNA_2024.ipynb - workshops/IPPN_2024.ipynb + - workshops/HGAC_2024.ipynb - Notebooks: - notebooks/00_ee_auth_colab.ipynb - notebooks/00_geemap_colab.ipynb