Skip to content

Description

Step purpose

Purpose: to calculate the normalized difference of the "green" or "nir" bands.

This step is highlighted below:

graph TB style D stroke:#f66,stroke-width:3px subgraph Process STAC item A[STAC Item] -.-> B A[STAC Item] -.-> C A[STAC Item] -.-> F subgraph scatter on bands B["crop(green)"]; C["crop(nir)"]; end B["crop(green)"] == crop_green.tif ==> D[Normalized difference]; C["crop(nir)"] == crop_green.tif ==> D[Normalized difference]; D == norm_diff.tif ==> E[Otsu threshold] end E -.-> F[Create STAC Catalog] F -.-> G[(storage)]

Code

The norm_diff script is a command-line tool for performing a normalized difference between two raster images.

It uses the click, rasterio, and numpy libraries to perform the calculation and save the result as a GeoTIFF file.

Here's an overview of what the script does:

  • It defines a command-line interface using the click library, with two arguments for providing the file paths of the two raster images that you want to calculate the normalized difference for.

  • The normalized_difference function is the main entry point. It opens the two input raster files specified as arguments.

  • It reads the data from the first raster (specified by rasters[0]) using rasterio, and it also copies the metadata (e.g., projection, geotransform) of this raster to be used for the output.

  • It then opens the second raster (specified by rasters[1]) and reads its data.

  • It updates the data type in the metadata to "float32" because the normalized difference result will be a floating-point image.

  • It creates an output raster named "norm_diff.tif" using rasterio. This output raster will have the same metadata as the first input raster, but it will be of data type float32.

  • It calculates the normalized difference between the two input arrays (array1 - array2) / (array1 + array2) and writes it to the output raster using dst_dataset.write().

The script is executable as a command-line tool as its usage is:

Usage: app.py [OPTIONS] RASTERS...

  Performs a normalized difference

Options:
  --help  Show this message and exit.

The Python code is provided here:

water-bodies/command-line-tools/norm_diff/app.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Normalized difference"""
import click
import rasterio
import numpy as np
from loguru import logger

np.seterr(divide="ignore", invalid="ignore")


@click.command(
    short_help="Normalized difference",
    help="Performs a normalized difference",
)
@click.argument("rasters", nargs=2)
def normalized_difference(rasters):
    """Performs a normalized difference"""

    logger.info(f"Processing the normalized image with {rasters[0]} and {rasters[1]}")

    with rasterio.open(rasters[0]) as ds1:
        array1 = ds1.read(1).astype(np.float32)
        out_meta = ds1.meta.copy()

    with rasterio.open(rasters[1]) as ds2:
        array2 = ds2.read(1).astype(np.float32)

    out_meta.update(
        {
            "dtype": "float32",
            "driver": "COG",
            "tiled": True,
            "compress": "lzw",
            "blockxsize": 256,
            "blockysize": 256,
        }
    )

    with rasterio.open("norm_diff.tif", "w", **out_meta) as dst_dataset:
        logger.info(f"Write norm_diff.tif")
        dst_dataset.write((array1 - array2) / (array1 + array2), 1)

    logger.info("Done!")


if __name__ == "__main__":
    normalized_difference()