Skip to main content

Satellite Image Processing: Spectral Indices and Change Detection

In this class we put what we learned about STAC into practice to investigate a real event: the collapse of Vale’s tailings dam in Brumadinho (MG) on January 25, 2019.

Our workflow will be: - Search for Sentinel-2 images from before and after the collapse; - Compute spectral indices (NDVI, NDWI and EVI); - Analyze the histograms of these indices; - Classify the image with thresholds (thresholding); - Compute the difference between the two dates to visualize the impact of the disaster.

Colab link

Open In Colab
Code
# only run this if you are on Colab! 
# git clone https://github.com/ftl-brazil-2026/ebook.git
# pip install -r requirements.txt
Code
from pystac_client import Client
import rasterio
from rasterio.windows import from_bounds
from pyproj import Transformer
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import psutil
Code
def check_memory(label: str = "", warn_threshold: float = 85.0) -> None:
    """Prints the current RAM usage and warns if it is close to the machine's limit."""
    mem = psutil.virtual_memory()
    used_gb = mem.used / (1024 ** 3)
    total_gb = mem.total / (1024 ** 3)
    available_gb = mem.available / (1024 ** 3)

    prefix = f"[RAM] {label}: " if label else "[RAM] "
    print(f"{prefix}{used_gb:.2f} GB used of {total_gb:.2f} GB ({mem.percent:.1f}%) | available: {available_gb:.2f} GB")

    if mem.percent >= warn_threshold:
        print(f"WARNING: RAM usage above {warn_threshold:.0f}%! Consider restarting the kernel "
              "or reducing the area of interest (bbox/window).")

check_memory()
[RAM] 15.60 GB usados de 32.00 GB (58.2%) | disponível: 13.39 GB

Searching for images in STAC

In the last class we used CBERS-4 data; in this class we’ll use Sentinel-2 data to get access to the SWIR bands. Let’s search for scenes over the dam region, both before and after 01/25/2019.

Area of interest: around the Córrego do Feijão mine / Brumadinho-MG.

If you want to cover a different area of interest, you can use this link to define the bbox easily: bbox finder

To download Sentinel-2 images you need to generate API keys. Follow this link for more information: API - CDSE.

If you already have an account, just go to CDSE-API-KEYS-GENERATION, generate a new API key, and paste both your Access Key and Secret Key into your .env, as shown in our .env_copy.

Code
import os 
from dotenv import load_dotenv
load_dotenv()
True
Code
#os.environ["GDAL_HTTP_TCP_KEEPALIVE"] = "YES"
os.environ["AWS_S3_ENDPOINT"] = "eodata.dataspace.copernicus.eu"
os.environ["AWS_ACCESS_KEY_ID"] = os.environ.get("CDSE_CLIENT")
os.environ["AWS_SECRET_ACCESS_KEY"] = os.environ.get("CDSE_SECRET")
os.environ["AWS_HTTPS"] = "YES"
os.environ["AWS_VIRTUAL_HOSTING"] = "FALSE"

# AWS-style endpoint for the Copernicus ecosystem in STAC
cdse_stac = "https://stac.dataspace.copernicus.eu/v1/"
client = Client.open(cdse_stac)
Code
list(client.get_all_collections())
Code
# approximate bbox covering the Brumadinho / Córrego do Feijão dam
# remember our bbox is in WGS84 - EPSG:4326 (a classic for any analysis, but not a projected system!)
bbox = [-44.142273, -20.144694, -44.103469, -20.110041]
Code
import folium
from shapely.geometry import box

bounds_polygon = box(*bbox)

centro_lat = (bbox[1] + bbox[3]) / 2
centro_lon = (bbox[0] + bbox[2]) / 2

m = folium.Map(location=[centro_lat, centro_lon], zoom_start=15)


folium.GeoJson(
    bounds_polygon.__geo_interface__,
    name="Bounds",
    style_function=lambda x: {
        'fillColor': 'none',
        'color': 'blue',
        'weight': 3,
        'fillOpacity': 0
    }
).add_to(m)

