English | Deutsch
NDWI (Sentinel-2)¶
The Normalized Difference Water Index shows open water. Water reflects more green light than near-infrared
light, while plants and soil do the opposite, so NDWI = (green - NIR) / (green + NIR) is above 0 for water and
below 0 for most land. The result is one number per pixel, between -1 and 1.
Download function |
|
|---|---|
Plot function |
|
Source |
Sentinel-2 Level-2A (collection |
Pixel size |
10 m |
Result |
one band of floating-point numbers, no unit, -1 … 1 |
Clouds |
scenes above |
Period |
mean of all observations of |
Example¶
from cdec import download_ndwi, plot_ndwi
spatial_extent = {"west": 8.721771, "south": 53.025027, "east": 8.904419, "north": 53.114965} # Bremen
temporal_extent = ["2024-06-01", "2024-07-01"] # June 2024, the end date is not included
path = download_ndwi("data/ndwi_bremen.tif", spatial_extent, temporal_extent)
plot_ndwi(path)
Bremen, June 2024. Blue: water, red: land. White: no data. Contains modified Copernicus Sentinel data 2024.¶
Things to know¶
Two indices are called NDWI. This page is the index of McFeeters (green and near-infrared), made to find open water. Gao’s index (near-infrared and short-wave infrared) measures the water in leaves; this package calls it NDMI. Check which one a book or a paper means.
Water is kept by default. The default
scl_classesis(4, 5, 6), because a water index without water is useless. NDVI and EVI drop water instead (NDVI (Sentinel-2)).Above 0 is a first guess, not a water map. Built-up areas and shadows can also have values close to 0 or slightly above. For a water mask combine the index with the scene classification (class 6, Scene classification (Sentinel-2)) or the land cover map.
The index is calculated from the mean reflectance of the period, not as the mean of the index of every date.
The plot uses red-white-blue colours and a fixed range of -1 to 1: red for land, blue for water, so the same colour always means the same NDWI and two maps can be compared by eye.
See also¶
NDMI (Sentinel-2), Scene classification (Sentinel-2), Filter clouds, Clouds and masking
Download¶
- cdec.download_normalized_difference_water_index(file_path, spatial_extent, temporal_extent, max_cloud_cover=20, scl_classes=(4, 5, 6), *, create_cog=True, overview_resampling='average')[source]¶
Short alias:
download_ndwiDownload a Sentinel-2 NDWI (water index) raster.
Green and near-infrared reflectance from Sentinel-2 Level-2A are masked using the Scene Classification Layer (SCL), then averaged over the requested period before calculating the Normalized Difference Water Index of McFeeters:
NDWI = (green - NIR) / (green + NIR)It is positive over open water and negative over land. This is not the index that some books also call NDWI and that uses short-wave infrared; that one is
cdec.download_normalized_difference_moisture_index(). The output retains the 10 m reflectance grid; SCL classification detail remains 20 m.- Parameters:
file_path (str | Path) – Destination path of the GeoTIFF file. Missing parent directories are created automatically.
spatial_extent (SpatialExtent) – Area to download: an openEO bounding-box dictionary such as
{"west": 8.7, "south": 53.0, "east": 8.9, "north": 53.1}(degrees, WGS 84), a Shapely geometry, or the path to a GeoJSON file.temporal_extent (TemporalExtent) – Start and end of the observation period, e.g.
["2024-06-01", "2024-07-01"]. The end date itself is not included, so this example covers all of June.max_cloud_cover (int) – Maximum cloud cover of a whole scene in percent. Scenes that are cloudier are not used at all. This scene filter complements
scl_classes, which removes single pixels.scl_classes (Sequence[int] | None) – Scene Classification Layer (SCL) classes to keep, as a tuple of class IDs. The default
SCL_LAND_AND_WATER=(4, 5, 6)keeps vegetation, non-vegetated land and water and drops clouds, cirrus, shadows, snow/ice, unclassified and defective pixels. Do not useSCL_LAND: it removes the water that this index looks for. UseNoneto switch SCL masking off. Runcdec.show_scl_classes()to see what each class means. Pixels without any retained observation remain no data.create_cog (bool) – If
True, write a Cloud Optimized GeoTIFF.overview_resampling (OverviewResampling) – Resampling method used to build COG overview levels.
- Returns:
Absolute path of the GeoTIFF that was written. The file has one band of floating-point numbers (
float32): the NDWI, a number without unit between -1 and 1 (open water is above 0, land below). Pixels without data are NaN, which the file declares as its no-data value. The pixel size is 10 m, in the UTM zone of the area (EPSG:32632 for Bremen).- Return type:
Path
Plot¶
- cdec.plot_normalized_difference_water_index(file_path, *, ax=None, title=None, cmap='RdBu', vmin=-1.0, vmax=1.0, colorbar=True, show=None)[source]¶
Short alias:
plot_ndwiDraw the NDWI of
download_normalized_difference_water_index.NDWI lies between -1 and 1: open water is above 0, land is below 0. The colour map goes from red (land) over white to blue (water). The range is fixed to -1 … 1, so two images always have the same colours and can be compared.
- Parameters:
file_path (str | Path) – The GeoTIFF written by
download_ndwi.ax (Axes | None) – A matplotlib
Axesto draw into.Nonecreates a new figure.title (str | None) – Title of the plot. Default: “NDWI (Sentinel-2)”. Use
""for no title.cmap (str) – Name of a matplotlib colour map.
vmin (float | None) – Values at the two ends of the colour map.
Nonetakes the smallest and largest value of the image instead.vmax (float | None) – Values at the two ends of the colour map.
Nonetakes the smallest and largest value of the image instead.colorbar (bool) – Draw a colour bar.
show (bool | None) – Call
plt.show()at the end. The default (None) shows the figure only if this function created it.
- Returns:
The axes with the plot, for further changes.
- Return type:
matplotlib.axes.Axes
- Raises:
ValueError – If the file does not have exactly one band.