{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://gishub.org/srm-workshop-colab)\n", "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/srm-workshop-binder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Interactive mapping and analysis of geospatial big data using geemap and Google Earth Engine**\n", "\n", "This notebook was developed for the geemap workshop at the [Society for Range Management (SRM) 2022 Annual Meeting](http://annualmeeting.rangelands.org).\n", "\n", "\n", "Authors: [Qiusheng Wu](https://github.com/giswqs)\n", "\n", "Link to this notebook: https://gishub.org/SRM\n", "\n", "\n", "## Introduction\n", "\n", "### Description\n", "\n", "Google Earth Engine (GEE) is a cloud computing platform with a multi-petabyte catalog of satellite imagery and geospatial datasets. It enables scientists, researchers, and developers to analyze and visualize changes on the Earth’s surface. The geemap Python package provides GEE users with an intuitive interface to manipulate, analyze, and visualize geospatial big data interactively in a Jupyter-based environment. The topics to be covered in this workshop include: \n", "\n", "1. Introducing geemap \n", "2. Creating interactive maps\n", "3. Searching GEE data catalog\n", "4. Data Visualization\n", "5. Data Analysis\n", "6. Data Export\n", "\n", "This workshop is intended for scientific programmers, data scientists, geospatial analysts, and concerned citizens of Earth. The attendees are expected to have a basic understanding of Python and the Jupyter ecosystem. Familiarity with Earth science and geospatial datasets is useful but not required.\n", "\n", "### Useful links\n", "- [Streamlit Geospatial](https://streamlit.gishub.org)\n", "- [Google Earth Engine](https://earthengine.google.com)\n", "- [geemap.org](https://geemap.org)\n", "- [Google Earth Engine and geemap Python Tutorials](https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPccOFv1dCwvGI6TYnirRTg3) (55 videos with a total length of 15 hours)\n", "- [Spatial Data Management with Google Earth Engine](https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPdz9LHIJIxHlZe3t-MRCn61) (19 videos with a total length of 9 hours)\n", "- [Ask geemap questions on GitHub](https://github.com/gee-community/geemap/discussions)\n", "\n", "### Prerequisite\n", "- A Google Earth Engine account. Sigh up [here](https://earthengine.google.com) if needed. \n", "- [Miniconda](https://docs.conda.io/en/latest/miniconda.html) or [Anaconda](https://www.anaconda.com/products/individual) has been installed on your computer.\n", "\n", "\n", "### Set up a conda environment\n", "\n", "```\n", "conda create -n gee python=3.9\n", "conda activate gee\n", "conda install geopandas\n", "conda install mamba -c conda-forge\n", "mamba install geemap localtileserver -c conda-forge\n", "\n", "```\n", "\n", "### Launch JupyterLab\n", "Open **Anaconda Prompt** or the **Terminal** and enter the following commands.\n", "\n", "```\n", "conda activate gee\n", "jupyter lab\n", "```" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Geemap basics\n", "\n", "### Install geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import subprocess\n", "\n", "try:\n", " import geemap\n", "except ImportError:\n", " print('Installing geemap ...')\n", " subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import ee\n", "import geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an interactive map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customize the default map\n", "\n", "You can specify the center(lat, lon) and zoom for the default map. You can also set the visibility of the controls." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4, draw_ctrl=False, toolbar_ctrl=False)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add basemaps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.add_basemap('HYBRID')\n", "Map.add_basemap('OpenTopoMap')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change basemaps interactively" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/PXURCSP.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use drawing tools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Map.user_roi.getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Map.user_rois.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert GEE JavaScript to Python\n", "\n", "https://developers.google.com/earth-engine/guides/image_visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "js_snippet = \"\"\"\n", "// Load an image.\n", "var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');\n", "\n", "// Define the visualization parameters.\n", "var vizParams = {\n", " bands: ['B5', 'B4', 'B3'],\n", " min: 0,\n", " max: 0.5,\n", " gamma: [0.95, 1.1, 1]\n", "};\n", "\n", "// Center the map and display the image.\n", "Map.setCenter(-122.1899, 37.5010, 10); // San Francisco Bay\n", "Map.addLayer(image, vizParams, 'false color composite');\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.js_snippet_to_py(\n", " js_snippet, add_new_cell=True, import_ee=True, import_geemap=True, show_map=True\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "\n", "Map = geemap.Map()\n", "\n", "# Load an image.\n", "image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')\n", "\n", "# Define the visualization parameters.\n", "vizParams = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]}\n", "\n", "# Center the map and display the image.\n", "Map.setCenter(-122.1899, 37.5010, 10)\n", "# San Francisco Bay\n", "Map.addLayer(image, vizParams, 'False color composite')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also convert GEE JavaScript to Python without coding.\n", "\n", "![](https://i.imgur.com/VnnrJwe.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Earth Engine datasets\n", "\n", "### Load Earth Engine datasets\n", "\n", "More GEE datasets can be found at https://developers.google.com/earth-engine/datasets/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "# Add Earth Engine datasets\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", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Add Earth Engine layers to Map\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 0.5)\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 1.5},\n", " 'Landsat 7',\n", ")\n", "Map.addLayer(states, {}, \"US States\")\n", "\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Search the Earth Engine Data Catalog" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dem = ee.Image('CGIAR/SRTM90_V4')\n", "Map.addLayer(dem, {}, \"CGIAR/SRTM90_V4\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hillshade = ee.Terrain.hillshade(dem)\n", "Map.addLayer(hillshade, {}, 'Hillshade')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, \"DEM\", True, 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use the datasets module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geemap.datasets import DATA" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "dem = ee.Image(DATA.USGS_SRTMGL1_003)\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use the Inspector tool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/drnfJ6N.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "# Add Earth Engine datasets\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(\n", " ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']\n", ")\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Add Earth Engine layers to Map\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 0.5)\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 180, 'gamma': 1.5},\n", " 'Landsat 7',\n", ")\n", "Map.addLayer(states, {}, \"US States\")\n", "\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data visualization \n", "\n", "### Use the Plotting tool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/t4jKsNo.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\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", "Map.addLayer(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", "Map.addLayer(hyperion, hyperion_vis, 'Hyperion')\n", "\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change layer opacity" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 1)\n", "Map.addLayer(states, {}, \"US States\", True)\n", "\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize raster data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "\n", "# Add Earth Engine dataset\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(\n", " ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']\n", ")\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 1)\n", "Map.addLayer(\n", " landsat7,\n", " {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 2},\n", " 'Landsat 7',\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize vector data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, \"US States\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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", "\n", "palette = ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']\n", "\n", "Map.add_styled_vector(\n", " states, column=\"NAME\", palette=palette, layer_name=\"Styled vector\", **vis_params\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a legend" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "legends = geemap.builtin_legends\n", "for legend in legends:\n", " print(legend)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.add_basemap('HYBRID')\n", "\n", "dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL')\n", "nlcd2016 = dataset.filter(ee.Filter.eq('system:index', '2016')).first()\n", "landcover = nlcd2016.select('landcover')\n", "\n", "Map.addLayer(landcover, {}, 'NLCD Land Cover 2016')\n", "Map.add_legend(builtin_legend='NLCD')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\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", "dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL')\n", "nlcd2016 = dataset.filter(ee.Filter.eq('system:index', '2016')).first()\n", "landcover = nlcd2016.select('landcover')\n", "\n", "Map.addLayer(landcover, {}, 'NLCD Land Cover 2016')\n", "Map.add_legend(legend_title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a colorbar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = 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", "Map.addLayer(dem, vis_params, 'SRTM DEM')\n", "\n", "colors = vis_params['palette']\n", "vmin = vis_params['min']\n", "vmax = vis_params['max']\n", "\n", "Map.add_colorbar(vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(\n", " vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\", orientation=\"vertical\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(\n", " vis_params,\n", " label=\"Elevation (m)\",\n", " layer_name=\"SRTM DEM\",\n", " orientation=\"vertical\",\n", " transparent_bg=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_colorbar(\n", " vis_params,\n", " label=\"Elevation (m)\",\n", " layer_name=\"SRTM DEM\",\n", " orientation=\"vertical\",\n", " transparent_bg=True,\n", " discrete=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a split-panel map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map.split_map(left_layer='HYBRID', right_layer='TERRAIN')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "Map.split_map(\n", " left_layer='NLCD 2016 CONUS Land Cover', right_layer='NLCD 2001 CONUS Land Cover'\n", ")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4)\n", "\n", "dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL')\n", "nlcd_2001 = (\n", " dataset.filter(ee.Filter.eq('system:index', '2001')).first().select('landcover')\n", ")\n", "nlcd_2016 = (\n", " dataset.filter(ee.Filter.eq('system:index', '2016')).first().select('landcover')\n", ")\n", "\n", "left_layer = geemap.ee_tile_layer(nlcd_2001, {}, 'NLCD 2001')\n", "right_layer = geemap.ee_tile_layer(nlcd_2016, {}, 'NLCD 2016')\n", "\n", "Map.split_map(left_layer, right_layer)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create linked maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = (\n", " ee.ImageCollection('COPERNICUS/S2')\n", " .filterDate('2018-09-01', '2018-09-30')\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=\"400px\",\n", " center=[38.4151, 21.2712],\n", " zoom=12,\n", " ee_objects=[image],\n", " vis_params=vis_params,\n", " labels=labels,\n", " label_position=\"topright\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use timeseries inspector\n", "\n", "Using data from the [Rangeland Analysis Platform](https://support.rangelands.app/article/46-new-to-rap-start-here-landing).\n", "- Vegetation cover dataset: `ee.ImageCollection('projects/rangeland-analysis-platform/vegetation-cover-v3')`\n", "- Rangeland production dataset: `ee.ImageCollection('projects/rangeland-analysis-platform/npp-partitioned-v3')`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap.colormaps as cm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection = ee.ImageCollection(\n", " 'projects/rangeland-analysis-platform/vegetation-cover-v3'\n", ").select('TRE')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "years = collection.aggregate_array('year').getInfo()\n", "years" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = [str(i) for i in years]\n", "names" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "palette = cm.palettes.ndvi\n", "palette" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {'min': 0, 'max': 50, 'palette': palette}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Map.addLayer(collection.first(), {}, \"First image\")\n", "Map.ts_inspector(\n", " left_ts=collection,\n", " right_ts=collection,\n", " left_names=names,\n", " right_names=names,\n", " left_vis=vis_params,\n", " right_vis=vis_params,\n", ")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Descriptive statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "centroid = ee.Geometry.Point([-122.4439, 37.7538])\n", "\n", "image = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').filterBounds(centroid).first()\n", "\n", "vis = {'min': 0, 'max': 3000, 'bands': ['B5', 'B4', 'B3']}\n", "\n", "Map.centerObject(centroid, 8)\n", "Map.addLayer(image, vis, \"Landsat-8\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.propertyNames().getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image.get('CLOUD_COVER').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "props = geemap.image_props(image)\n", "props.getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats = geemap.image_stats(image, scale=90)\n", "stats.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zonal statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "# Add Earth Engine dataset\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "\n", "# Set visualization parameters.\n", "dem_vis = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Add Earth Engine DEM to map\n", "Map.addLayer(dem, dem_vis, 'SRTM DEM')\n", "\n", "# Add Landsat data to map\n", "landsat = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "\n", "landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}\n", "Map.addLayer(landsat, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\")\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, 'US States')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dem_stats = 'dem_stats.csv'\n", "\n", "# Allowed output formats: csv, shp, json, kml, kmz\n", "# Allowed statistics type: MEAN, MAXIMUM, MINIMUM, MEDIAN, STD, MIN_MAX, VARIANCE, SUM\n", "geemap.zonal_stats(dem, states, out_dem_stats, stat_type='MEAN', scale=1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_landsat_stats = 'landsat_stats.csv'\n", "geemap.zonal_stats(\n", " landsat, states, out_landsat_stats, stat_type='SUM', scale=1000\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zonal statistics by group" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL')\n", "nlcd2016 = dataset.filter(ee.Filter.eq('system:index', '2016')).first()\n", "landcover = nlcd2016.select('landcover')\n", "Map.addLayer(landcover, {}, 'NLCD 2016')\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, 'US States')\n", "Map.add_legend(builtin_legend='NLCD')\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nlcd_stats = 'nlcd_stats.csv'\n", "\n", "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n", "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n", "geemap.zonal_stats_by_group(\n", " landcover,\n", " states,\n", " nlcd_stats,\n", " stat_type='SUM',\n", " denominator=1000000,\n", " decimal_places=2,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### WhiteboxTools\n", "\n", "https://www.whiteboxgeo.com/manual/wbt_book/intro.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import whiteboxgui" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "whiteboxgui.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "whiteboxgui.show(tree=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/aNRfUIf.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data export" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export ee.Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "image = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "\n", "landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}\n", "Map.addLayer(image, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\", True, 1)\n", "\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Draw any shapes on the map using the Drawing tools before executing this code block\n", "roi = Map.user_roi\n", "\n", "if roi is None:\n", " roi = ee.Geometry.Polygon(\n", " [\n", " [\n", " [-115.413031, 35.889467],\n", " [-115.413031, 36.543157],\n", " [-114.034328, 36.543157],\n", " [-114.034328, 35.889467],\n", " [-115.413031, 35.889467],\n", " ]\n", " ]\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "filename = 'landsat.tif'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exporting all bands as one single image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = image.clip(roi).unmask()\n", "geemap.ee_export_image(\n", " image, filename=filename, scale=90, region=roi, file_per_band=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exporting each band as one image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image(\n", " image, filename=filename, scale=90, region=roi, file_per_band=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Export an image to Google Drive¶" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geemap.ee_export_image_to_drive(image, description='landsat', folder='export', region=roi, scale=30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export ee.ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loc = ee.Geometry.Point(-99.2222, 46.7816)\n", "collection = (\n", " ee.ImageCollection('USDA/NAIP/DOQQ')\n", " .filterBounds(loc)\n", " .filterDate('2008-01-01', '2020-01-01')\n", " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "collection.aggregate_array('system:index').getInfo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.getcwd()\n", "geemap.ee_export_image_collection(collection, out_dir=out_dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extract pixels as a numpy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810').select(['B4', 'B5', 'B6'])\n", "\n", "aoi = ee.Geometry.Polygon(\n", " [[[-110.8, 44.7], [-110.8, 44.6], [-110.6, 44.6], [-110.6, 44.7]]], None, False\n", ")\n", "\n", "rgb_img = geemap.ee_to_numpy(img, region=aoi)\n", "print(rgb_img.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rgb_img_test = (255 * ((rgb_img[:, :, 0:3] - 100) / 3500)).astype('uint8')\n", "plt.imshow(rgb_img_test)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export pixel values to points" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "# Add Earth Engine dataset\n", "dem = ee.Image('USGS/SRTMGL1_003')\n", "landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],\n", "}\n", "\n", "# Add Earth Engine layers to Map\n", "Map.addLayer(\n", " landsat7, {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200}, 'Landsat 7'\n", ")\n", "Map.addLayer(dem, vis_params, 'SRTM DEM', True, 1)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Download sample data**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "work_dir = os.getcwd()\n", "in_shp = os.path.join(work_dir, 'us_cities.shp')\n", "if not os.path.exists(in_shp):\n", " data_url = 'https://github.com/giswqs/data/raw/main/us/us_cities.zip'\n", " geemap.download_from_url(data_url, out_dir=work_dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "in_fc = geemap.shp_to_ee(in_shp)\n", "Map.addLayer(in_fc, {}, 'Cities')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Export pixel values as a shapefile**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_shp = os.path.join(work_dir, 'dem.shp')\n", "geemap.extract_values_to_points(in_fc, dem, out_shp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Export pixel values as a csv**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_csv = os.path.join(work_dir, 'landsat.csv')\n", "geemap.extract_values_to_points(in_fc, landsat7, out_csv)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export ee.FeatureCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "\n", "fc = ee.FeatureCollection('users/giswqs/public/countries')\n", "Map.addLayer(fc, {}, \"Countries\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_shp = 'countries.shp'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_to_shp(fc, filename=out_shp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_csv = os.path.join(out_dir, 'countries.csv')\n", "geemap.ee_export_vector(fc, filename=out_csv)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_kml = os.path.join(out_dir, 'countries.kml')\n", "geemap.ee_export_vector(fc, filename=out_kml)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geemap.ee_export_vector_to_drive(fc, description=\"countries\", folder=\"export\", file_format=\"shp\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }