-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rasters over international date line #6
Comments
I forgot to mention that there is no problem with polygons crossing the international date line. For example, this works: p <- 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"))
mapview(p) |
So far, I can only confirm that this is an issue of the underlying leaflet package (though it may well be a problem of leaflet.js). 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) The only way of getting around this in a half decent manner (with a warning) is to rotate the raster, however, this will draw the raster bit beyond the date line at the far western end of the map instead of simply extending it towards the east. mapview(rotate(r))
leaflet() %>% addTiles() %>% addRasterImage(rotate(r)) All other attempts of cutting the raster at the date line and drawing it in 2 instances have failed so far. I have reproduced your issue at rstudio/leaflet#225. Let's see what they come up with. |
I had a similar problem, and found using Here's the reproducible code and results... define rasterlibrary(raster)
library(leaflet)
# define raster that exceeds [-180,180]
r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r)) map with > 180 chopped offleaflet() %>% addTiles() %>% addRasterImage(r) %>% addGraticule() option A:
|
Thanks! |
Sparked by r-spatial/stars#256 I think I finally found a way to make this work. library(raster)
library(leaflet)
# define raster that exceeds [-180,180]
r <- raster(xmn = 165, xmx = 211, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(r) %>% addGraticule()
# option 2: shift and bisect into raster pieces left and right of dateline to minimize size
b <- shift(r, -360)
b1 <- crop(b, extent(-180,180,-90,90), snap="in") %>% trim()
b1 = shift(b1, -180 - xmin(b1))
b2 <- crop(b, extent(-360,-180,-90,90), snap="in") %>% shift(360) %>% trim()
b2 = shift(b2, 180 - xmax(b2))
# map with bisected chunks, worldCopyJump = TRUE
leaflet(options=leafletOptions(worldCopyJump = TRUE)) %>%
addTiles() %>%
addRasterImage(b1) %>%
addRasterImage(b2) %>%
addGraticule() One thing that needs to happen with this approach though is that the color scales need to be defined globally so that coloring respects the value range of the entire image... Maybe there is a better approach? At least this seems to work |
Kia ora Tim, much appreciated. The issue is that in shifting the cropped rasters it results in the coordinates of the pixels being shifted so that they may not long reflect the what was originally observed on the ground at a particular lat/long and recorded in a raster cell for that area. So unless there's any other trick for getting leaflet to draw pixels that actually span the 180 line? - I'm a bit stumped. Thanks again |
Mapview seems to not be supporting rasters going over the international date line.
This works fine:
But not this, with the raster being cropped and the cells being stretched:
Any idea how I can make this work?
Thank you for your package. This is quite promising.
The text was updated successfully, but these errors were encountered: