0

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)
N'ya
  • 347
  • 3
  • 13
  • Can you provide a reproducible example with code + data that shows what you've tried and how you've failed? – Dan Mar 27 '19 at 16:22
  • 1
    `maptools::unionSpatialPolygons` ? – G5W Mar 27 '19 at 16:37
  • If you know the polygons are perfect circles you can do this mathematically. What do you want to do if the polygons aren't perfect circles? Is that even a possibility? Are they always convex polygons? What if the overlap area has more than one ring? – Spacedman Mar 28 '19 at 07:49
  • in my case the polygons are perfect circles. in case there are polygons that are not perfect circles, wouldn´t it be possible to identify the intersection points and create the shortest (straight) line between them. 1st problem: the lines would have to be limited to cross the overlapping area only. 2nd problem: if multiple intersections are located at the same area... no solution yet. so iguess you are right, it´s not easy to solve. however i just would like to know if there is an applyable solution for the case of perfect sircles available. – N'ya Mar 28 '19 at 09:03

1 Answers1

2

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)

Spacedman
  • 92,590
  • 12
  • 140
  • 224
Ege Rubak
  • 4,347
  • 1
  • 10
  • 18