[GIS] Remove sliver gaps between polygons with R

leafletpolygonrsliverstiger

Is there a way to eliminate the small "slivers" between polygons using R? The desired solution would create a new SpatialPolygonsDataFrame where the shared boundaries between polygons are coincident. I'm specifically interested in a solution that uses R, rather than ArcMap or QGIS.

I would also be interested to hear an explanation as to why these gaps exist in the first place.

Here's a reproducible example of the spatial data I'm working with:

library(rgdal)      
library(sp)
library(tigris)
library(magrittr)
library(leaflet)
library(gplots)

# This project will use WGS 84 projected coordinate system
crs_proj <- CRS("+init=epsg:4326") 

# These are the FIPS codes of the specific block groups in my study area
sel <- c("530330079005", "530330079001", "530330079004", 
         "530330085002", "530330085003", "530330086003", 
         "530330087003", "530330085001", "530330090001", 
         "530330091001", "530330091002", "530330092001", 
         "530330092002", "530330086001", "530330090002", 
         "530330086002", "530330079003", "530330079002", 
         "530330087002", "530330087001")

# Create polygons
polygons <- tigris::block_groups(state = "WA",county = "King") %>% 
        .[.@data$GEOID %in% sel,] %>% 
        spTransform(CRSobj = crs_proj)

# Map the result
leaflet() %>% 
        addProviderTiles("CartoDB.Positron") %>% 
        addPolygons(data = polygons,
                    stroke = F,
                    fillColor = col2hex("red"), fillOpacity = 1)

Annoying slivers between the polygons

As you can see in the above screenshot, there are small gaps between the census block group polygons. These location of these gaps is rendered differently depending on the zoom level, but there are always some gaps visible.

Can anyone recommend an R function (or a combination of functions) to programmatically adjust the polygons to eliminate these gaps?

Best Answer

It seems that the solution lies in setting the smoothFactor argument in AddPolygons to 0, as suggested in this related post: Leaflet geojson styling leaves gaps between polygon

I also found it necessary to add a small stroke to the polygons in order to completely remove the sliver gaps from the example map.

leaflet() %>% 
    addProviderTiles("CartoDB.Positron") %>% 
    addPolygons(data = polygons, smoothFactor = 0,
                weight = .75, color = col2hex("red"), opacity = 1,
                fillColor = col2hex("red"), fillOpacity = 1)

enter image description here

Interestingly, when I decreased the opacity of the polygon I found I no longer needed to add the stroke.

leaflet() %>% 
        addProviderTiles("CartoDB.Positron") %>% 
        addPolygons(data = polygons, smoothFactor = 0,
                    stroke = FALSE,
                    fillColor = col2hex("red"), fillOpacity = .5)

50% Opacity

Related Question