Created
August 9, 2020 10:22
-
-
Save csaybar/9ae7378d2d98276872eb03a7bbe6c1a8 to your computer and use it in GitHub Desktop.
rgee animations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rgee) | |
library(magick) | |
ee_Initialize() | |
col <- ee$ImageCollection("JRC/GSW1_1/YearlyHistory")$map(function(img) { | |
year <- img$date()$get("year") | |
yearImg <- img$gte(2)$multiply(year) | |
despeckle <- yearImg$connectedPixelCount(15, TRUE)$eq(15) | |
yearImg$updateMask(despeckle)$selfMask()$set("year", year) | |
}) | |
appendReverse <- function(col) col$merge(col$sort('year', FALSE)) | |
# ----------------------------------- | |
# 1 Basic Animation - Ucayali Peru | |
# ----------------------------------- | |
bgColor = "FFFFFF" # Assign white to background pixels. | |
riverColor = "0D0887" # Assign blue to river pixels. | |
## 1.1 Create the dataset | |
annualCol = col$map(function(img) { | |
img$unmask(0)$ | |
visualize(min = 0, max = 1, palette = c(bgColor, riverColor))$ | |
set("year", img$get("year")) | |
}) | |
basicAnimation <- appendReverse(annualCol) | |
## 1.2 Set video arguments | |
aoi <- ee$Geometry$Rectangle(-74.327, -10.087, -73.931, -9.327) | |
videoArgs = list( | |
dimensions = 600, # Max dimension (pixels), min dimension is proportionally scaled. | |
region = aoi, | |
framesPerSecond = 10 | |
) | |
## 1.3 Download GIF | |
animation_ucayali <- basicAnimation$getVideoThumbURL(videoArgs) | |
ucayali_gif <- tempfile() | |
download.file(animation_ucayali, ucayali_gif) | |
## 1.4 Read GIF using magick | |
animation <- image_read(ucayali_gif) | |
animation_info <- image_info(animation) | |
ani_geom <- sprintf("%sx%s", min(animation_info$width),min(animation_info$height)) | |
new_animation <- image_extent(animation, ani_geom, gravity = "NorthWest") | |
years_info <- basicAnimation$aggregate_array("year")$getInfo() | |
## 1.5 Add text annotations | |
for (index in seq_along(years_info)) { | |
new_animation[index] <- new_animation[index] %>% | |
image_annotate( | |
text = years_info[index], | |
size = 30, | |
color = "#E5E4E5", | |
location = "+190+40", | |
font="consolas", | |
boxcolor = rgb(0,0,0) | |
) %>% | |
image_annotate( | |
text = "JRC/GSW1_1/YearlyHistory", | |
size = 13, | |
color = "#E5E4E5", | |
location = "+10+550", | |
font="consolas", | |
boxcolor = rgb(0,0,0) | |
) | |
} | |
image_write_gif( | |
image = new_animation, | |
path = "/home/csaybar/demo_01.gif" | |
) | |
# ----------------------------------- | |
# 2. Fading history animation | |
# ----------------------------------- | |
## 2.1 Create the dataset | |
bgImg <- ee$Image(1)$visualize(palette = bgColor) | |
fadeFilter <- ee$Image(1)$visualize(palette = bgColor, opacity = 0.1) | |
fadeFilterCol <- col$map(function(img) { | |
imgVis <- img$visualize(palette = riverColor) | |
imgVis$blend(fadeFilter)$set('year', img$get('year')) | |
}) | |
yearSeq <- ee$List$sequence(1984, 2018) | |
year_fun <- function(year) { | |
fadeComp = fadeFilterCol$filter(ee$Filter$lte("year", year))$sort("year")$mosaic() | |
thisYearImg = col$filter(ee$Filter$eq("year", year))$first()$visualize( | |
palette = riverColor | |
) | |
bgImg$blend(fadeComp)$blend(thisYearImg)$set("year", year) | |
} | |
fadeCol <- ee$ImageCollection$fromImages( | |
images = yearSeq$map(ee_utils_pyfunc(year_fun)) | |
) | |
## 2.2 Set video arguments | |
aoi <- ee$Geometry$Rectangle(-74.327, -10.087, -73.931, -9.327) | |
videoArgs = list( | |
dimensions = 600, # Max dimension (pixels), min dimension is proportionally scaled. | |
region = aoi, | |
framesPerSecond = 10 | |
) | |
## 2.3 Download GIF | |
animation_ucayali <- fadeCol$getVideoThumbURL(videoArgs) | |
ucayali_gif <- tempfile() | |
download.file(animation_ucayali, ucayali_gif) | |
## 2.4 Read GIF using magick | |
animation <- image_read(ucayali_gif) | |
animation_info <- image_info(animation) | |
ani_geom <- sprintf("%sx%s", min(animation_info$width), min(animation_info$height)) | |
new_animation <- image_extent(animation, ani_geom, gravity = "NorthWest") | |
years_info <- fadeCol$aggregate_array("year")$getInfo() | |
## 2.5 Add text annotations | |
for (index in seq_along(years_info)) { | |
new_animation[index] <- new_animation[index] %>% | |
image_annotate( | |
text = years_info[index], | |
size = 30, | |
color = "#E5E4E5", | |
location = "+210+40", | |
font="consolas", | |
boxcolor = rgb(0,0,0) | |
) %>% | |
image_annotate( | |
text = "JRC/GSW1_1/YearlyHistory", | |
size = 13, | |
color = "#E5E4E5", | |
location = "+10+520", | |
font="consolas", | |
boxcolor = rgb(0,0,0) | |
) | |
} | |
## 2.6 It seems there is a small bug here, preview looks great but not 'magick::image_write_gif' | |
## exportation. | |
new_animation | |
# image_write_gif( | |
# image = new_animation, | |
# path = "/home/csaybar/demo_02.gif" | |
# ) |
Author
csaybar
commented
Aug 9, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment