[R-sig-Geo] dissolve internal borders of polygons using st_union and group_by

Marta Rufino m@rt@@m@ru||no @end|ng |rom gm@||@com
Tue Oct 22 15:07:27 CEST 2019


Hi,

Thank you very much to everybody.
Here it is a summary of all answers obtained (I hope), with the example
objects (one map and one squared set of polygons).

Cheers,
M.

PS: it was done to produce the world distribution of estuaries (
http://rpubs.com/MRufino/541732)

# If using a simple polygon example (
https://cran.r-project.org/web/packages/sf/vignettes/sf3.html):
b0 = st_polygon(list(rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))))
b1 = b0 + 2
b2 = b0 + c(-0.2, 2)
x = st_sfc(b0, b1, b2)
a0 = b0 * 0.8
a1 = a0 * 0.5 + c(2, 0.7)
a2 = a0 + 1
a3 = b0 * 0.5 + c(2, -0.5)
y = st_sfc(a0,a1,a2,a3)
plot(x, border = 'red')
plot(y, border = 'green', add = TRUE)

world_map <- rbind(st_as_sf(x),st_as_sf(y)) %>% st_set_crs(4326)
world_map$continent <- c(rep("a",4), rep("b",3))
world_map
plot(world_map)

# World map (if using real data):
# world_map <- rnaturalearth::ne_countries(scale = 'small', returnclass =
c("sf"))

# World countries:
world_map
object.size(world_map)

# 1st case: in this case we have the groups by continent, but the country
boundaries are still there, although without label:
(kk <- world_map %>%
  dplyr::group_by(continent) %>%
  summarise())
object.size(kk)
kk %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# 2nd case: appeantly different, but very similar...
(kk1 <- world_map %>%
  dplyr::group_by(continent) %>%
  summarise(st_union(.)))
object.size(kk1)
kk1 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# 3rd case (does not work with polygon example)
(kk2 <- world_map %>%
  dplyr::group_by(continent) %>%
  dplyr::summarize(geom = st_union(geometry)))
object.size(kk2)
kk2 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# 4th case  (does not work with polygon example)
(kk3 <- world_map %>%
  dplyr::group_by(continent) %>%
  dplyr::summarize(geom = st_combine(geometry)))
object.size(kk3)
kk3 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# Suggested from Roger Bivand and Barry Rowlingson:
kk4 <- aggregate(st_buffer(world_map, .00000001),
list(world_map$continent), head, n=1)
kk4 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()


# Suggested by Roger Bivand:
library(rgeos)
wm <- as(world_map, "Spatial")
cs <- gUnaryUnion(wm, id=as.character(wm$continent))
kk5 <- st_as_sf(cs)
kk5$continent <- row.names(cs)
kk5
object.size(kk5)
kk5 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# Suggested by Andy Teucher:
library(rmapshaper)

kk6 <- ms_dissolve(world_map, field = "continent")
kk6 %>%
  st_transform(crs = 3857) %>%
  ggplot()+
  geom_sf()

# In all cases the objects produced are actually very similar at a first
glance, bur in fact they differ on the properties and sizes.
# In no case, the borders between countries were actually dissolved, which
was my objective

object.size(world_map)
object.size(kk)
object.size(kk1)
object.size(kk2)
object.size(kk3)
object.size(kk4)
object.size(kk5)
object.size(kk6)

# Now, the challenge is, what is the difference between all those files
that apperantly are the same?

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list