When I read a shape file containing polygons or polylines using 0.7-1 or 0.7-0 of sf and the newest version of leaflet (2.0.2) the polygons or lines are not displayed in a leaflet plot, however they are displayed when I'm using plot() or tmap().
Rolling back to version 0.6-3 fixes the issue.
For example:
library(sf)
library(tmap)
#A random example, this will work for any shapefile that I've tried
#http://geoportaal-s-hertogenbosch.opendata.arcgis.com/datasets/a08bb84043a74b91b1243c50e104f337_1.zip
shape <- st_read("Wijken.shp")
tmap_mode("plot")
tm_shape(shape) + tm_polygons() #This will plot the polygons
tmap_mode("view")
tm_shape(shape) + tm_polygons() #This will not
I am not sure this is really an sf issue. The problem is that leaflet (or better javascript in general) doesn't like named objects very much. Reading a shapefile, however, leads to a named sfc_* column. In mapview we therefore make sure to set names of geometries to NULL. Hence, everything works as expected with mapview(shape).
leaflet does not do this, and apparently neither does tmap.
Whether this should be addressed in sf, leaflet or tmap warrants pinging @schloerke, @jcheng5 and @mtennekes to discuss where this should be addressed.
For now what you need to do is
names(st_geometry(shape)) = NULL
then
tmap_mode("plot")
tm_shape(shape) + tm_polygons()
will work as expected.
It has already been fixed in tmap (see https://github.com/mtennekes/tmap/issues/248), so it should work with the github version.
This may also be of interest for @SymbolixAU regarding spatialwidget
@tim-salabim
(or better javascript in general) doesn't like named objects very much
I'm not too clear on what you mean here; do you have an example of a 'named' object in javascript which causes this issue?
See here for a similar issue:
So, this is not an sf issue, but a leaflet one. As pointed out by @tim-salabim, addPolygons fails if the geometry column has names "attached" to it for whatever reason.
Most helpful comment
I am not sure this is really an sf issue. The problem is that leaflet (or better javascript in general) doesn't like named objects very much. Reading a shapefile, however, leads to a named
sfc_*column. In mapview we therefore make sure to set names of geometries toNULL. Hence, everything works as expected withmapview(shape).leaflet does not do this, and apparently neither does tmap.
Whether this should be addressed in sf, leaflet or tmap warrants pinging @schloerke, @jcheng5 and @mtennekes to discuss where this should be addressed.
For now what you need to do is
then
will work as expected.