0

Problem

I would like to create interactive maps of a regular grid that comes in a CRS which is not WGS84. In my case the CRS is EPSG:25833 which has unit metres.

Given the comment here, it does not seem to be possible to "display rasters with arbitrary crs".

Is this still the case? Or is there some kind of hack/solution now using packages like plainview, leaflet, mapview or tmap (or some other package I'm not aware of)?

Moreover, I do not understand what exactly mapview::mapview shows when I feed it a non-lat/lon stars object. See the following example, where I suppose that the sf object is rendered correctly whereas the stars object is not?

Would tmap::tmap_leaflet do a better job here?

Example

library(dplyr)
library(mapview)
library(sf)
library(raster)
library(stars)
library(tmap)

stars_obj <- raster(
  # Rough bbox of the city of Berlin, Germany
  xmn=360000, xmx=430000, 
  ymn=5790000, ymx=5850000,
  # CRS with units metres, NOT lat/lon
  crs = st_crs(25833)$wkt,
  # Grid cell size: 10km x 10km
  resolution = 10000) %>% 
  st_as_stars()
stars_obj <- stars_obj %>% 
  mutate(layer = 1:ncell(stars_obj))

sf_obj <- st_as_sf(stars_obj)

# mapview
mapview(stars_obj) + 
  mapview(sf_obj)

# tmap
tmap_leaflet(
  tm_shape(stars_obj) +
  tm_raster(alpha = 0.8)
)

This is how mapview::mapview renders it: enter image description here

This is how tmap::tmap_leaflet renders it: tmap

chamaoskurumi
  • 2,271
  • 2
  • 23
  • 30
  • 1
    The rendering of the stars object seems correct. If you decrease the grid cell size to 1000 you will see that also the grid is being rotated and lines up with the sf object much better. As for your question about rasters with arbitrary crs, this is still not possible in any sensible way as far as I know. – TimSalabim Nov 13 '22 at 08:28
  • I tried with a smaller resolution and indeed grid and sf align much better - I think I get now what the discrepancy is: Is it fair to say the max difference between the two is 1/2 of cell width or height @TimSalabim ? – chamaoskurumi Nov 14 '22 at 09:19
  • I'm not sure I understand your question. The two are the same but for the raster we cannot rotate the whole thing so that for large cell sizes the difference between the two becomes more evident. – TimSalabim Nov 15 '22 at 07:31

0 Answers0