datasets.sentinel2

Sentinel-2 presets for vegetation indices (NDVI, EVI) and spectral bands.

About Sentinel-2

Sentinel-2 is a European Space Agency (ESA) mission providing high-resolution optical imagery.

Key features: - 10m resolution (visible and NIR bands) - ~5 day revisit time - 13 spectral bands - Available from 2015 onwards

We use the Surface Reflectance Harmonized collection (COPERNICUS/S2_SR_HARMONIZED) which includes atmospheric correction.

Vegetation Indices

The most common indices for monitoring vegetation health and recovery:

  • NDVI (Normalized Difference Vegetation Index): (NIR - Red) / (NIR + Red)
  • EVI (Enhanced Vegetation Index): 2.5 * (NIR - Red) / (NIR + 6*Red - 7.5*Blue + 1)

source

mask_s2_clouds


def mask_s2_clouds(
    image
):

Mask clouds in Sentinel-2 imagery using QA60 band.


source

add_indices


def add_indices(
    image
):

Add NDVI and EVI bands to a Sentinel-2 image.

Note: Expects raw Sentinel-2 SR reflectance (scaled by 10000). Scales to 0-1 range before computing indices.


source

get_s2_collection


def get_s2_collection(
    start_date:str, end_date:str, geometry:NoneType=None, cloud_pct:int=20
):

Get a processed Sentinel-2 collection with NDVI and EVI.

Args: start_date: Start date (YYYY-MM-DD) end_date: End date (YYYY-MM-DD) geometry: Optional geometry to filter bounds cloud_pct: Maximum cloud cover percentage (default 20)

Returns: ee.ImageCollection with NDVI and EVI bands added

Presets

Note: NDVI and EVI are computed bands, so we need a slightly different approach. The ContinuousLayer points to the raw collection, and the extraction handles index computation.

For now, we provide a simpler preset that assumes indices are pre-computed.

Usage

from gee_polygons.datasets.sentinel2 import SENTINEL2_NDVI, get_s2_collection

# Option 1: Use the preset with extract_continuous
df = site.extract_continuous(
    SENTINEL2_NDVI,
    start_date='2020-01-01',
    end_date='2024-12-31',
    reducer='mean',
    frequency='yearly'
)

# Option 2: Get the full collection for custom processing
collection = get_s2_collection('2020-01-01', '2024-12-31', site.geometry)

Visualization Helpers

Functions for getting Sentinel-2 imagery ready for visualization with gee_polygons.visualize.


source

get_sentinel_composite


def get_sentinel_composite(
    geometry:Geometry, date_range:tuple, bands:list=None, cloud_pct:int=20, reducer:Literal='median'
)->Image:

Get a cloud-masked Sentinel-2 composite for visualization.

This returns an ee.Image ready for use with gee_polygons.visualize.render_image().

Args: geometry: Region of interest date_range: (start_date, end_date) as ‘YYYY-MM-DD’ strings bands: Bands to include (default: true color B4/B3/B2) cloud_pct: Maximum cloud cover percentage for filtering reducer: Temporal reducer (‘median’, ‘mean’, ‘mosaic’)

Returns: ee.Image with selected bands, scaled to 0-1 reflectance

Example: from gee_polygons.datasets.sentinel2 import get_sentinel_composite, SENTINEL_VIS from gee_polygons.visualize import render_image

image = get_sentinel_composite(
    geometry=site.geometry,
    date_range=('2020-06-01', '2020-08-31')
)

pil_img = render_image(
    image=image,
    region=site.geometry.buffer(500).bounds(),
    vis_params=SENTINEL_VIS,
    boundary=site.geometry
)