AF7
AF7

Reputation: 3242

Leaflet Polylines do not show up

I have a very small SpatialLinesDataFrame which I need to plot with Leaflet in R.

Unfortunately, for some reason I'm not able to do so. This is the data (it's 12KB).

I try to do:

library(leaflet)
load("mylines.Rdata")
leaflet() %>% addTiles() %>% addPolylines(data=mylines)

However the resulting map does not make sense, I can only see a line in the top of the screen and this is not what should be plotted. This is the result: enter image description here

Instead, If I do:

library(mapview)
mapview(mylines)

Result: enter image description here It works perfectly, despite mapview using leaflet underneath. What am I doing wrong in the Leaflet syntax?

Upvotes: 0

Views: 1136

Answers (1)

AF7
AF7

Reputation: 3242

I am used to working with leaflet providing rasters, so I usually use the addRasterImage function, which needs data projected over leaflet's display projection (EPSG:3857). However, for polygons and lines, as @TimSalabim correctly pointed out, this is not the case.

So the solution in this case was to just not reproject the data to the leaflet projection beforehand, and provide it in lat-lon coordinates (EPSG:4326).

mapview worked since it does this automagically.

Upvotes: 2

Related Questions