sf::st_combine makes MULTIPOLYGONs with holes

222 views Asked by At

I have some polygons in a data.frame called walktime.

> class(walktime)
[1] "sf"         "data.frame"

I want to combine the polygons to one. As I understand this is possible with

walktime2 <- sf::st_combine(walktime)
> class(walktime2)
[1] "sfc_MULTIPOLYGON" "sfc" 

But if I plot walktime2 I have holes in my combined polygon.

LeafletMap of the layer walktime2 has holes

How can I avoid this?

Thanks for any help.

Reproducable code is here: https://github.com/StatistikVolker/exp_st_combine_holes

1

There are 1 answers

1
Allan Cameron On

You need st_union rather than st_combine.

From the docs for ?st_combine

st_combine combines geometries without resolving borders, using c.sfg (analogous to c for ordinary vectors).

Whereas

If st_union is called with a single argument, x, (with y missing) and by_feature is FALSE all geometries are unioned together

So we can resolve your issue by doing:

walktime2 <- st_union(walktime)

KITAmap3 <- KITAmap %>%
  addPolygons(data = walktime2,
              color = "#00A600",
              weight = 0,
              #opacity = 1.0,
              fillOpacity = 1.0
  )
KITAmap3

enter image description here