if (version_GEOS0() >= "3.9.0") {
x = readWKT(paste("GEOMETRYCOLLECTION(POLYGON((0 0,10 0,10 10,0 10,0 0)),",
"POLYGON((15 0,25 15,35 0,15 0)))"))
# Maximum inscribed circles of both the square and circle independently
c1 = gMaximumInscribedCircle(x, byid=TRUE)
c1_sp <- as(c1, "SpatialPoints") # coercion of straight line segments to points
c1_sp1 <- NULL
if ((length(c1_sp) %% 2) == 0) c1_sp1 <- c1_sp[seq(1, length(c1_sp), 2)]
if (!is.null(c1_sp1)) c1_circ <- gBuffer(c1_sp1, byid=TRUE,
width=gLength(c1, byid=TRUE))
# Maximum inscribed circle of square and circle together, needs gUnaryUnion(),
# inscribes circle in the component permitting the largest circle
c2 = gMaximumInscribedCircle(gUnaryUnion(x))
opar <- par(mfrow=c(2,1))
plot(x)
plot(c1, col='red', add=TRUE, lwd=2)
if (!is.null(c1_sp1)) plot(c1_circ, border="red", add=TRUE)
plot(x)
plot(c2, col='blue', add=TRUE)
par(opar)
}
Run the code above in your browser using DataLab