Reputation: 343
I have a list of 'SpatialPolygonsDataFrame' and I want to receive the intersection of all these polygons. I already found some hints on stackoverflow.com how to solve this problem, nevertheless, I would like to know why I am receiving this ERROR message.
see URL:https://gis.stackexchange.com/questions/140504/extracting-intersection-areas-in-r URL:https://gis.stackexchange.com/questions/156660/loop-to-check-multiple-polygons-overlap-in-r
length_b_loop <- length(as.vector(dat_ftprints))-1
# dat_ftprints is the list of 25 SpatialPolygonsDataFrame
for (b in length_b_loop){
if (b == 1){
spc_intersect_poly1 <- dat_ftprints[[b]]
spc_intersect_poly2 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_poly1, spc_intersect_poly2)
} else {
spc_intersect_poly3 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_temp, spc_intersect_poly3)
}
}
# get a single polygon which represents the intersection of all polygons stored in the list
Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'
Thanks a lot in advance!
Upvotes: 2
Views: 1638
Reputation: 1233
I can't confirm this as you have not provided a repeatable example but it looks like there is an error in your for
loop. You are iterating only over the value 24, not values 1 to 24. This means that spc_intersect_temp was not created correctly and is still whatever the last value you assigned it to. Try this:
length_b_loop <- length(dat_ftprints)-1
for (b in 1:length_b_loop){
if (b == 1){
spc_intersect_poly1 <- dat_ftprints[[b]]
spc_intersect_poly2 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_poly1, spc_intersect_poly2)
} else {
spc_intersect_poly3 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_temp, spc_intersect_poly3)
}
}
EDIT
I would first check your input data by using lapply(dat_ftprints,is)
to make sure that they are all spatial polygon data frames. If they are then the code should work. Example below with polygons constructed from the help page:
##construct polygons
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,2,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,5,2,2)))
Sr3 = Polygon(cbind(c(4,2,5,10,4),c(5,3,2,5,5)))
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3), "s3")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
plot(SpP, col = c(rgb(1,0,0,0.25),rgb(0,1,0,0.25),rgb(0,0,1,0.25)), pbg="white")
##construct list of SPDFs
dat_ftprints=list(SpatialPolygonsDataFrame(SpatialPolygons(list(Polygons(list(Sr1), "s1"))), data = data.frame(x=1,row.names = "s1")),
SpatialPolygonsDataFrame(SpatialPolygons(list(Polygons(list(Sr2), "s2"))), data = data.frame(x=1,row.names = "s2")),
SpatialPolygonsDataFrame(SpatialPolygons(list(Polygons(list(Sr3), "s3"))), data = data.frame(x=1,row.names = "s3")))
##run previous code
length_b_loop <- length(dat_ftprints)-1
for (b in 1:length_b_loop){
if (b == 1){
spc_intersect_poly1 <- dat_ftprints[[b]]
spc_intersect_poly2 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_poly1, spc_intersect_poly2)
} else {
spc_intersect_poly3 <- dat_ftprints[[b+1]]
spc_intersect_temp <- raster::intersect(spc_intersect_temp, spc_intersect_poly3)
}
}
plot(spc_intersect_temp,col="black",add=T)
Upvotes: 1