Search code examples
rspatialrgdalr-spr-maptools

Extracting the parent polygon from a nested SpatialPolygonsDataFrame or 'Dissolving' holes from a parent ploygon


EDIT After more research and still no solution, I am adding a substantial edit as well as a link to the .shp file.

The shape file is included here

I have a SpatialPolygonsDataFrame that contains 9 polygons, each of which also contains multiple nested polygons - the 'holes'. The summary of the data are here.

> summary(data)
Object of class SpatialPolygonsDataFrame
Coordinates:
        min       max
x  483298.9  643204.4
y 4782172.1 4997248.3
Is projected: TRUE 
proj4string :
[+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0]
Data attributes:
       Id          IndID  
 Min.   :0   BHS_011_A:1  
 1st Qu.:0   BHS_015_A:1  
 Median :0   BHS_083_A:1  
 Mean   :0   BHS_089_A:1  
 3rd Qu.:0   BHS_091_A:1  
 Max.   :0   BHS_129_A:1  
             (Other)  :3  

A sample of the structure of the data is below.

Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 9 obs. of  2 variables:
  .. ..$ Id   : int [1:9] 0 0 0 0 0 0 0 0 0
  .. ..$ IndID: Factor w/ 9 levels "BHS_011_A","BHS_015_A",..: 1 2 3 4 5 6 7 8 9
  ..@ polygons   :List of 9
  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
  .. .. .. ..@ Polygons :List of 5
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 513497 4986246
  .. .. .. .. .. .. ..@ area   : num 76614017
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:287, 1:2] 509244 507384 507214 507010 506899 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 509678 4979511
  .. .. .. .. .. .. ..@ area   : num 1462398
  .. .. .. .. .. .. ..@ hole   : logi TRUE
  .. .. .. .. .. .. ..@ ringDir: int -1
  .. .. .. .. .. .. ..@ coords : num [1:7, 1:2] 509301 509269 509194 509007 509412 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 515572 4988493
  .. .. .. .. .. .. ..@ area   : num 1579348
  .. .. .. .. .. .. ..@ hole   : logi TRUE
  .. .. .. .. .. .. ..@ ringDir: int -1
  .. .. .. .. .. .. ..@ coords : num [1:10, 1:2] 514520 514570 514684 516501 515996 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"

As seen in the example below (one of nine) the parent Polygon has multiple holes.

data <- readOGR(".", "IndLineBuff")
plot(data[data$IndID == "MTG_005_A",])

enter image description here

This is my first foray into the sp(), rgdal(), rgeos(), and other spatial packages and I have found a number of helpful posts regarding the use of operators to extract area, etc, but nonetheless, questions remain. While this post offers solutions that are close, I cannot seem to adapt the code to fit the needs described herein.

I want to obtain a SpatialPolygonsDataFrame that contains only the parent (biggest) polygon from each set of sublists (i.e. data@polygon). It seems like I should be able to either extract only the parent polygon or 'dissolve' the holes.

The final out come would be 9 polygons, each the parent of the 9 lists, that I could export as an ESRI shapefile.

Any suggestions would be appreciated.


Solution

  • The following hack might work:

    data@polygons = lapply(data@polygons, function(x) { x@Polygons = x@Polygons[1]; x})
    plot(data[data$IndID == "MTG_005_A",])
    

    In your case, it seems you've buffered linear features or so, and want to get rid of the holes in polygons resulting. Each of the 9 polygons seems to consist of the main polygon followed by a set of wholes. The function selects the first of this list, returning the list-of-lists required. To understand the class structure, study ?"SpatialPolygons-class", ?"Polygons-class" and ?"Polygon-class" carefully.