[R-sig-Geo] Combining polygons and calculating their area (i.e. number of cells)

Alexander Zimmermann alexander.zimmermann.ii at uni-potsdam.de
Wed Dec 18 16:08:45 CET 2013


Dear All,

I have a problem with combining polygons and calculating their area.
In the following I try to provide a description of the problem:
I have a simple raster (created with R-package: raster). Using the function
"rasterToPolygons" I get polygons of all raster cells that contain the  
value "1":

library(raster)
dat = list()
dat$x = seq(1.5, by = 10, len = 10)
dat$y = seq(3.5, by = 10, len = 15)
dat$z = matrix(sample(c(0,1), size = 10*15, replace = T), 10, 15)
r=raster(dat);plot(r)
r_poly = rasterToPolygons(r, fun = function(r) {r == 1}, dissolve = F)
plot(r_poly, add = T)

I do not use "dissolve = T" to avoid that all polygons are merged into  
one big polygon (i.e. a multipart polygon). Instead, I wish to obtain  
a new SpatialPolygonsDataFrame in which all polygons that share an  
edge or a point are combined. Polygons that are clearly separated  
should be identifiable as individual polygons (i.e. I wish to avoid to  
obtain a multipart polygon). Based on the new SpatialPolygonsDataFrame  
I would like to analyze the size of the combined (individual) polygons  
as follows:

b = extract(r,r_poly_new) # "r_poly_new" contains the combined polygons
str(b)                    # list of clearly separated polygons
tab = lapply(b,table)
tab

My question is twofold:
1) How to combine polygons that share an edge or point (under the  
constraint that clearly separated polygons remain individual polygons)?
2) How to get this information into a format which allows analyzing  
the areas of the combined (but not multipart) polygons?

Thank you very much for your feedback.

Alex Zimmermann

--------------------------------------------------------------------
Dr. Alexander Zimmermann
Institute of Earth and Environmental Science, University of Potsdam
Karl-Liebknecht-Str. 24-25, 14476 Potsdam - Golm, Germany
phone +49-(0)331-9772047, fax +49-(0)331-9772068



More information about the R-sig-Geo mailing list