# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# [![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/eemont/blob/master/docs/tutorials/016-Spectral-Indices-From-Awesome-Spectral-Indices-List.ipynb)

# + [markdown] id="jZEthLln92Ep"
# # Spectral Indices From the [Awesome Spectral Indices for GEE](https://github.com/davemlz/awesome-ee-spectral-indices)

# + [markdown] id="dNa470OZ8Oec"
# _Tutorial created by **<NAME>**_: [GitHub](https://github.com/davemlz) | [Twitter](https://twitter.com/dmlmont)
#
# - GitHub Repo: [https://github.com/davemlz/eemont](https://github.com/davemlz/eemont)
# - PyPI link: [https://pypi.org/project/eemont/](https://pypi.org/project/eemont/)
# - Conda-forge: [https://anaconda.org/conda-forge/eemont](https://anaconda.org/conda-forge/eemont)
# - Documentation: [https://eemont.readthedocs.io/](https://eemont.readthedocs.io/)
# - More tutorials: [https://github.com/davemlz/eemont/tree/master/docs/tutorials](https://github.com/davemlz/eemont/tree/master/docs/tutorials)

# + [markdown] id="CD7h0hbi92Er"
# ## Let's start!

# + [markdown] id="E0rc6Cya92Es"
# If required, please uncomment:

# + id="NYzyvKtk92Es"
# #!pip install eemont
# #!pip install geemap

# + [markdown] id="x3Rm3qt_92Et"
# Import the required packges.

# + id="H0C9S_Hh92Et"
import ee, eemont, geemap
import geemap.colormaps as cm

# + [markdown] id="k1sdX2p592Eu"
# Authenticate and Initialize Earth Engine and geemap.

# + id="7QDXqVwy8Oef"
Map = geemap.Map()
# -

# Let's define a point of interest:

poi = ee.Geometry.PointFromQuery("El Cairo, Egypt",user_agent = "eemont-tutorial-016")

# Let's preprocess the S2 dataset:

S2 = (ee.ImageCollection("COPERNICUS/S2_SR")
      .filterBounds(poi)
      .filterDate("2020-01-01","2021-01-01")
      .maskClouds()
      .scaleAndOffset())

# ## New method for computing Spectral Indices

# In order to compute Spectral Indices from the Awesome List of Spectral Indices, please use the `spectralIndices` method for ee.Image and ee.ImageCollection classes (This method will eventually replace the `index` method):

S2offline = S2.spectralIndices(["NDVI","kNDVI","NDWI","SeLI"]).median()

# The `spectralIndices` method will look into the Awesome List of Spectral Indices that comes with the eemont package (offline). If the list is outdated (the local list is updated with every new version or with the dev installation), you can use the `online` argument to use the most recent list directly from the [Awesome Spectral Indices for GEE repositoy](https://github.com/davemlz/awesome-ee-spectral-indices):

S2online = S2.spectralIndices(["NDVI","kNDVI","NDWI","SeLI"],online = True).median()

# The `spectralIndices` has new arguments for some of the new indices that were added to the list:
#
# - cexp (float, default = 1.16) – Exponent used for OCVI.
# - nexp (float, default = 2.0) – Exponent used for GDVI.
# - alpha (float, default = 0.1) – Weighting coefficient used for WDRVI.
# - slope (float, default = 1.0) – Soil line slope.
# - intercept (float, default = 0.0) – Soil line intercept.

# Check the [eemont API Reference](https://eemont.readthedocs.io/en/latest/classes/index.html) and the [Awesome Spectral indices Documentation](https://awesome-ee-spectral-indices.readthedocs.io/en/latest/) for more info.

# Now, let's visualize our indices!

# +
vegetation_vis = {
    "min": 0,
    "max": 1,
    "palette": cm.palettes.ndvi
}

water_vis = {
    "min": 0,
    "max": 1,
    "palette": cm.palettes.ndwi
}
# -

# Visualize everything with `geemap`:

Map.addLayer(S2online.select("NDVI"),vegetation_vis,"NDVI")
Map.addLayer(S2online.select("kNDVI"),vegetation_vis,"kNDVI")
Map.addLayer(S2online.select("SeLI"),vegetation_vis,"SeLI")
Map.addLayer(S2online.select("NDWI"),water_vis,"NDWI")
Map.centerObject(poi,8)
Map

StarcoderdataJupyterScriptsDedupFiltered