m
Make this Notebook Trusted to load map: File -> Trust Notebook
Code
search_antes = client.search(
    collections=["sentinel-2-l2a"],
    bbox=bbox,
    datetime="2018-10-01/2019-01-24",
    query={"eo:cloud_cover": {"lt": 15}}
)

search_depois = client.search(
    collections=["sentinel-2-l2a"],
    bbox=bbox,
    datetime="2019-01-26/2019-04-30",
    query={"eo:cloud_cover": {"lt": 15}}
)

print("Images before the collapse:", search_antes.matched())
for item in search_antes.items():
    print(" ", item.id, "- clouds:", round(item.properties["eo:cloud_cover"], 1), "%")

print("\nImages after the collapse:", search_depois.matched())
for item in search_depois.items():
    print(" ", item.id, "- clouds:", round(item.properties["eo:cloud_cover"], 1), "%")
Imagens antes do rompimento: None
  S2A_MSIL2A_20190122T131241_N0500_R138_T23KNT_20221210T105448 - nuvens: 2.2 %
  S2B_MSIL2A_20190117T131239_N0500_R138_T23KNT_20221220T235155 - nuvens: 3.4 %
  S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510 - nuvens: 0.8 %

Imagens depois do rompimento: None
  S2B_MSIL2A_20190427T131249_N0500_R138_T23KNT_20221021T034403 - nuvens: 2.9 %
  S2B_MSIL2A_20190308T131239_N0500_R138_T23KNT_20221114T091205 - nuvens: 5.1 %
  S2A_MSIL2A_20190201T131241_N0500_R138_T23KNT_20221206T064832 - nuvens: 1.0 %

Let’s pick two scenes from the same orbit/tile (same footprint), one from each period, with low cloud cover.

Code
item_antes = list(list(search_antes.items()))[2]

item_depois = list(list(search_depois.items()))[1]

