Problem with data projection on leaflet

leaflet

#1

Hello, I tried leaflet with shiny. But when I zoom really close on my map, I can see errors with my data. The polygons are not on the right place (on the picture I joined, I added a red arrow where the polygon should be). Does anyone have this problem? Or has a solution?
Biodiversity


#2

Hi @SophieC,

Couple of questions...

  • Do you have a code snippet that I could use to reproduce this error?
  • Are all of the polygons shifted by the same amount?
  • Do the polygon locations match what mapview::addMouseCoordinates() says they are?
library(leaflet)
library(mapview)
# use addMouseCoordinates() to check the coordinate locations of the mouse
leaflet() %>% addTiles() %>% addMouseCoordinates()

Best,
Barret


#3

Possibly the wrong datum is assumed for the polygon layer, or it's not one that leaflet can transform properly. Can you share the proj4string for the layer? Is this an sp object or sf? Use sp::proj4string(x) or sf::st_crs(x) to report the actual proj4string for the polygon layer.

Unfortunately it might be incorrectly set, and there's no way to tell but go back down the chain of data pathways, or it might simply be able to be transformed (by sp or sf) to something leaflet can deal with.


#4

Thanks @barret for your response!

  • This is the code I used:

mesures<- readOGR("data_simplify", "Mesures_compensation")
mesuresGE<-spTransform(mesures, CRS("+init=epsg:4326"))

labelsMesuresGE <- sprintf("%s
%s ",
mesuresGE@data$commune,
mesuresGE@data$Type_Regroup)%>%
lapply(htmltools::HTML)

leaflet()%>%
addTiles() %>%
addProviderTiles(providers$Esri.WorldImagery)%>%
setView(6.12, 46.185, zoom=12) %>%
addPolygons(data= mesuresGE, color = ~palMesuresGE(mesuresGE@data$Type_Regroup),
stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, group= "Genève",
highlight =
highlightOptions(weight = 10,color = "white", dashArray = "",
fillOpacity = 0.7,bringToFront = TRUE),
label = labelsMesuresGE,labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px", direction = "auto"))

  • Yes, all the polygons shifted by the same amount and I've the same problem for all my layers (I've 10 others maps).

  • I tried the map view::addMouseCoordinates() but the polygons locations do not match with the coordinates.

Thanks in advance for your help!
Sophie


#5

Hello @mdsumner ,

  • This is the proj4string for the layer:

mesuresGE@proj4string@projargs
[1] "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

-My layer is an sp object.

Thanks in advance!
Sophie


#6

I don't think I could help without investigating the actual shapefile, but can you also share what is the proj4string before running spTransform?

proj4string(mesures) # is enough

I can only guess that the original metadata is somehow incorrect - if present it will be WKT in a .prj with the .shp, .dbf and others. I'm happy to check the files if you wanted to share them, sadly this kind of problem can only.be fixed painstakingly and has no guarantees.


#7

Hello @mdsumner ,
The proj4string of the original shapefile is:

proj4string(mesures)
[1] "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs"

Is it correct?
Or should I send you the data?
Thanks in advance!


#8

Well the oblique Mercator is definitely my favourite projection family so I'd love to see the data!
This is Swiss Oblique Mercator on the Bessel ellipsoid - there's no way to tell if it's correct at face value, but what I can do is see if it lines up in a a couple of different contexts, just to check. Sometimes in that process you can see what's wrong. Can you zip up all the files and send to my username at gmail? (You'll need to include at least .shp, .shx, .dbf, and .prj but if there are more - files with the same "no-extension" name as the .shp- please include them). If altogether that .zip is bigger than 10Mb we might need another solution, so feel free to email me.


#9

Ok thanks! Could you send me your email ? (I don't find it on your profil).
Thanks in advance!


#10

I finally found a solution: I change the projection directly in ArcGIS with the GCS_WGS_1984 projection! And it worked!


#11

So my guess is ultimately that leaflet can't reproject, or can't reproject with datum transformations. It should work if you used spTransform to convert to longlat WGS84 - specifically +init=epsg:4326.

Glad you solved it!