{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How to find out the greenest day of the year" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import libraries" ] }, { "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 a region of interest (ROI)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "countries = ee.FeatureCollection('users/giswqs/public/countries')\n", "Map.addLayer(countries, {}, 'coutries')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roi = countries.filter(ee.Filter.eq('id', 'USA'))\n", "Map.addLayer(roi, {}, 'roi')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filter ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start_date = '2019-01-01'\n", "end_date = '2019-12-31'\n", "\n", "l8 = (\n", " ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')\n", " .filterBounds(roi)\n", " .filterDate(start_date, end_date)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# print(l8.size().getInfo())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a median composite" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "median = l8.median()\n", "\n", "visParams = {\n", " 'bands': ['B4', 'B3', 'B2'],\n", " 'min': 0,\n", " 'max': 0.4,\n", "}\n", "\n", "Map.addLayer(median, visParams, 'Median')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define functions to add time bands" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def addNDVI(image):\n", " ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI')\n", " return image.addBands(ndvi)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def addDate(image):\n", " img_date = ee.Date(image.date())\n", " img_date = ee.Number.parse(img_date.format('YYYYMMdd'))\n", " return image.addBands(ee.Image(img_date).rename('date').toInt())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def addMonth(image):\n", " img_date = ee.Date(image.date())\n", " img_doy = ee.Number.parse(img_date.format('M'))\n", " return image.addBands(ee.Image(img_doy).rename('month').toInt())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def addDOY(image):\n", " img_date = ee.Date(image.date())\n", " img_doy = ee.Number.parse(img_date.format('D'))\n", " return image.addBands(ee.Image(img_doy).rename('doy').toInt())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map over an ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "withNDVI = l8.map(addNDVI).map(addDate).map(addMonth).map(addDOY)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a quality mosaic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "greenest = withNDVI.qualityMosaic('NDVI')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "greenest.bandNames().getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display the max value band" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ndvi = greenest.select('NDVI')\n", "palette = [\n", " '#d73027',\n", " '#f46d43',\n", " '#fdae61',\n", " '#fee08b',\n", " '#d9ef8b',\n", " '#a6d96a',\n", " '#66bd63',\n", " '#1a9850',\n", "]\n", "Map.addLayer(ndvi, {'palette': palette}, 'NDVI')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayer(greenest, visParams, 'Greenest pixel')\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display time bands" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayer(\n", " greenest.select('month'),\n", " {'palette': ['red', 'blue'], 'min': 1, 'max': 12},\n", " 'Greenest month',\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.addLayer(\n", " greenest.select('doy'),\n", " {'palette': ['brown', 'green'], 'min': 1, 'max': 365},\n", " 'Greenest doy',\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }