Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rgeos::createPolygonsComment Is there some higher-level functionality in sf or terra that we can use for this #9

Closed
rsbivand opened this issue Dec 16, 2022 · 2 comments

Comments

@rsbivand
Copy link
Member

Thanks @rsbivand. With regard to rgeos::createPolygonsComment, it looks to me like we use this for sp Polygons objects whenever they're missing comment data indicating, for each hole, what non-hole they belong to.

createPolygonsComment attempts to create a valid comment for a Polygons object by assessing which polygons contain a given hole (using gContains). Ownership is assigned to the smallest polygon (by area) that contains the given hole.

rstudio/leaflet#833

@rsbivand
Copy link
Member Author

rsbivand commented Dec 16, 2022

From ?rgeos::createPolygonsComment:

library(sp)
p1 <- Polygon(cbind(x=c(0, 0, 10, 10, 0), y=c(0, 10, 10, 0, 0)), hole=FALSE) # I
p2 <- Polygon(cbind(x=c(3, 3, 7, 7, 3), y=c(3, 7, 7, 3, 3)), hole=TRUE) # H
p8 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(1, 2, 2, 1, 1)), hole=TRUE) # H
p9 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(5, 6, 6, 5, 5)), hole=TRUE) # H
p3 <- Polygon(cbind(x=c(20, 20, 30, 30, 20), y=c(20, 30, 30, 20, 20)), hole=FALSE) # I
p4 <- Polygon(cbind(x=c(21, 21, 29, 29, 21), y=c(21, 29, 29, 21, 21)), hole=TRUE) # H
p5 <- Polygon(cbind(x=c(22, 22, 28, 28, 22), y=c(22, 28, 28, 22, 22)), hole=FALSE) # I
p6 <- Polygon(cbind(x=c(23, 23, 27, 27, 23), y=c(23, 27, 27, 23, 23)), hole=TRUE) # H
p7 <- Polygon(cbind(x=c(13, 13, 17, 17, 13), y=c(13, 17, 17, 13, 13)), hole=FALSE) # I
p10 <- Polygon(cbind(x=c(24, 24, 26, 26, 24), y=c(24, 26, 26, 24, 24)), hole=FALSE) # I
p11 <- Polygon(cbind(x=c(24.25, 24.25, 25.75, 25.75, 24.25), y=c(24.25, 25.75, 25.75, 24.25, 24.25)), hole=TRUE) # H
p12 <- Polygon(cbind(x=c(24.5, 24.5, 25.5, 25.5, 24.5), y=c(24.5, 25.5, 25.5, 24.5, 24.5)), hole=FALSE) # I
p13 <- Polygon(cbind(x=c(24.75, 24.75, 25.25, 25.25, 24.75), y=c(24.75, 25.25, 25.25, 24.75, 24.75)), hole=TRUE) # H
	
lp <- list(p1, p2, p13, p7, p6, p5, p4, p3, p8, p11, p12, p9, p10)
#           1   2    3   4   5   6   7   8   9   10   11  12   13
#           0   1   11   0   6   0   8   0   1   13    0   1    0
#           I   H    H   I   H   I   H   I   H    H    I   H    I
spls <- SpatialPolygons(list(Polygons(lp, ID="1")))
comment(spls)
sapply(slot(spls, "polygons"), comment)
length(slot(slot(spls, "polygons")[[1]], "Polygons"))
spls1 <- spls
spls1 = rgeos::createSPComment(spls1)
comment(spls1)
sapply(slot(spls1, "polygons"), comment)
length(slot(slot(spls1, "polygons")[[1]], "Polygons"))
sapply(slot(as(sf::st_as_sfc(spls1), "Spatial"), "polygons"), comment)
length(slot(slot(as(sf::st_as_sfc(spls1), "Spatial"), "polygons")[[1]], "Polygons"))
spls_sfc <- sf::st_as_sfc(spls)
xx <- as(spls_sfc, "Spatial")
comment(xx)
sapply(slot(xx, "polygons"), comment)
length(slot(slot(xx, "polygons")[[1]], "Polygons"))
spls_sfc <- sf::st_as_sfc(spls, forceMulti=TRUE)
xx <- as(spls_sfc, "Spatial")
comment(xx)
sapply(slot(xx, "polygons"), comment)
length(slot(slot(xx, "polygons")[[1]], "Polygons"))

@rsbivand
Copy link
Member Author

rsbivand commented Jan 7, 2023

Need to check against GEOS version, it looks as though 3.9.3 is not sufficient. r-spatial/sf#2069 (comment)

@rsbivand rsbivand closed this as completed Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant