2

First, let's set up the data. Download adjoining Seoul, Incheon, Gyeonggi-do region shapefiles from this website (in Korean). I attach the files, as they are not accessible outside Korea. Then read into R, name, and combine (credit):

library(sf)

#Read, name and combine regions
sca_nsdi <- rbind(cbind(st_read("LARD_ADM_SECT_SGG_11.shp"), Name="Seoul"), 
                  cbind(st_read("LARD_ADM_SECT_SGG_28.shp"), Name="Incheon"), 
                  cbind(st_read("LARD_ADM_SECT_SGG_41.shp"), Name="Gyeonggi-do"))

#Plotting reveals sub-regions (the legend was natively clipped)
plot(sca_nsdi[, "Name"])

enter image description here

I wish to merge the sub-regions of these 3 regions. I have tried applying this solution, but without success:

library(dplyr)

sca_nsdi <- sca_nsdi %>% group_by(Name) %>% summarise(geometry=st_union(geometry)) %>% 
  ungroup()

plot(sca_nsdi[, "Name"])

enter image description here The sub-regions are imperfectly merged. (For some reason, the bounding box is also shrunk to the size of Seoul.)

2
  • Someone more savvy might come up with a full solution, but I usually come across this problem when the polygons are not perfectly aligned to start with (tiny gaps along the boundaries). You could try ms_dissolve from the rmapshaper package with the snap = TRUE argument, which corrects these to some degree. (And use field = "Name" to get the grouping you require.)
    – Sandy AB
    Commented May 13, 2020 at 8:42
  • @SandyAB snap appears to be insufficient in this case. Most sub-regions are not dissolved.
    – syre
    Commented May 13, 2020 at 9:05

1 Answer 1

3

The first solution to dissolving problems is usually to find a set of pre-dissolved borders. Do they exist?

These borders have a lot of digitising errors if they are supposed to represent a division into contiguous areas.

Using QGIS its easy to zoom into your data and see what's happening. Here's an area where three features all overlap and cross with about a width of 20m:

enter image description here

and here's a massive gap of about 170 metres width:

enter image description here

and there are slivers everywhere:

enter image description here

My usual go-to tool for fixing these things is a standalone programme called pprepair https://github.com/tudelft3d/pprepair - there is some work on implementing this within R but I don't think its there yet.

Otherwise the trick is to buffer the polygons so they overlap, then do the union. Given your workflow I would buffer then union the three individual area objects first, then perhaps apply a negative buffer to shrink each one back, and only then combine them into a single object. This way you can tailor the buffer size for each of the three regions.

A quick test shows a 40m buffer works pretty well.

enter image description here

I don't understand why the bounding box is going wrong but I suspect its the grouped object only taking the extent of the first (or last?) group it processes... possible bug..

The pprepair process does a good job of fixing all this once you get the parameters correct. If you try this approach and struggle start a new question and ask.

5
  • st_buffer also expands the outer boundary of the region so is the solution to st_buffer a second time but negatively? Rather than increasing buffer size, is there not a function to clean up the inside of a boundary?
    – syre
    Commented May 15, 2020 at 3:45
  • 1
    Yes, you can do a negative buffer if you think extending your boundary by 20m is going to significantly affect your results. You won't get the exact same boundary back though since negative buffering doesn't precisely reverse positive buffering.
    – Spacedman
    Commented May 15, 2020 at 7:11
  • I don't know of an R function that will clean up boundaries, and I don't know a better standalone cleanup program than pprepair. If anyone does they can add an answer here.
    – Spacedman
    Commented May 15, 2020 at 7:13
  • I have since found another solution to a very similar problem here: stackoverflow.com/questions/53548332/….
    – syre
    Commented May 15, 2020 at 7:42
  • I've given up trying to install pprepair in Windows, but I've achieved reasonably good results with the buffer method. Inclusions remain, which I guess have to be removed manually through QGIS. Technically, the answer is not complete, but I've marked it as the solution anyway (and am very grateful to @Spacedman for even a partial solution).
    – syre
    Commented May 18, 2020 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.