Description
Step purpose
Purpose: to calculate the normalized difference of the "green" or "nir" bands.
This step is highlighted below:
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]
) usingrasterio
, 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 typefloat32
. -
It calculates the normalized difference between the two input arrays
(array1 - array2) / (array1 + array2)
and writes it to the output raster usingdst_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 |
|