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

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Thu Oct 17 13:42:20 CEST 2019


On Thu, 17 Oct 2019, Roger Bivand wrote:

> On Thu, 17 Oct 2019, Marta Rufino wrote:
>
>>  Hi,
>>
>>  I am trying to dissolve the internal borders of larger polygons (sf
>>  object)
>>  by a grouping variable or by proximity (adjacent)
>>  and after looking in the web I found several alternatives, but none is
>>  doing what I wanted.
>>
>>  ## Reproducible example:
>>  # World map
>>  require(rnaturalearth); require(sf)
>>  world_map <- rnaturalearth::ne_countries(scale = 'small', returnclass =
>>  c("sf"))
>>
>>  # World countries:
>>  world_map
>>  object.size(world_map)
>>
>>  # 1st alternative: 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 = 42310) %>%
>>   ggplot()+
>>   geom_sf()
>
> Please state all versions:
>
> sessionInfo()
> sf_extSoftVersion()
>
> With an updated system, most of your code just does not work for me. You are 
> looking for sf::aggregate():
>
> kk <- aggregate(world_map, list(world_map$continent), head, n=1)
>
> plot(st_geometry(kk))
>
> shows that although the country boundaries are largely removed, the 
> underlying data are not properly aligned, so slivers remain, some on 
> continent boundaries, some as holes in land masses.
>
> Contributions welcome to remove the artefacts.

But:

library(rgeos)
wm <- as(world_map, "Spatial")
cs <- gUnaryUnion(wm, id=as.character(wm$continent))
cs_sf <- st_as_sf(cs)
cs_sf$continent <- row.names(cs)
cs_sf
object.size(cs_sf)
plot(cs_sf)

gets much closer.

Maybe try st_precision() for some suitable value? The precision model in 
rgeos multiplies all coordinates by 1e+8 and rounds to integer, reducing 
boundary slivers.

Roger

>
> Using tidyverse really occludes analysis here.
>
> Note that EPSG 42310 simply does not exist in PROJ 6, it is retrievable from:
>
> kk1 <- st_transform(kk, crs = paste0("+proj=merc +lat_ts=0 +lon_0=0",
> " +k=1.000000 +x_0=0 +y_0=0 +ellps=GRS80 +datum=WGS84 +units=m"))
> plot(st_geometry(kk1))
>
> and should never be used for obvious reasons.
>
> kk2 <- st_transform(kk, crs=3857)
> plot(st_geometry(kk2))
>
> is not much better (Web Mercator).
>
> Hope this clarifies,
>
> Roger
>
>>
>>  # 2nd alternative : apparently different, but very similar...
>>  (kk1 <- world_map %>%
>>   dplyr::group_by(continent) %>%
>>   summarise(st_union(.)))
>>  object.size(kk1)
>>  kk1 %>%
>>   st_transform(crs = 42310) %>%
>>   ggplot()+
>>   geom_sf()
>>
>>  # 3rd alternative
>>  (kk2 <- world_map %>%
>>   dplyr::group_by(continent) %>%
>>   summarise(geom = st_union(geometry)))
>>  object.size(kk2)
>>  kk2 %>%
>>   st_transform(crs = 42310) %>%
>>   ggplot()+
>>   geom_sf()
>>
>>  # 4th alternative
>>  (kk3 <- world_map %>%
>>   dplyr::group_by(continent) %>%
>>   summarise(geom = st_combine(geometry)))
>>  object.size(kk3)
>>  kk3 %>%
>>   st_transform(crs = 42310) %>%
>>   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 (as reflected by their
>>  sizes).
>>  object.size(world_map)
>>  object.size(kk1)
>>  object.size(kk2)
>>  object.size(kk3)
>>  object.size(kk4)
>>
>>  # And more importantly, in no case the borders between countries were
>>  actually dissolved, which was my objective.
>> 
>>
>>  So, the question is how can I get the continents with the country borders
>>  dissolved?
>>  Further, could I dissolve all contiguous borders (instead of having a
>>  grouping variable)- I saw this in a couple of posts but the answers were
>>  very complex.
>>
>>  Any help will be greatly appreciated,
>>  Best wishes,
>>  M.
>> 
>> 
>> 
>> 
>
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list