0

I am trying to plot a raster over New Zealand in leaflet but I am having some issues. I've found that polygons and points/markers plot as expected:

library(raster)
library(leaflet)
library(dplyr)
library(sp)

p1 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(x = c(165, 165, 185, 185, 165), y = c(-50, -31, -31, -50, -50)))), 1)), proj4string=CRS("+init=epsg:4326"))
p2 <- data.frame(lng = runif(100, 165, 185), lat = runif(100, -50, -31))

leaflet() %>% 
    addTiles() %>% 
    addPolygons(data = p1) %>% 
    addMarkers(lat = p2$lat, lng = p2$lng)

And a raster with a maximum longitude of 180 will plot just fine:

r <- raster(xmn = 165, xmx = 180, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(r, opacity = 0.5)

But a raster with a longitude extending beyond 180 that covers all of New Zealand does not plot properly:

r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(r, opacity = 0.5)

And while I can rotate the raster and get it to plot, it splits the raster and plots the part beyond 180 back at 0:

r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(rotate(r), opacity = 0.5)

I'm wondering if anyone else has run into this issue and if so how to get around it?

lbusett
  • 5,801
  • 2
  • 24
  • 47
  • 1
    This was actually the very first issue posted for **mapview** https://github.com/environmentalinformatics-marburg/mapview/issues/6 about a year ago. So far, I have not come across a working solution for this. I need to have a look at this at the leafletjs level to see whether the issue stems from there. – TimSalabim Dec 07 '16 at 15:02
  • maybe you could use setView: setView(lng = 175, lat = -31, zoom = 4) – MLavoie Dec 07 '16 at 15:04
  • Maybe just converting to a different projection beforehand ? Unless you realy need to plot lat/lon. (by the way: a maximum latitude of 180 is clearly an impossibility. I think you meant "longitude") – lbusett Dec 07 '16 at 20:25
  • I'd be interested to see how you get on testing this issue at the leafletjs level @TimSalabim. I had a quick go but I've not worked with JavaScript before so I didn't get far. – Darcy Webber Jan 13 '17 at 00:34
  • Actually I haven't found time yet. It is not on top of my priority list right now, so don't expect too much too soon. – TimSalabim Jan 13 '17 at 09:31

1 Answers1

0

It looks as if this has been sorted in a recent update of leaflet. The following now works as expected:

library(raster)
library(leaflet)

r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, 
            crs = "+init=epsg:4326", nrows = 100, ncols = 100)
r[] <- rnorm(ncell(r))

leaflet() %>% addTiles() %>% addRasterImage(rotate(r), opacity = 0.5)

Note that the use of rotate is essential.