How to Build a GIF of Satellite Imagery in R

Print More

Русский

“The timelapse imagery of Chennai’s disappearing reservoirs is mind boggling,” Earther senior reporter Brian Kahn wrote on Twitter recently. Kahn had just published the piece “Why Chennai, India’s Sixth Biggest City, Has Run Out of Water,” which was accompanied by several GIFs of the city’s vanishing reservoirs.

When asked how he’d built the GIFs, Kahn pointed us to the European Space Agency’s Sentinel Hub, which allows users to explore satellite imagery. The Sentinel Hub Playground service is especially user-friendly, letting users quickly access and visualize Sentinel-2, Landsat 8, DEM, and MODIS images.

We’ve attempted to recreate that animation of Chennai’s disappearing Lake Puzhal reservoir using Sentinel Hub and RStudio. The process and code below could be used for countless other animations to show, say, dam collapses, deforestation, forest fires, and more.

By the way, if you’d rather not use R, you could use Sentinel Hub’s EO Browser to create a GIF with their user interface. Scroll down to the bottom of this post for details.

Note: For an introduction to R and RStudio, check out these tutorials.

Downloading the Satellite Imagery

After navigating to Sentinel Hub Playground, type “Chennai, India” into the search bar and drag the window over to the reservoir of interest. Make sure you have “Sentinel-2” selected under Datasets on the top right and “Natural color” in the Rendering tab on the far left.

Select the calendar icon on the top nav bar and start looking through the images available of Chennai.

Once you’ve landed on an image you like – ideally without much cloud cover – click the “Generate” button on the bottom left and the pop-up window will have a “Download Image” button. Click it.

I ended up downloading 15 JPGs – from 2018-06-15 to 2019-06-15 – after clicking through the intervening months and finding the least cloudy days. I kept Playground’s file naming structure, which looked like: “Sentinel-2 image on 2018-06-15.jpg,” and saved them all to a folder I could navigate to in RStudio.

Animating JPGs in R Using “Magick”

Next, thanks to an awesome tutorial from ecologist Ryan Peek, I knew I could import the files into RStudio and create a GIF using the R packages “dplyr” and “purrr” for data wrangling and “magick” for image processing.

It was as easy as plug-and-play: You tell RStudio where the files are, then read them in, then join them, then give the GIF a title like “Lake Puzhal, Chennai: June 2018 – June 2019,” and then animate the GIF at a frame rate of 4 fps.

library(dplyr)
library(purrr) 
library(magick)

chennai_gif <- list.files(path = "chennai/", pattern = "*.jpg", full.names = T) %>% 
    map(image_read) %>% 
    image_join() %>% 
    image_annotate("Lake Puzhal, Chennai: June 2018 - June 2019", location = "+10+10", size = 20, color = "white") %>%
    image_animate(fps=4) %>% 
    image_write("chennai.gif") 

Finally, head over to your working directory and you should have your chennai.gif. Worked like a charm.

More details and tutorials on the incredible “magick” package here.

Exporting a Square GIF for Social

Using the image_crop() function, you can crop your GIF and scoot over using the two “+” arguments. More details here.

chennai_square <- list.files(path = "chennai/", pattern = "*.jpg", full.names = T) %>% 
  map(image_read) %>% 
  image_join() %>% 
  image_crop("500x500+450+125") %>%
  image_annotate("Lake Puzhal, Chennai: June 2018 - June 2019", location = "+10+10", size = 16, color = "white") %>%
  image_animate(fps=4) %>% 
  image_write("chennai-box.gif") 

Using Sentinel Hub’s EO Browser to Create GIFs With No Code

Watchdog group Bellingcat has a great tutorial for using Sentinel Hub’s EO Browser. Basically, you get a free login, head over to your area of interest, and click the “Create timelapse animation” icon on the right.

Then, you select your dates and amount of cloud cover – I chose 27% for the below GIF – and export the file by clicking “Download.” Easy.

Final GIF looks like:

More Satellite Imagery Datasets

More “Geojournalism” Resources


This article was originally published on Storybench and is reproduced here with permission.

Aleszu Bajak is the editor of Storybench. He is a science journalist and a former Knight Science Journalism Fellow at MIT. He is also an alum of Science Friday, the founder of LatinAmericanScience.org, and is passionate about breaking down the divide between journalists, developers, and designers.

Leave a Reply

Your email address will not be published. Required fields are marked *