Animated topographic visualization in R

[This article was first published on Stories by Bálint Komjáti on Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I created the following video in R from ground up:

https://vimeo.com/645508457

This article will give you a general understanding of the process creating the visualization above. To replicate my results please follow the steps outlined in this REPO.

But first, what is this video about?

Aerothlon is an alpine triathlon race of mountain running, mountain biking and paragliding. I had the pleasure to take part in the 2021 edition in Kleinarl (AT). The video visualizes the efforts of all athletes at 350x speed.

Ok, how to make it?

1. Getting the data

You need the following data for the surrounding area of the tracklogs:

  • Digital elevation data
  • Overlay satellite image

Although there are many free sources available out there within R, the geoviz package provides great one-liners for this purpose, e.g.:

digital_elev_data <- geoviz::mapzen_dem(center_of_location_lat,
                                        center_of_location_long,
                                        area_side_in_km)

2. Prepare the 3D scene

The heart of this visualization is the awesome R package rayshader which renders the 3D scene of the elevation data, projects the overlay image onto the surface and takes care of shading as well.

The package is fully automated, supports piping and so very intuitive. After converting the elevation data into a matrix (named elmat below) you just basically need to play with the sun altitude and direction:

scene <- elmat %>%
         rayshader::sphere_shade(sunangle = sunangle, 
                                 sunaltitude = sunaltitude) %>%
         rayshader::add_shadow(rayshader::ray_shade(elmat) %>%
         rayshader::add_shadow(rayshader::lamb_shade(elmat) %>% 
         rayshader::dd_overlay(overlay_image) %>%
         rayshader::plot_3d(elmat)

This will pop up an rgl window showing your scene rendered in 3D.

3. Create animation frames

The concept is to sequentially add a fraction of the tracklogs to the scene (utilizing the geoviz::add_gps_to_rayshader() function), adjust camera view and save each step as a standalone image.

To large extend this visualization and the corresponding technology was inspired by the package rayshaderanimate, however I decided to write my own animation function due to the massive changes needed to fit my needs:

create_rayshader_animantion_frames() wraps the snippets above inside a loop and has an extended argument list to adjust e.g. camera movements, video resolution, video length.

Once the frames were generated I retouched each using a tailor made Photoshop batch process. (This is not documented, but not essential either.)

4. Render video

For this you need to have ffmpeg installed on your computer. This little application will render an mp4 video from the images generated before. With the function render_rayshader_animantion() under my repo you can invoke ffmpeg from R (concept again taken from the package rayshaderanimate).

And that’s it!

I am proudly contributing to www.r-bloggers.com as well with this post .

To leave a comment for the author, please follow the link and comment on their blog: Stories by Bálint Komjáti on Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)