I am trying to merge two polygons (departaments) of Colombia in a single polygon. My goal is to keep only the edges of both polygons but this does not happen. Below I attach the code. Thanks for any help.
download.file("https://github.com/nebulae-co/colmaps/raw/master/data/departamentos.rda","departamentos") load("departamentos") library(sf) library(dplyr) library(rgdal) library(mapview) df1 = subset(x = departamentos, depto == "Bogotá, D. C.") df2 = subset(x = departamentos, depto == "Cundinamarca") df1@data$id <- df2@data$id <- "a" df3 = rbind(df1, df2, makeUniqueIDs = TRUE) plot(df3)
Plot the two polygons
## option 1 df_12 <- sf::st_as_sf(df3) %>% group_by(id) %>% summarise(geometry = sf::st_union(geometry)) %>% ungroup() plot(as_Spatial(df_12)) ## option 2 plot(raster::aggregate(df3)) ## option 3 plot(rgeos::gUnaryUnion(df3)) ## optiom 4 plot(st_union(st_make_valid(st_as_sf(df3)))) ## option 5 plot(st_as_sf(rgeos::gBuffer(as_Spatial(st_make_valid(st_as_sf(df3))), byid=F, width=0)))
没有评论:
发表评论