1

Is there any way to project raster/images as animation onto Leaflet?

In R raster package we can animate raster layers with: animate(rasters). And you can save it as GIF with:saveGIF(animate(rasters)).

But how do we project the GIF onto a leaflet?

My workaround is to create PNG files

saveWidget(m, "temp.html")
webshot("temp.html", file = "filename.png"), cliprect = "viewport")

and creating a GIF:

im.convert(png_files, output = "output.gif")

I am sure there are a lot of other workarounds to do this with Javascript and CSS but I was wondering if it is possible to do it in R without manipulating Javascript and still have that dynamic map with raster animations.

I was directed to create a new question for this from this question which is a little bit different: Create a gif from a series of Leaflet maps in R

Community
  • 1
  • 1
  • I don't think there is an out-of-the-box solution for this, but `mapview` offers some interesting and improving functionality with `raster` and `rasterBrick`. I'll keep thinking about easy ways to achieve an animated GIF. – timelyportfolio Jun 06 '16 at 02:28

1 Answers1

0

Here's a workaround (showing an animated.gif in a popup):

library(mapview)

gif <- "https://upload.wikimedia.org/wikipedia/commons/d/d6/MeanMonthlyP.gif"

viewExtent(kiliNDVI, fillOpacity = 0, opacity = 0,
           popup = popupImage(gif, src = "remote")) + kiliNDVI[[1]]

viewExtent basically draws a rectangle around a Raster* object. We don't see it on the map because we make it transparent. popupImage() lets you embed images in popups. In case you want a local image, set src = "local".

TimSalabim
  • 5,604
  • 1
  • 25
  • 36
  • 1
    What package provides `popupImage`? – jbaums Jun 09 '16 at 04:08
  • 2
    **mapview** version 1.1.0 – TimSalabim Jun 09 '16 at 06:46
  • 1
    Thanks Tim, I am getting this error when I run `mapview` with `popupImage`: `Error: 'onRender' is not an exported object from 'namespace:htmlwidgets' ` – Ebrahim Jahanshiri Jun 09 '16 at 12:59
  • Sorry, I have no idea where that comes from because 1. `popupImage` doens't use `onRender` and 2. `onRender` is an exported object from `namespace:htmlwidgets`. Maybe update your packages? – TimSalabim Jun 09 '16 at 13:29
  • Thanks, Tim, I was finally able to replicate the code in my situation. It is a great way to animate the `GIF`s on top of maps. But the popup size was very small for me since I am just showing the same map with `GIF`s that I have created from my `rasters` on top of the same map. – Ebrahim Jahanshiri Jun 10 '16 at 06:53
  • 1
    You can set `width` and `height` in pixels to enlarge the image. But the popup div will not scale beyond 300px. This should, however, be resolved with some of the next releases of **leaflet** (I hope). For more examples on how to use the popup functions in **mapview** see http://environmentalinformatics-marburg.github.io/mapview/popups/html/popups.html – TimSalabim Jun 10 '16 at 07:36