Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create new polygons by simplifying from two SpatialPolygonsDataFrame objects in R?
    primarykey
    data
    text
    <p>say I have two sets of shape files that cover the same region and often, but not always share borders, e.g. US counties and PUMAs. I'd like to define a new scale of polygon that uses both PUMAs and counties as atomic building blocks, i.e. neither can ever be split, but I'd still like as many units as possible. Here is a toy example:</p> <pre><code>library(sp) # make fake data # 1) counties: Cty &lt;- SpatialPolygons(list( Polygons(list(Polygon(cbind(x=c(0,2,2,1,0,0),y=c(0,0,2,2,1,0)), hole=FALSE)),"county1"), Polygons(list(Polygon(cbind(x=c(2,4,4,3,3,2,2),y=c(0,0,2,2,1,1,0)),hole=FALSE)),"county2"), Polygons(list(Polygon(cbind(x=c(4,5,5,4,4),y=c(0,0,3,2,0)),hole=FALSE)),"county3"), Polygons(list(Polygon(cbind(x=c(0,1,2,2,0,0),y=c(1,2,2,3,3,1)),hole=FALSE)),"county4"), Polygons(list(Polygon(cbind(x=c(2,3,3,4,4,3,3,2,2),y=c(1,1,2,2,3,3,4,4,1)),hole=FALSE)),"county5"), Polygons(list(Polygon(cbind(x=c(0,2,2,1,0,0),y=c(3,3,4,5,5,3)),hole=FALSE)),"county6"), Polygons(list(Polygon(cbind(x=c(1,2,3,4,1),y=c(5,4,4,5,5)),hole=FALSE)),"county7"), Polygons(list(Polygon(cbind(x=c(3,4,4,5,5,4,3,3),y=c(3,3,2,3,5,5,4,3)),hole=FALSE)),"county8") )) counties &lt;- SpatialPolygonsDataFrame(Cty, data = data.frame(ID=paste0("county",1:8), row.names=paste0("county",1:8), stringsAsFactors=FALSE) ) # 2) PUMAs: Pum &lt;- SpatialPolygons(list( Polygons(list(Polygon(cbind(x=c(0,4,4,3,3,2,2,1,0,0),y=c(0,0,2,2,1,1,2,2,1,0)), hole=FALSE)),"puma1"), Polygons(list(Polygon(cbind(x=c(4,5,5,4,3,3,4,4),y=c(0,0,5,5,4,3,3,0)),hole=FALSE)),"puma2"), Polygons(list(Polygon(cbind(x=c(0,1,2,2,3,3,2,0,0),y=c(1,2,2,1,1,2,3,3,1)),hole=FALSE)),"puma3"), Polygons(list(Polygon(cbind(x=c(2,3,4,4,3,3,2,2),y=c(3,2,2,3,3,4,4,3)),hole=FALSE)),"puma4"), Polygons(list(Polygon(cbind(x=c(0,1,1,3,4,0,0),y=c(3,3,4,4,5,5,3)),hole=FALSE)),"puma5"), Polygons(list(Polygon(cbind(x=c(1,2,2,1,1),y=c(3,3,4,4,3)),hole=FALSE)),"puma6") )) Pumas &lt;- SpatialPolygonsDataFrame(Pum, data = data.frame(ID=paste0("puma",1:6), row.names=paste0("puma",1:6), stringsAsFactors=FALSE) ) # desired result: Cclust &lt;- SpatialPolygons(list( Polygons(list(Polygon(cbind(x=c(0,4,4,3,3,2,2,1,0,0),y=c(0,0,2,2,1,1,2,2,1,0)), hole=FALSE)),"ctyclust1"), Polygons(list(Polygon(cbind(x=c(4,5,5,4,3,3,4,4),y=c(0,0,5,5,4,3,3,0)),hole=FALSE)),"ctyclust2"), Polygons(list(Polygon(cbind(x=c(0,1,2,2,3,3,4,4,3,3,2,2,0,0),y=c(1,2,2,1,1,2,2,3,3,4,4,3,3,1)),hole=FALSE)),"ctyclust3"), Polygons(list(Polygon(cbind(x=c(0,2,2,3,4,0,0),y=c(3,3,4,4,5,5,3)),hole=FALSE)),"ctyclust4") )) CtyClusters &lt;- SpatialPolygonsDataFrame(Cclust, data = data.frame(ID = paste0("ctyclust", 1:4), row.names = paste0("ctyclust", 1:4), stringsAsFactors=FALSE) ) # take a look par(mfrow = c(1, 2)) plot(counties, border = gray(.3), lwd = 4) plot(Pumas, add = TRUE, border = "#EEBB00", lty = 2, lwd = 2) legend(-.5, -.3, lty = c(1, 2), lwd = c(4, 2), col = c(gray(.3), "#EEBB00"), legend = c("county line", "puma line"), xpd = TRUE, bty = "n") text(coordinates(counties), counties@data$ID,col = gray(.3)) text(coordinates(Pumas), Pumas@data$ID, col = "#EEBB00",cex=1.5) title("building blocks") #desired result: plot(CtyClusters) title("desired result") text(-.5, -.5, "maximum units possible,\nwithout breaking either PUMAs or counties", xpd = TRUE, pos = 4) </code></pre> <p><img src="https://i.stack.imgur.com/rioSm.png" alt="enter image description here"> I've naively tried many of the g* functions in the rgeos package to achieve this and have not made headway. Does anyone know of a nice function or awesome trick for this task? Thank you!</p> <p>[I'm also open to suggestions on a better title]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload