Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save csaybar/11a56c97b74a5707496eda3eb50a8075 to your computer and use it in GitHub Desktop.
Save csaybar/11a56c97b74a5707496eda3eb50a8075 to your computer and use it in GitHub Desktop.
[Medium] RStudio Cloud and rgee NDVI animation
library(rgee)
library(sf)
library(magick)
ee_Initialize()
mask <- system.file("shp/arequipa.shp", package = "rgee") %>%
st_read(quiet = TRUE) %>%
sf_as_ee()
region <- mask$geometry()$bounds()
col <- ee$ImageCollection("MODIS/006/MOD13A2")$select("NDVI")
col <- col$map(function(img) {
doy <- ee$Date(img$get("system:time_start"))$getRelative("day", "year")
img$set("doy", doy)
})
distinctDOY <- col$filterDate("2013-01-01", "2014-01-01")
filter <- ee$Filter$equals(leftField = "doy", rightField = "doy")
join <- ee$Join$saveAll("doy_matches")
joinCol <- ee$ImageCollection(join$apply(distinctDOY, col, filter))
comp <- joinCol$map(function(img) {
doyCol = ee$ImageCollection$fromImages(img$get("doy_matches"))
doyCol$reduce(ee$Reducer$median())$set("date", img$date()$format("MMM"))
})
dates <- comp$aggregate_array("date")$getInfo()
visParams <- list(
min = 0.0,
max = 9000.0,
bands = "NDVI_median",
palette = c("FFFFFF", "CE7E45", "DF923D", "F1B555", "FCD163", "99B718",
"74A901", "66A000", "529400", "3E8601", "207401", "056201",
"004C00", "023B01", "012E01", "011D01", "011301")
)
rgbVis <- comp$map(function(img) {
do.call(img$visualize, visParams) %>%
ee$Image$clip(mask)
})
gifParams <- list(
region = region,
dimensions = 600,
crs = "EPSG:3857",
framesPerSecond = 2
)
animation <- ee_utils_gif_creator(rgbVis, gifParams, mode = "wb")
animation %>%
ee_utils_gif_annotate(
text = "NDVI: MODIS/006/MOD13A2",
size = 15,
color = "white",
location = "+10+10"
) %>%
ee_utils_gif_annotate(
text = dates,
size = 30,
location = "+290+350",
color = "white",
font = "arial",
boxcolor = "#000000"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment