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:
Instead, If I do:
library(mapview)
mapview(mylines)
Result: It works perfectly, despite
mapview
using leaflet
underneath. What am I doing wrong in the Leaflet
syntax?
I am used to working with leaflet
providing raster
s, 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.