I have constructed a number of buffered polygons and I would like to calculate the following:
buffered area minus area of polygons intersecting with buffer
In order to better understand my problem, I have attached an image. In the picture you can see the red polygons with a buffer. Within those buffers are other polygons or parts of other polygons. I now need to calculate the area of the other polygons within the buffer of the red polygons. Is it also possible to calculate only parts of intersecting polygons (as some polygons are only partly intersecting with the buffer)?
I am using the packages sf, sp and rgdal and I thought about using the intersect command? But I have absolutely no idea on how to start with this problem as I am a new bee to R in general (otherwise I could have provided you some of my failed codes). Here is some information about my data:
class(polygons)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
as(polygons,"sf")
I've solved my problem through the following code:
data1con<-as(data1, "sf")
geom1<-st_geometry(data1con)
buff1<-st_buffer(geom1,dist=5000)
plot(buff1, border="green")
plot(data1, border="red", add=TRUE)
plot(data2, add=TRUE)
data2con<-as(data2, "sf")
st_is_valid(data2con)
[1] TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
[15] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[29] TRUE TRUE TRUE TRUE
st_is_valid(buff1)
TRUE TRUE TRUE TRUE TRUE TRUE
valid<-st_make_valid(data2con)
intersection<-st_intersection(buff1,valid)
plot(intersection, col="blue", add=TRUE)
area<-st_area(intersection)