Skip to content

Commit

Permalink
Add NDVI calculation
Browse files Browse the repository at this point in the history
Add a function that calculates NDVI for a given scene. It's better than
having to remember the equations all the time. The function can also
take care of adding some metadata and copying the metadata from the
original scene. The function allows customizing the names of the red and
NIR bands.
  • Loading branch information
leouieda committed Aug 23, 2024
1 parent bb7e359 commit f5a5a5c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
25 changes: 20 additions & 5 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@ Input and output
----------------

.. autosummary::
:toctree: generated/
:toctree: generated/

load_scene
save_scene
load_panchromatic

Processing
----------
Visualization
-------------

.. autosummary::
:toctree: generated/
:toctree: generated/

composite
pansharpen
equalize_histogram
adjust_l1_colors

Indices
-------

.. autosummary::
:toctree: generated/

ndvi

Processing
----------

.. autosummary::
:toctree: generated/

pansharpen
interpolate_missing

Sample datasets
Expand Down
1 change: 1 addition & 0 deletions xlandsat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from . import datasets
from ._composite import composite
from ._enhancement import adjust_l1_colors, equalize_histogram
from ._indices import ndvi
from ._interpolation import interpolate_missing
from ._io import load_panchromatic, load_scene, save_scene
from ._pansharpen import pansharpen
Expand Down
41 changes: 41 additions & 0 deletions xlandsat/_indices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2022 The xlandsat developers.
# Distributed under the terms of the MIT License.
# SPDX-License-Identifier: MIT
"""
Calculate indices based on data in xarray.Datasets
"""


def ndvi(scene, red_band="red", nir_band="nir"):
"""
Normalized difference vegetation index
Calculate the NDVI for the given scene, defined as:
.. math::
NDVI = \dfrac{NIR - Red}{NIR + Red}
Parameters
----------
scene : :class:`xarray.Dataset`
A Landsat scene, as read with :func:`xlandsat.load_scene`.
red_band : str
The name of the variable in ``scene`` that corresponds to the red band.
nir_band : str
The name of the variable in ``scene`` that corresponds to the NIR band.
Returns
-------
ndvi : :class:`xarray.DataArray`
The calculated NDVI, with the metadata attributes from the original
scene.
"""
red = scene[red_band]
nir = scene[nir_band]
result = (nir - red) / (nir + red)
result.name = "ndvi"
attrs = {"long_name": "normalized difference vegetation index"}
attrs.update(scene.attrs)
result = result.assign_attrs(attrs)
return result

Check warning on line 41 in xlandsat/_indices.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_indices.py#L34-L41

Added lines #L34 - L41 were not covered by tests

0 comments on commit f5a5a5c

Please sign in to comment.