1
votes

I've been looking for an existing R function(s) for aggregating polygon features within the same layer that share a common boundary (i.e., producing output like 'Dissolve Boundaries' tool in ArcGIS).

I created a polygon layer from a raster file using gdal_polygonizeR (https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/). Some of the polygon shapes are separated by a single raster cell, and are therefore stored as distinct features in the shapefile. I want to combine such polygon features into a single polygon feature, and create a new shapefile (reducing the total number of polygon elements), ideally with a threshold distance for dissolving.

Does anyone know of an existing method for doing this in R?

UPDATE: I think the solution may involve aggregate, followed by disaggregate. I am currently exploring this with particular attention to ensuring polygon features with holes remain associated with the parent polygon (see: Split polygon parts of a single SpatialPolygons Object). Will update again if/when I find a solution.

3

3 Answers

3
votes

After converting a raster file to polygon (using gdal_polygonizeR NOT rasterToPolygon due to runtime issues), I have been able to 'Dissolve Boundaries' of individual polygon features within the same layer ('poly' in code below) by applying the following steps (NB: I have provided example output related to the number of features in the output datasets change after specified functions are run):

library(raster)
library(sp)

#inspect initial polygon output for number of features
poly       #e.g., features: 360

#aggregate initial polygon output into single multi-part features
#this solves some unrealistic feature separation which resulted from
#the raster to polygon conversion
polya = aggregate(poly, dissolve = TRUE)
polya      #e.g., features: 1

#disaggregate multi-part polygon into separate features
polyd <- disaggregate(polya)
polyd      #e.g., features: 228

#apply a negligible buffer distance around polygon features to combine features 
#that are only connected by one raster corner (and therefore counted 
#incorrectly as distinct features after the above steps)
#and dissolve
polyb <- buffer(polyd, width = 0.001, dissolve = TRUE)
polyb      #e.g., features: 1

#disaggregate multi-part buffered polygon into separate features
polybd <- disaggregate(polyb)
polybd     #e.g., features: 181

This method is not perfect as it results in polygon features that are not precisely equal in area to the original raster to polygon output, but it's the best I could come up with so far. Please comment if you have a better solution.

2
votes
library(raster)
y <- aggregate(x)
0
votes

Please see this answer to your question. I believe it is doing what you are looking for. However, I am running into time issue when using the poly2nb function, as I have very large vectors. Hence I am trying your solution.