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