Skip to content

Instantly share code, notes, and snippets.

@KMarkert
Created October 30, 2021 14:08
Show Gist options
  • Save KMarkert/502f771fca046e0037c11b3ffaf5ca70 to your computer and use it in GitHub Desktop.
Save KMarkert/502f771fca046e0037c11b3ffaf5ca70 to your computer and use it in GitHub Desktop.
using EarthEngine
Initialize()
# base ndvi function to do the calculation
function ndvi(n::EE.AbstractEEObject, r::EE.AbstractEEObject)
return (n - r) / (n + r)
end
# ndvi function for image, will extract out the correct bands
# and then pass to base ndvi function
function ndvi(img::EE.Image)
n = select(img, ["B5"])
r = select(img, ["B4"])
ndvi(n,r)
end
# ndvi function for image, will extract out the correct values
# and then pass to base ndvi function
function ndvi(feature::EE.Feature)
n = get(feature, "B5")
r = get(feature, "B4")
ndvi(n,r)
end
# get a Landsat 8 image collection
ic = sort(
limit(
filterDate(
EE.ImageCollection("LANDSAT/LC08/C01/T1_SR"),
"2018-01-01", "2019-01-01"
),
100, "CLOUD_COVER"),
"system:time_start", false
)
# get fist image from collection
img = first(ic)
# sample the image so we have a feature collection
fc = sample(img; scale=30, numPixels=500)
# apply ndvi function on the feature collection
# we have to use @eefunc and provide the input type to force
# the Python side to do the correct thing with a mapped function
ndvi_fc = map(fc, @eefunc ndvi EE.Feature)
# apply ndvi function on the image collection
# we have to use @eefunc and provide the input type to force
# the Python side to do the correct thing with a mapped function
ndvi_ic = map(fc, @eefunc ndvi EE.Image)
# apply ndvi on image
ndvi_img = ndvi(img)
n_value = EE.Number(0.1)
r_value = EE.Number(0.2)
# apply ndvi on ee.Number values
ndvi_value = ndvi(n_value, r_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment