1

When creating a leaflet map from R with providertiles the resulting .htmlfile does show a significant misalignment between the polygon (here country borders) and the basemap. Country borders for Switzerland were taken from here and are projected to EPSG4326. However, the result as shown in the screenshot below (lake Zurich) demonstrates the spatial difference. I am aware of this question and also of this hint but they didn't help me any further.

Here is a reproducible example:

#set working directory as desired----
wd <- setwd()

#download and unzip shapefile from URL----
temp <- tempfile()
download.file("https://geodata.ucdavis.edu/gadm/gadm4.0/shp/gadm40_CHE_shp.zip",temp)
unzip(temp, list=TRUE)
unzip(temp, "gadm40_CHE_0.cpg")
unzip(temp, "gadm40_CHE_0.dbf")
unzip(temp, "gadm40_CHE_0.prj")
unzip(temp, "gadm40_CHE_0.shp")
unzip(temp, "gadm40_CHE_0.shx")
unzip(temp, "gadm40_CHE_1.cpg")
unzip(temp, "gadm40_CHE_1.dbf")
unzip(temp, "gadm40_CHE_1.prj")
unzip(temp, "gadm40_CHE_1.shp")
unzip(temp, "gadm40_CHE_1.shx")
unlink(temp)

#load polygon (shapefile) as SF----
CH0 <-  sf::st_read(wd, layer="gadm40_CHE_0", stringsAsFactors = F)
sf::st_crs(CH0)
CH1 <-  sf::st_read(wd, layer="gadm40_CHE_1", stringsAsFactors = F)
sf::st_crs(CH1)

#leaflet----
library(dplyr) #for pipes
library(leaflet)
map <- leaflet() %>% 
          addProviderTiles(providers$Esri.WorldTopoMap) %>%
          
          addPolygons(data=CH1, color = "red", weight = 2, smoothFactor = 0.5, opacity = 0.75,
                      fillOpacity = 0.25, fillColor = "transparent", label = CH1$NAME_1) %>%
          addPolygons(data=CH0, color = "purple", weight = 2.5, smoothFactor = 0.5, opacity = 0.75,
                      fillOpacity = 0.25, fillColor = "transparent") %>%
          addMarkers(lng = 8.808369980330726, lat = 47.21968417926996) %>% 

          htmlwidgets::onRender("function() { 
                                    var map = this;
                                    var legends = map.controls._controlsById;
                                    function addActualLegend() {
                                       var sel = $('.leaflet-control-layers-base').find('input[type=\"radio\"]:checked').siblings('span').text().trim();
                                       $.each(map.controls._controlsById, (nm) => map.removeControl(map.controls.get(nm)));
                                       map.addControl(legends[sel]);
                                    }
                                    $('.leaflet-control-layers-base').on('click', addActualLegend);
                                    addActualLegend();
                                 }")
map

Screenshot of result

Boombardeiro
  • 105
  • 8
  • It's impossible to answer this without a minimal example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrew Chisholm May 31 '22 at 07:49
  • @AndrewChisholm: understood. I have added a reproducible example in the main text. Thanks for your help! – Boombardeiro May 31 '22 at 11:28
  • 1
    I get the same wrong map if I use mapview in R, QGIS and Google Earth. If I download data for the UK, I can see that maps look correct when compared to OpenStreetMap. It looks to me as though there's something wrong with the Swiss data. – Andrew Chisholm May 31 '22 at 19:23
  • The data basis of the UK data seems to be from raster data, which however, should not make any difference. Also, UK has not so many borders to verify on level 0 or 1. I tested Austria data and they seem to have the same misalignment. I am not very sure if it is really the data itself. – Boombardeiro Jun 01 '22 at 10:56
  • I compared Germany to Switzerland using QGIS with Open Street Map only. I can see that the boundaries of the countries are perfectly aligned with each other. In contrast the interior boundaries are correct for Germany but systematically wrong for Switzerland. I suggest using other data for Switzerland. – Andrew Chisholm Jun 01 '22 at 17:37
  • thx for that observation! greetings – Boombardeiro Jun 03 '22 at 08:13
  • I rechecked the legibility of the downloaded boundaries and i can confirm that the geometries from above do not fit at least to the borders of Switzerland. I would recommend to download needed data from here: – Boombardeiro Jun 06 '22 at 21:00

0 Answers0