I'm trying to work with individual polygons of a SpatialPolygonsDataFrame (SPDF). I've seen these two questions: Using sapply
on SPDF and a for-loop on a SPDF. The latter I thought may be helpful, but I don't understand the %>%
-syntax and don't know how to apply it to conditional code.
I want to iterate over an SPDF, shrink (gBuffer
) polygons, as long as they don't get smaller than 1 ha (10'000 m²), then maybe apply other steps to those polygons. Something like this:
# get shp file with multiple polygons, creates SpatialPolygonsDataFrame
shpA = shapefile(file.path(PATH_DATA, 'sample_test_A.shp'))
# iterate over shp and work with individual polygons
for (i in 1:length(shpA)) {
# THIS DOESN'T GIVE ME A SPATIAL POLYGON I CAN WORK WITH:
pol_temp = shpA[[1]] # or shpA@polygons[[i]]
# create buffered polygon and check if it is > 1ha
pol_buffered = gBuffer(shpA, width=-30)
if(area(pol_buffered) > 10000){
# replace polygon
shpA[[1]] == pol_buffered
}
# maybe do some other stuff, like sampling the polygon
...
}