print(item_antes)
print(item_depois)
<Item id=S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510>
<Item id=S2B_MSIL2A_20190308T131239_N0500_R138_T23KNT_20221114T091205>
Code
item_antes.assets
{'AOT_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_AOT_10m.jp2>,
 'AOT_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_AOT_20m.jp2>,
 'AOT_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_AOT_60m.jp2>,
 'B01_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B01_20m.jp2>,
 'B01_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B01_60m.jp2>,
 'B02_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_B02_10m.jp2>,
 'B02_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B02_20m.jp2>,
 'B02_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B02_60m.jp2>,
 'B03_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_B03_10m.jp2>,
 'B03_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B03_20m.jp2>,
 'B03_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B03_60m.jp2>,
 'B04_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_B04_10m.jp2>,
 'B04_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B04_20m.jp2>,
 'B04_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B04_60m.jp2>,
 'B05_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B05_20m.jp2>,
 'B05_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B05_60m.jp2>,
 'B06_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B06_20m.jp2>,
 'B06_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B06_60m.jp2>,
 'B07_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B07_20m.jp2>,
 'B07_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B07_60m.jp2>,
 'B08_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_B08_10m.jp2>,
 'B09_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B09_60m.jp2>,
 'B11_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B11_20m.jp2>,
 'B11_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B11_60m.jp2>,
 'B12_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B12_20m.jp2>,
 'B12_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B12_60m.jp2>,
 'B8A_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_B8A_20m.jp2>,
 'B8A_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_B8A_60m.jp2>,
 'CLD_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/QI_DATA/MSK_CLDPRB_20m.jp2>,
 'CLD_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/QI_DATA/MSK_CLDPRB_60m.jp2>,
 'Product': <Asset href=https://download.dataspace.copernicus.eu/odata/v1/Products(412233a3-f94b-400b-8f9c-f5fbb15c7e60)/$value>,
 'SCL_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_SCL_20m.jp2>,
 'SCL_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_SCL_60m.jp2>,
 'SNW_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/QI_DATA/MSK_SNWPRB_20m.jp2>,
 'SNW_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/QI_DATA/MSK_SNWPRB_60m.jp2>,
 'TCI_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_TCI_10m.jp2>,
 'TCI_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_TCI_20m.jp2>,
 'TCI_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_TCI_60m.jp2>,
 'WVP_10m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R10m/T23KNT_20190107T131239_WVP_10m.jp2>,
 'WVP_20m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R20m/T23KNT_20190107T131239_WVP_20m.jp2>,
 'WVP_60m': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/IMG_DATA/R60m/T23KNT_20190107T131239_WVP_60m.jp2>,
 'thumbnail': <Asset href=https://datahub.creodias.eu/odata/v1/Assets(0f756242-9d94-4914-ba65-f0108a093f27)/$value>,
 'safe_manifest': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/manifest.safe>,
 'granule_metadata': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/GRANULE/L2A_T23KNT_A009602_20190107T131241/MTD_TL.xml>,
 'inspire_metadata': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/INSPIRE.xml>,
 'product_metadata': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/MTD_MSIL2A.xml>,
 'datastrip_metadata': <Asset href=s3://eodata/Sentinel-2/MSI/L2A_N0500/2019/01/07/S2B_MSIL2A_20190107T131239_N0500_R138_T23KNT_20221216T195510.SAFE/DATASTRIP/DS_S2RP_20221216T195510_S20190107T131241/MTD_DS.xml>}

Reading the bands by window

As we learned in the previous class, we don’t need to download the whole scene: we read only the window matching our area of interest, saving RAM and focusing only on the area we care about.

Code
# two helper functions to read the bands within the window 
def read_window(uri: str, bbox: list, masked: bool = True) -> np.ma.MaskedArray:
    """Reads the raster window matching the bbox (in EPSG:4326)."""
    with rasterio.open(uri) as src:
        transformer = Transformer.from_crs("EPSG:4326", 
                                           src.crs, 
                                           always_xy=True
                                           )
        minx, miny = transformer.transform(bbox[0], bbox[1])
        maxx, maxy = transformer.transform(bbox[2], bbox[3])
        window = from_bounds(minx, miny, maxx, maxy, src.transform).round_offsets().round_lengths()
        return src.read(1, 
                        window=window, 
                        masked=masked
                        )


def read_bands(item, bbox):
    """Reads the blue, green, red and NIR bands (MUX: BAND5, 6, 7, 8)"""
    assets = item.assets
    blue = read_window(assets["B02_10m"].href, bbox).astype("float32")
    green = read_window(assets["B03_10m"].href, bbox).astype("float32")
    red = read_window(assets["B04_10m"].href, bbox).astype("float32")
    nir = read_window(assets["B08_10m"].href, bbox).astype("float32")
    return blue, green, red, nir 

def read_bands_20m(item, bbox):
    assets = item.assets
    blue_20 = read_window(assets["B02_20m"].href, bbox).astype("float32")
    green_20 = read_window(assets["B03_20m"].href, bbox).astype("float32")
    red_20 = read_window(assets["B04_20m"].href, bbox).astype("float32")
    swir1_20 = read_window(assets["B11_20m"].href, bbox).astype("float32")
    swir2_20 = read_window(assets["B12_20m"].href, bbox).astype("float32")
    return blue_20, green_20, red_20, swir1_20, swir2_20
Code
# before the event
blue_antes, green_antes, red_antes, nir_antes  = read_bands(item_antes, bbox)
check_memory("")

# after the event
blue_depois, green_depois, red_depois, nir_depois = read_bands(item_depois, bbox)
check_memory("")

print("shape:", red_antes.shape)

## doing the same thing for the 20 m resolution
blue_antes_20, green_antes_20, red_antes_20, swir1_antes_20, swir2_antes_20 = read_bands_20m(item_antes, bbox)
blue_depois_20, green_depois_20, red_depois_20, swir1_depois_20, swir2_depois_20 = read_bands_20m(item_depois, bbox)
[RAM] 14.39 GB usados de 32.00 GB (55.5%) | disponível: 14.25 GB
[RAM] 14.49 GB usados de 32.00 GB (55.8%) | disponível: 14.14 GB
shape: (381, 408)

Creating RGB composites for before and after the event

Code
# normalizing by percentile here, but it could be normalized by min/max
def normalize(array, lo_pct=2, hi_pct=98):
    """Normalizes a numpy array using a percentile contrast stretch"""
    data = np.ma.filled(array, np.nan).astype("float64")
    valid = data[np.isfinite(data)]
    p_lo, p_hi = np.percentile(valid, [lo_pct, hi_pct])
    normalized = (array - p_lo) / (p_hi - p_lo)
    return np.clip(normalized, 0, 1)


# just stacks the images into a single numpy object
def make_rgb(red, green, blue):
    return np.dstack((normalize(red), normalize(green), normalize(blue)))


rgb_antes = make_rgb(red_antes, green_antes, blue_antes)
rgb_depois = make_rgb(red_depois, green_depois, blue_depois)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(rgb_antes)
ax1.set_title("Before (10/23/2018)")
ax1.axis("off")
ax2.imshow(rgb_depois)
ax2.set_title("After (05/22/2019)")
ax2.axis("off")
plt.tight_layout()
plt.show()

Code
### Creating a false-color composite
# normalizing by percentile here, but it could be normalized by min/max

rgb_antes = make_rgb(nir_antes, green_antes, blue_antes)
rgb_depois = make_rgb(nir_depois, green_depois, blue_depois)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(rgb_antes)
ax1.set_title("Before (10/23/2018)")
ax1.axis("off")
ax2.imshow(rgb_depois)
ax2.set_title("After (05/22/2019)")
ax2.axis("off")
plt.tight_layout()
plt.show()

Spectral indices

Now let’s compute three widely used remote sensing indices:

  • NDVI (Normalized Difference Vegetation Index)
  • NDWI (Normalized Difference Water Index): presence of water/moisture
  • EVI (Enhanced Vegetation Index): similar to NDVI, but corrects for atmospheric and soil effects

We’ll also try a spectral index that highlights particulate matter in water or exposed soil, using the catalog of spectral indices at Awesome Spectral Indices

Let’s move on to the indices - **** - ****

Code
def calc_ndvi(nir, red):
    """NDVI = (NIR - RED) / (NIR + RED)"""
    with np.errstate(divide="ignore", invalid="ignore"):
        return (nir - red) / (nir + red)


def calc_ndwi(green, nir):
    """NDWI (McFeeters) = (GREEN - NIR) / (GREEN + NIR)"""
    with np.errstate(divide="ignore", invalid="ignore"):
        return (green - nir) / (green + nir)


def calc_evi(nir, red, blue, G=2.5, C1=6, C2=7.5, L=1):
    """EVI = G * (NIR - RED) / (NIR + C1*RED - C2*BLUE + L)"""
    with np.errstate(divide="ignore", invalid="ignore"):
        return G * (nir - red) / (nir + C1 * red - C2 * blue + L)


def calc_soil_index(B, G, R, N, S1, S2):
    "Normalized Shortwave Infrared Difference Soil-Moisture"
    with np.errstate(divide="ignore",invalid="ignore"):
        return (S1 - S2)/(S1 + S2)
Code
ndvi_antes = calc_ndvi(nir_antes, red_antes)
ndvi_depois = calc_ndvi(nir_depois, red_depois)

ndwi_antes = calc_ndwi(green_antes, nir_antes)
ndwi_depois = calc_ndwi(green_depois, nir_depois)

evi_antes = calc_evi(nir_antes, red_antes, blue_antes)
evi_depois = calc_evi(nir_depois, red_depois, blue_depois)

## soil index
## with the SWIR1 band we have a problem, since its resolution is 20 m while R,G,B,N are 10 m. 
## So either we resample all the bands to 20 m with numpy, or we get the bands already at 20 m straight from STAC. 
## I'll get them straight from STAC
## the NIR band I'll resample by hand using scikit-image
import skimage as ski
nir_antes_20 = ski.transform.resize(nir_antes,(swir1_antes_20.shape))
nir_depois_20 = ski.transform.resize(nir_depois,(swir1_depois_20.shape))

soil_antes = calc_soil_index(blue_antes_20,green_antes_20,red_antes_20, nir_antes_20, swir1_antes_20, swir2_antes_20)
soil_depois = calc_soil_index(blue_depois_20, green_depois_20, red_depois_20, nir_depois_20, swir1_depois_20, swir2_depois_20)
check_memory("after computing the indices")
[RAM] depois de calcular os índices: 14.65 GB usados de 32.00 GB (56.1%) | disponível: 14.04 GB
Code
fig, axes = plt.subplots(4, 2, figsize=(10, 14))

for ax, data, title in zip(axes[0], [ndvi_antes, ndvi_depois], ["NDVI - Before", "NDVI - After"]):
    im = ax.imshow(data, cmap="RdYlGn", vmin=-1, vmax=1)
    ax.set_title(title)
    ax.axis("off")
    fig.colorbar(im, ax=ax, fraction=0.046)

for ax, data, title in zip(axes[1], [ndwi_antes, ndwi_depois], ["NDWI - Before", "NDWI - After"]):
    im = ax.imshow(data, cmap="Blues", vmin=-1, vmax=1)
    ax.set_title(title)
    ax.axis("off")
    fig.colorbar(im, ax=ax, fraction=0.046)

for ax, data, title in zip(axes[2], [evi_antes, evi_depois], ["EVI - Before", "EVI - After"]):
    im = ax.imshow(data, cmap="RdYlGn", vmin=-1, vmax=1)
    ax.set_title(title)
    ax.axis("off")
    fig.colorbar(im, ax=ax, fraction=0.046)


for ax, data, title in zip(axes[3], [soil_antes, soil_depois], ["Soil - Before", "Soil - After"]):
    im = ax.imshow(data, cmap="RdYlGn", vmin=-1, vmax=1)
    ax.set_title(title)
    ax.axis("off")
    fig.colorbar(im, ax=ax, fraction=0.046)

plt.tight_layout()
plt.show()

Histogram

The histogram shows how the index values are distributed across the image. Let’s look at the NDVI from before the collapse.

Code
ndvi_flat = np.ma.filled(ndvi_antes, np.nan).ravel()
ndvi_flat = ndvi_flat[np.isfinite(ndvi_flat)]

plt.figure(figsize=(8, 4))
sns.histplot(ndvi_flat, kde=True, 
             color="seagreen", 
             binrange=(-1, 1), 
             bins=50
             )
plt.axvline(0.2, color="orange", linestyle="--", label="threshold 0.2")
plt.axvline(0.45, color="darkgreen", linestyle="--", label="threshold 0.45")
plt.title("Distribution of NDVI values (before the collapse)")
plt.xlabel("NDVI")
plt.legend()
plt.show()

Code
evi_normalized
array([0.87793689, 0.87822718, 0.87827761, ..., 0.87648972, 0.87642845,
       0.87650834], shape=(155447,))
Code
evi_flat = np.ma.filled(evi_antes, np.nan).ravel()
evi_flat = evi_flat[np.isfinite(evi_flat)]
evi_normalized = (evi_flat - evi_flat.min()) / (evi_flat.max() - evi_flat.min())
plt.figure(figsize=(8, 4))
sns.histplot(evi_normalized, 
             kde=True, 
             color="seagreen", 
             binrange=(0, 1), 
             bins=50)
plt.axvline(0.2, color="orange", linestyle="--", label="threshold 0.2")
plt.axvline(0.45, color="darkgreen", linestyle="--", label="threshold 0.45")
plt.title("Distribution of EVI values (before the collapse)")
plt.xlabel("EVI")
plt.legend()
plt.show()

Threshold classification (thresholding)

Based on the histogram, we can define thresholds to classify the image into classes. For NDVI:

  • < 0.2: exposed soil / water / urban area
  • 0.2 – 0.45: sparse/weak vegetation
  • >= 0.45: vigorous vegetation
Code
def classify_ndvi(ndvi):
    """0 = no data, 1 = soil/water/urban, 2 = weak vegetation, 3 = vigorous vegetation"""
    ndvi_filled = np.ma.filled(ndvi, np.nan)
    classes = np.zeros_like(ndvi_filled, dtype=np.int8)
    classes[ndvi_filled < 0.2] = 1
    classes[(ndvi_filled >= 0.2) & (ndvi_filled < 0.45)] = 2
    classes[ndvi_filled >= 0.45] = 3
    return classes


classes_antes = classify_ndvi(ndvi_antes)
classes_depois = classify_ndvi(ndvi_depois)

cmap = ListedColormap(["black", "#d9b38c", "#a8d08d", "#1a7a1a"])

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(classes_antes, cmap=cmap, vmin=0, vmax=3)
ax1.set_title("NDVI classes - Before")
ax1.axis("off")
ax2.imshow(classes_depois, cmap=cmap, vmin=0, vmax=3)
ax2.set_title("NDVI classes - After")
ax2.axis("off")
plt.tight_layout()
plt.show()

Code
def classify_evi(evi):
    """0 = sem dado, 
    1 = solo/água/urbano,  
    2 = vegetação"""
    evi_filled = np.ma.filled(evi, np.nan)
    classes = np.zeros_like(evi_filled, dtype=np.int8)
    classes[evi_filled < 0.75] = 1
    classes[(evi_filled >= 0.75)] = 2
    return classes


classes_antes = classify_ndvi(evi_antes)
classes_depois = classify_ndvi(evi_depois)

cmap = ListedColormap(["black", "#d9b38c", "#a8d08d", "#1a7a1a"])

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(classes_antes, cmap=cmap, vmin=0, vmax=3)
ax1.set_title("EVI classes - Before")
ax1.axis("off")
ax2.imshow(classes_depois, cmap=cmap, vmin=0, vmax=3)
ax2.set_title("EVI classes - After")
ax2.axis("off")
plt.tight_layout()
plt.show()

Code
# DO THE NDWI CLASSIFICATION HERE 

Difference between the images (before x after)

Now for an important part: let’s evaluate the difference between one image and the other. Note that this difference is computed pixel by pixel: each pixel is compared with the pixel at the same position in the other image. This requires the rasters to be aligned, projected in the same CRS and to have the same np.shape.

By subtracting the before NDVI from the after NDVI, we can see exactly where the vegetation was destroyed by the wave of tailings. Strongly negative values (blue) indicate loss of vegetation / soil covered by mud.

Code
ndvi_diff = np.ma.filled(ndvi_depois, np.nan) - np.ma.filled(ndvi_antes, np.nan)

plt.figure(figsize=(8, 6))
im = plt.imshow(ndvi_diff, cmap="RdBu", vmin=-1, vmax=1)
plt.colorbar(im, label="NDVI difference (after - before)")
plt.title("Impact of the dam collapse — NDVI difference")
plt.axis("off")
plt.show()

print("Mean NDVI difference:", np.nanmean(ndvi_diff))
print("Pixels with strong vegetation loss (diff < -0.3):", np.sum(ndvi_diff < -0.3))

Diferença média de NDVI: -0.037199162
Pixels com forte perda de vegetação (diff < -0.3): 13679

If 13,000 pixels changed in vegetation, and I know the size of each pixel, then I can estimate the affected area.

Code
# Here I assume every change in the image larger than 0.3 was vegetation loss caused by the dam collapse
total_pixels_alterados = np.sum(ndvi_diff < -0.3)

# knowing that each pixel has a 10 m resolution
area_m2 = total_pixels_alterados*10*10
area_km2 = area_m2 / 1e6

print(f"Affected area in m2: {area_m2}")
print(f"Affected area in km2: {area_km2}")
Area afetada em m2: 1367900
Area afetada em km2:1.3679

8. What about the water?

Mining tailings behave similarly to water/mud in the spectral bands. Let’s also look at the NDWI difference, which can reveal the mud and the silting of the Paraopeba River.

Code
ndwi_diff = np.ma.filled(ndwi_depois, np.nan) - np.ma.filled(ndwi_antes, np.nan)

plt.figure(figsize=(8, 6))
im = plt.imshow(ndwi_diff, cmap="RdBu_r", vmin=-1, vmax=1)
plt.colorbar(im, label="NDWI difference (after - before)")
plt.title("NDWI difference — possible silting of the Paraopeba River")
plt.axis("off")
plt.show()

[RAM] fim do notebook: 15.29 GB usados de 32.00 GB (58.1%) | disponível: 13.41 GB

References