How to union 2 intersecting polygons receiving 2 single polygons

477 Views Asked by At

How to union intersecting polygons (perfect circles) like in the following picture: enter image description here

So far, I used rgeos and sf, but couldn´t identiy a simple way yet.

library(rgeos)
library(sp)
pts <- SpatialPoints(cbind(c(2,3), c(1,1)))
plot(pts)
pol <- gBuffer(pts, width=0.6, byid=TRUE)
plot(pol)

@ Ege Rubak provided the hint to create a convex hull around the differences of circles. with rgeos the solution looks like foollowing code. However, I struggle receiving the solution in a single step.

gSym1 <- gDifference(pol[1,],pol[2,])
gch1 <- gConvexHull(gSym1)
gSym2 <- gDifference(pol[2,],pol[1,])
gch2 <- gConvexHull(gSym2)
plot(gch1)
plot(gch2, add=TRUE)
1

There are 1 best solutions below

0
On

I must agree with @Spacedman that your question could use a lot more detail about the problem. Below is a quick approach for two circles using spatstat. Packages as sf, sp, etc. surely have the same capabilities.

Two overlapping (polygonal appoximations of) discs in a box:

library(spatstat)
A <- disc()
B <- shift(A, vec = c(1.6,0))
box <- boundingbox(union.owin(A,B))
plot(box, main = "")
B <- shift(A, vec = c(1.6,0))
colA <- rgb(1,0,0,.5)
colB <- rgb(0,1,0,.5)
plot(A, col = colA, add = TRUE, border = colA)
plot(B, col = colB, add = TRUE, border = colB)

Set differences:

AnotB <- setminus.owin(A, B)
BnotA <- setminus.owin(B, A)
plot(box, main = "")
plot(AnotB, col = colA, add = TRUE, border = colA)
plot(BnotA, col = colB, add = TRUE, border = colB)

Convex hulls of set differences:

AA <- convexhull(AnotB)
BB <- convexhull(BnotA)
plot(box, main = "")
plot(AA, col = colA, add = TRUE, border = colA)
plot(BB, col = colB, add = TRUE, border = colB)

If you want to find the intersection points:

edgesA <- edges(A)
edgesB <- edges(B)
x <- crossing.psp(edgesA,edgesB)
plot(box, main = "")
plot(A, col = colA, add = TRUE, border = colA)
plot(B, col = colB, add = TRUE, border = colB)
plot(x, add = TRUE, pch = 20, col = "blue", cex = 3)