tmap
tmap copied to clipboard
tmap_save / tmap_animation memory usage
Recently, I've been working on a project with a few hundreds of raster layers and a goal to animate them. This way, I found out that tmap uses a lot of memory for saving maps. If I understand correctly, tmap_animation uses tmap_save in the background, and tmap_save takes a lot of RAM to save each frame. You can find a simplified example below - it needs ~16GB of RAM to work...
library(tmap)
library(raster)
library(spDataLarge)
data("land")
land2 = as(land, "Raster")
raster_stack = stack(replicate(50, land2))
format(object.size(raster_stack), "MB")
my_map = tm_shape(raster_stack) +
tm_raster() +
tm_facets(ncol = 1, nrow = 1)
format(object.size(my_map), "MB")
tmap_animation(my_map, "anim.gif")
Yes, tmap_save
is used to draw the animation frames. I've done some profiling (Rprof
and the profvis
package) in the past to increase the computation speed, but not yet to improve memory usage. We should definitely do this.
Another thing: when you don't specify width and height to tmap_animation
, it executes a tmap call to estimate the aspect ratio. So it would be a little more efficient to specify width and height in advance.
With the new version of tmap_animation
(#446) there is still a memory accumulation up to 16GB. The strange thing is that the memory remains occupied after the animation has been saved successfully (until RStudio crashes).
It is not a perfect solution, rather a hacky one, but it worked for one of my projects to limit the memory accumulation. Instead of saving all of layers, I split it into several rasterstacks and then save (note: it uses the raster package):
d <- paste(tempdir(), "/tmap_plots", sep = "/")
if (dir.exists(d)) unlink(d, recursive = TRUE)
dir.create(d, showWarnings = FALSE)
n <- raster::nlayers(rasterstack)
l <- 1
step <- ifelse(n < 20, n, 20)
for (i in seq(1, n, by = step)){
r1 <- raster::subset(rasterstack, i:(i + (step - 1)))
tm <- tmap::tm_shape(r1) +
tmap::tm_raster(style = "cont", palette = "viridis", title = "Value:")
suppressMessages(tmap::tmap_save(
tm,
filename = paste(d,
paste0("plot", sprintf("%03d", l), "%03d.png"),
sep = "/"),
width = width,
height = height,
dpi = dpi
))
l <- l + 1
}
Hello, I would also like to request improved memory usage by tmap_save
please.
When visualising large numbers of spatial objects tmap
is able to display the objects in an interactive visualisation in a web browser on my machine easily but when I attempt to save the visualisation as a static HTML file R fills the same machine's RAM (then SWAP space) and then crashes.
Otherwise, my experience with tmap
has been great.
Thank you for all your work.
The tmap v4 RAM requirement for my example is now ~6GB RAM.