# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.9.7 64-bit (''spyndex'': conda)'
#     name: python3
# ---

# # Earth Engine

# [![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/spyndex/blob/main/docs/tutorials/ee.ipynb)
# ![level7](https://raw.githubusercontent.com/davemlz/spyndex/main/docs/_static/level7.png)

# New level: Level 7 - `spyndex + Google Earth Engine`!
#
# Remember to install `spyndex`!

# !pip install -U spyndex

# Now, let's start!
#
# First, import `spyndex` and `Earth Engine`:
#
# > We also import `eemont` and `geemap` to make things easier.

import spyndex
import ee, eemont, geemap

# And remember to initialize `Earth Engine`:

Map = geemap.Map()

# ## `ee.Number`
#
# Just as Python Built-In types, the `ee.Number` can be used with `spyndex`!

NIR = ee.Number(0.678)
RED = ee.Number(0.123)

# Let's check our data types:

print(f"NIR type: {type(NIR)}, value: {NIR.getInfo()}")
print(f"RED type: {type(RED)}, value: {RED.getInfo()}")

# And let's compute the `NDVI`.

idx = spyndex.computeIndex("NDVI",{"N": NIR, "R": RED})

# And our result is also an `ee.Number`!

print(f"idx type: {type(idx)}, value: {idx.getInfo()}")

# ## `ee.Image`
#
# All spectral indices from `Awesome Spectral Indices` can be computed automatically in Python for `Earth Engine` by using `eemont` for most datasets (Sentinel-2, MODIS, Landsat, etc.). However, some datasets are not included. NAIP is a good example, let's use a NAIP image!

img = ee.Image("USDA/NAIP/DOQQ/m_3009057_ne_15_1_20170910")

# NAIP images values go from 0 to 255, so, let's correct that!

img = img / 255.0

# Let's check the image metadata:

img.getInfo()

# Great! We have 4 bands (R, G, B, N) and all of them go now from 0 to 1.
#
# Let's compute some indices!

idx = spyndex.computeIndex(
    index = ["VARI","ExG","NDVI"],
    params = {
        "R": img["R"],
        "G": img["G"],
        "B": img["B"],
        "N": img["N"]
    }
)

# If we check the indices, we'll see that the result is another `ee.Image`!

print(f"idx type: {type(idx)}")

# Now, let's add those indices as new bands in the original image.

img = img.addBands(idx)

# If we check our bands, we'll see that the new bands (indices) were added:

img.bandNames().getInfo()

# Let's visualize evrything interactively with `geemap`!
#
# First, import the colormaps:

import geemap.colormaps as cm

# And then, plot everything!

Map = geemap.Map()
Map.addLayer(img[["R","G","B"]],{"min":0,"max":0.3},"RGB")
Map.addLayer(img["VARI"],{"palette":cm.palettes.ndvi},"VARI")
Map.addLayer(img["ExG"],{"palette":cm.palettes.ndvi},"ExG")
Map.addLayer(img["NDVI"],{"palette":cm.palettes.ndvi},"NDVI")
Map.centerObject(img,13)
Map

# Amazing!!!! :)

StarcoderdataJupyterScriptsDedupFiltered