5

I'm trying to make a leaflet map in Lambert 93 (st_transform(2154)) composed of: 1. Polygons (France) 2. Raster Layer (Europe)

I can't understand why the following code is not working.

library(leaflet)
library(tidyverse)
library(sf)
library(grid)
library(fasterize)
library(cartography)
library(mapview)


sf_europe <- cartography::nuts0.spdf %>% st_as_sf  %>% mutate(num=ifelse(id=="FR",1,0)) %>% st_transform(2154) 

r <- raster(sf_europe, res = 10000)
r <- fasterize(sf_europe, r, field="num",background = 2)

epsg_affichage <- leafletCRS(crsClass = 'L.Proj.CRS', code = 'EPSG:2154',
                             proj4def = "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m +no_defs",
                             resolutions = c(65536, 32768, 16384, 8192, 4096, 2048)
)

sf_france_4326 <- sf_europe %>% filter(id=="FR") %>% st_transform(4326)


leaflet(options =
          leafletOptions(maxZoom = 5,
                         crs =  epsg_affichage
          )) %>%
   addGraticule(style= list(color= '#999', weight= 0.5, opacity= 1)) %>%
   addGraticule(sphere = TRUE, style= list(color= '#777', weight= 1, opacity= 0.25)) %>% 
  addFeatures(data=sf_france_4326) %>% 
  addPolygons(data=sf_france_4326, color = "red", fill = NA) %>% 
  addRasterImage(r, project = FALSE)


Here is the sessionInfo

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

Random number generation:
  RNG:     Mersenne-Twister 
Normal:  Inversion 
Sample:  Rounding 

locale:
  [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
  [1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
  [1] mapview_2.7.0     fasterize_1.0.0   cartography_2.2.0 sf_0.7-7         
[5] forcats_0.4.0     stringr_1.4.0     dplyr_0.8.3       purrr_0.3.2      
[9] readr_1.3.1       tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.1    
[13] tidyverse_1.2.1   leaflet_2.0.2    

loaded via a namespace (and not attached):
  [1] Rcpp_1.0.2         lubridate_1.7.4    lattice_0.20-38    png_0.1-7         
[5] class_7.3-15       assertthat_0.2.1   zeallot_0.1.0      digest_0.6.20     
[9] mime_0.7           R6_2.4.0           cellranger_1.1.0   backports_1.1.4   
[13] stats4_3.6.1       e1071_1.7-2        httr_1.4.1         pillar_1.4.2      
[17] rlang_0.4.0        lazyeval_0.2.2     readxl_1.3.1       rstudioapi_0.10   
[21] raster_3.0-2       rgdal_1.4-4        webshot_0.5.1      htmlwidgets_1.3   
[25] munsell_0.5.0      shiny_1.3.2        broom_0.5.2        compiler_3.6.1    
[29] httpuv_1.5.2       modelr_0.1.5       base64enc_0.1-3    pkgconfig_2.0.2   
[33] rgeos_0.5-1        htmltools_0.3.6    tidyselect_0.2.5   codetools_0.2-16  
[37] viridisLite_0.3.0  crayon_1.3.4       withr_2.1.2        later_0.8.0       
[41] satellite_1.0.1    nlme_3.1-140       jsonlite_1.6       xtable_1.8-4      
[45] gtable_0.3.0       lifecycle_0.1.0    DBI_1.0.0          magrittr_1.5      
[49] units_0.6-4        scales_1.0.0       KernSmooth_2.23-15 cli_1.1.0         
[53] stringi_1.4.3      promises_1.0.1     sp_1.3-1           xml2_1.2.2        
[57] generics_0.0.2     vctrs_0.2.0        RColorBrewer_1.1-2 tools_3.6.1       
[61] leafem_0.0.1       glue_1.3.1         hms_0.5.1          crosstalk_1.0.0   
[65] yaml_2.2.0         colorspace_1.4-1   classInt_0.4-1     rvest_0.3.4       
[69] haven_2.1.1 

Indeed, the raster seems to be at the wrong place. Europe is in Australia (see picture) and I really don't know how to fix this problem.

Thanks for your help!

enter image description here

antuki
  • 236
  • 2
  • 9
  • Can you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or share a portion of your data? Plus, what picture you are referring to? – Majid Sep 25 '19 at 00:22
  • Sorry, I forgot to add the picture, it's done now. I also added sessionInfo in the reprex. The portion of my data is in the cartography package so you don't need to download any data before running the example. Tell me if I forgot something again. Thanks. – antuki Sep 25 '19 at 10:20

0 Answers0