I am using a shapefile of Brazilian municipalities, so there should be no gap between the polygons. Unfortunately, there are some:

enter image description here

It seems to be fine, but a closer look reveal the problem (note the scale, polygons are separated by around 20m from each other)

enter image description here

This affects the generation of a Spatial Contiguity Matrix, for instance. Polygons 1 and 2 are not considered as neighbors, when they should be.


  • Is there a way to check for other gaps? For instance, plotting the country polygon and checking for "holes" inside of it.

  • Is there a way to correct this for all the polygons? For instance, forcing polygons that are closer then X meters to share the border.

To download the shapefile on R:

cities <- read_municipality(code_muni = "all",  year = 2010)

Polygons 1 and 2 are with "code_mn" 1400233 and 1505304, respectively.

  • for checking gaps you may add all areas; if they fall short of the area of the country there must be other gaps; you may as well use sf::st_difference(country, st_union(municipalities)) and these will give you a layer with all gaps. For closing the gaps you may use st_snap with tolerance = 20 (your 20 meters)
    – Elio Diaz
    Sep 1, 2022 at 17:02
  • 1
    Could buffer solve the problem of closing the gaps as well?
    – Oalvinegro
    Sep 2, 2022 at 13:53
  • The buffer process works when you want to dissolve polygons by group; in this case you want to close these borders, so buffering forth and back shouldn't work; let's figure something out
    – Elio Diaz
    Sep 2, 2022 at 16:02

1 Answer 1


Using mapshaper and selecting on the option of "snap vertices", and then, in console, writing clean without further arguments, you get this st_difference(contry, cleaned_from_mapshaper) respect to the read_country() file, which would only be at the Caroebe region. So programmatically cleaning it would be writing a process for two cases.

I know it's not R; although there's rmapshaper package, but it lacks the clean function. This is the best solution which doesn't imply a lot of coding.


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.