I tried to add a raster file (stored in r_max) to my map.
r_max
is a RasterLayer with values from about 0 to 5:
class : RasterLayer
dimensions : 180, 360, 64800 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
source : map.tif
names : map
The command plot(r_max)
gives a nice colored plot, but the following code does not show the raster information on the generated map (the legend is visible though):
leaflet() %>% addTiles() %>%
setView(lng = 15.6, lat = 28.1, zoom = 3.9) %>%
addRasterImage(r_max, opacity=0.8, colors=colorNumeric("Blues", domain=NULL)) %>%
addLegend(pal = colorNumeric("Blues", domain=NULL), values = values(r_max), title = "Max", opacity=1)
The source of the data is a GeoTIFF file downloaded from the Copernicus Interactive Climate Atlas.
This worked well in the past with some other raster data. I tried several options for the addRasterImage
without success.
I tried to add a raster file (stored in r_max) to my map.
r_max
is a RasterLayer with values from about 0 to 5:
class : RasterLayer
dimensions : 180, 360, 64800 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
source : map.tif
names : map
The command plot(r_max)
gives a nice colored plot, but the following code does not show the raster information on the generated map (the legend is visible though):
leaflet() %>% addTiles() %>%
setView(lng = 15.6, lat = 28.1, zoom = 3.9) %>%
addRasterImage(r_max, opacity=0.8, colors=colorNumeric("Blues", domain=NULL)) %>%
addLegend(pal = colorNumeric("Blues", domain=NULL), values = values(r_max), title = "Max", opacity=1)
The source of the data is a GeoTIFF file downloaded from the Copernicus Interactive Climate Atlas.
This worked well in the past with some other raster data. I tried several options for the addRasterImage
without success.
- Can you please edit your question to make it a minimal, self-contained, reproducible example? That is, create a small raster with code, and use that? – Robert Hijmans Commented Mar 12 at 19:59
2 Answers
Reset to default 2This is a known leaflet
bug since at least November 2022. It occurs when the latitude coordinates of a raster extend to -90/90. I assume none of your other files that your code worked with extended to latitude -90/90?
Either way, here is workaround that uses the terra
package as raster
has been deprecated (raster
equivalent of solution is detailed at the end for reference). This method is not ideal as it involves terra::crop()
ing the data e.g. you lose some data at the poles. However, given your example use case, the impact is arguably trivial.
library(terra)
# terra 1.8.15
library(leaflet)
packageVersion("leaflet")
# [1] ‘2.2.2’
# Load GeoTIFF from .zip in working directory, previously
# downloaded from https://atlas.climate.copernicus.eu/atlas
unzip("CMIP6-Mean temperature.zip",
files = "map.tif")
r_max <- rast("map.tif")
# Increase r_max resolution to reduce impact of crop()
r_max <- disagg(r_max, 2)
# Crop r_max latitude values
r_crop <- crop(r_max, ext(-180, 180, -89.5, 89.5))
# Plot
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng = 15.6, lat = 28.1, zoom = 3.9) %>%
addRasterImage(r_crop,
opacity = 0.8,
colors = colorNumeric("Blues", domain = NULL)) %>%
addLegend(pal = colorNumeric("Blues", domain = NULL),
values = values(r_crop),
title = "Max",
opacity = 1)
Screencap of result:
The raster
equivalent for prepping the data for leaflet
:
library(raster)
r_max <- raster("data/map.tif")
r_max <- disaggregate(r_max, 2)
# Explicitly define package for crop in case terra is loaded
r_crop <- raster::crop(r_max, extent(-180, 180, -89.5, 89.5))
Example data
library(terra)
x <- rast()
values(x) <- sample(5, ncell(x), replace=TRUE)
Use terra's wrapper around leaflet
plet(x, type="classes")
To make a nicer map:
x <- mask(x, geodata::world(path="."), names="whatever")
plet(x, type="classes")