Suppose I have a shapefile (lakes.shp) which consists of 5 polygons (Figure 1a).
The shapefile is stored as a SpatialPolygonsDataFrame. The data underlying these polygons can be viewed with:
lakes@data
There is a column called SHAPE_Area, which is the area of each polygon. Now let’s say, we want to create a buffer zone around the lake (Figure 1b).
Great! We can still see the underlying data with
buffer_ring@data
However, the SHAPE_Area shown are the original lake areas. To find the true polygon area, we must dig into the polygon layer: buffer_ring@polygons. But each polygon is an element of a list, and the slot with “area” is deeply nested. Instead of looping through each polygon, here’s an easy way to extract polygon areas:
For the area of only the buffer:
lakeAreas = sapply(slot(lakes, "polygons"), slot, "area") output = data.frame(bufferArea = bufAreas - lakeAreas)