{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ ">\n", ">\n", ">\n", ">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Multispectral Functions Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Note: This example notebook covers the basics for how to calculate multispectral indices and does not cover image visualization. \n", ">\n", "> If you would like to learn how to visualize multispectral index products, [please follow the `Complete_ReadMe_Example.ipynb` or `Palettes_and_Visualization.ipynb` example notebooks on GitHub](https://github.com/radwinskis/RadGEEToolbox/tree/main/Example%20Notebooks)\n", ">" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ee\n", "from RadGEEToolbox import LandsatCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earth Engine initialized successfully.\n" ] } ], "source": [ "# Store name of Google Cloud Project assosiated with Earth Engine - replace with your project ID/name\n", "PROJECT_ID = 'your-cloud-project-id'\n", "# Attempt to initialize Earth Engine\n", "try:\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Earth Engine initialized successfully.\")\n", "except Exception as e:\n", " print(\"Initialization failed, attempting authentication...\")\n", " try:\n", " ee.Authenticate()\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Authentication and initialization successful.\")\n", " except Exception as auth_error:\n", " print(\"Authentication failed. Error details:\", auth_error)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`RadGEEToolbox` has many available spectral index calculations for both Landsat and Sentinel-2 data**\n", "\n", "Such indices follow, but are not limited to: NDWI (water), NDVI (vegetation), NDTI (water turbidity), relative chlorophyll-a concentrations, \n", "and other indices for mapping evaporites (established by the author of this package - see Radwin & Bowen, 2021). Additionally, Land Surface \n", "Temperature (LST) can be easily calculcated for Landsat imagery.\n", "\n", "**In addition to multispectral indices, there are easy ways to temporally reduce image collections to mean, median, min, or max images**\n", "\n", "**In the below cell are examples of how to quickly process an entire image collection to the available spectral indices, using the available attribute functions**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# First we need an image collection\n", "col = LandsatCollection(start_date='2023-06-01', end_date='2023-06-30', tile_row=32, tile_path=38)\n", "\n", "#Then it is as easy as calling the desired attribute for multispectral indices\n", "NDWI = col.ndwi\n", "NDVI = col.ndvi\n", "NDTI = col.turbidity\n", "halite = col.halite\n", "gypsum = col.gypsum\n", "chlorophyll = col.chlorophyll\n", "surface_temperature = col.LST" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "______\n", "\n", "**It is also very easy to perform temporal reductions on an entire collection, as shown below**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Temporal reductions of image collections\n", "mean_img = col.mean\n", "min_img = col.min\n", "median_img = col.median\n", "max_img = col.max\n", "\n", "#Temporal reductions can be applied to multispectral indices as well\n", "mean_chlorophyll = chlorophyll.mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The above approach calculates spectral indices but does not perform binary threshold masking / classification**\n", "\n", "_______\n", "\n", "\n", "**If you wish to perform binary thresholding, there are alternative functions for calculating spectral indices and masking based on a floating point threshold value, where values below the threshold are masked as nodata pixels -- as shown below**\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#Using methods to calculate multispectral indices\n", "\n", "NDWI = col.ndwi_collection(threshold=0)\n", "NDVI = col.ndvi_collection(threshold=0.3)\n", "turbidity = col.turbidity_collection(threshold=0)\n", "\n", "#If you will be using Landsat 5 and Landsat 8 or 9 imagery together in a collection, you can specify different thresholds \n", "# for TM vs OLI sensors where ng_threshold means \"next generation\" threshold for newer sensors\n", "\n", "NDWI = col.ndwi_collection(threshold=0, ng_threshold=0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**By directly calling static method functions, `RadGEEToolbox` provides the freedom to use almost any of the core functions, including spectral index calculations, on GEE objects**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#Example using static method function to calculate NDWI for all images in an ee.ImageCollection object\n", "#Note the need for lambda to map functions with arguments other than the input image\n", "\n", "NDWI = col.collection.map(lambda image: LandsatCollection.landsat_ndwi_fn(image, threshold=0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### *Please refer to the [RadGEEToolbox documentation](https://radgeetoolbox.readthedocs.io/en/latest/) for more information and a comprehensive list of available functionality*" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }