{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/examples/notebooks/tn_surface_water.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Automated mapping of surface water in the state of Tennessee using Google Earth Engine cloud computing\n", "\n", "Author: Qiusheng Wu ([Website](https://wetlands.io) - [GitHub](https://github.com/giswqs))" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Installs geemap package\n", "import subprocess\n", "\n", "try:\n", " import geemap\n", "except ImportError:\n", " print('geemap package not installed. Installing ...')\n", " subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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": [ "## Define region of interest (ROI)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roi = ee.FeatureCollection('TIGER/2018/States').filter(\n", " ee.Filter.eq('NAME', 'Tennessee')\n", ")\n", "Map.addLayer(roi, {}, \"TN\")\n", "Map.centerObject(roi, 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Landsat timeseries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images = geemap.landsat_timeseries(\n", " roi=roi, start_year=1984, end_year=2020, start_date='01-01', end_date='12-31'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "first = images.first()\n", "\n", "vis_params = {'bands': ['NIR', 'Red', 'Green'], 'min': 0, 'max': 3000}\n", "\n", "Map.addLayer(first, vis_params, 'First image')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate Normalized Difference Water Index (NDWI)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ndwi_images = images.map(\n", " lambda img: img.normalizedDifference(['Green', 'SWIR1']).rename('ndwi')\n", ")\n", "\n", "ndwi_palette = [\n", " '#ece7f2',\n", " '#d0d1e6',\n", " '#a6bddb',\n", " '#74a9cf',\n", " '#3690c0',\n", " '#0570b0',\n", " '#045a8d',\n", " '#023858',\n", "]\n", "\n", "first_ndwi = ndwi_images.first()\n", "\n", "Map.addLayer(first_ndwi, {'palette': ndwi_palette}, 'First NDWI')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extract surface water extent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "water_images = ndwi_images.map(lambda img: img.gt(0).selfMask())\n", "\n", "first_water = water_images.first()\n", "\n", "Map.addLayer(first_water, {'palette': ['blue']}, 'First Water')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate surface water areas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cal_area(img):\n", " pixel_area = img.multiply(ee.Image.pixelArea()).divide(1e6)\n", " img_area = pixel_area.reduceRegion(\n", " **{\n", " 'geometry': roi.geometry(),\n", " 'reducer': ee.Reducer.sum(),\n", " 'scale': 1000,\n", " 'maxPixels': 1e12,\n", " }\n", " )\n", " return img.set({'water_area': img_area})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "water_areas = water_images.map(cal_area)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "water_stats = water_areas.aggregate_array('water_area').getInfo()\n", "water_stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot temporal trend" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "x = list(range(1984, 2021))\n", "y = [item.get('ndwi') for item in water_stats]\n", "\n", "plt.bar(x, y, align='center', alpha=0.5)\n", "# plt.xticks(y_pos, objects)\n", "plt.ylabel('Area (km2)')\n", "plt.title('Surface water dynamics in Tennessee')\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayerControl()\n", "Map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }