Use apply to intersect all polygons in list A with all polygons in list B in r

StackOverflow https://stackoverflow.com/questions/16093219

  •  04-04-2022
  •  | 
  •  

문제

I have two lists; both of them contain polygons of the class gpc.poly.

The first list is "ptareas":

    > ptareas
    $AG00035
    GPC Polygon
       Num. Contours:  1 
       Num. Vertices:  2000 
       BBox (X):  5.999999 --> 6.000001 
       BBox (Y):  11 --> 11 

    $AG00036
    GPC Polygon
       Num. Contours:  1 
       Num. Vertices:  2000 
       BBox (X):  9.999999 --> 10 
       BBox (Y):  4.999999 --> 5.000001

The second list is "isect_polys":

> isect_polys
[[1]]
GPC Polygon
   Num. Contours:  2 
   BBox (X):  4.928932 --> 25.07107 
   BBox (Y):  -2.071068 --> 25.07107 

[[2]]
GPC Polygon
   Num. Contours:  1 
   Num. Vertices:  1028 
   BBox (X):  4.928932 --> 11.23867 
   BBox (Y):  3.953478 --> 12.02995 

[[3]]
GPC Polygon
   Num. Contours:  2 
   BBox (X):  4.928932 --> 25.07107 
   BBox (Y):  -2.071068 --> 25.07107 

[[4]]
GPC Polygon
   Num. Contours:  1 
   Num. Vertices:  1028 
   BBox (X):  4.928932 --> 11.23867 
   BBox (Y):  3.953478 --> 12.02995 

[[5]]
GPC Polygon
   Num. Contours:  2 
   BBox (X):  4.928932 --> 25.07107 
   BBox (Y):  -2.071068 --> 25.07107 

[[6]]
GPC Polygon
   Num. Contours:  1 
   Num. Vertices:  1028 
   BBox (X):  4.928932 --> 11.23867 
   BBox (Y):  3.953478 --> 12.02995 

What I am trying (and failing) to do is pretty simple. I want to intersect each of the polygons in ptareas with each of the polygons in isect_polys. In practice, the length of each list will vary every time I run the script, but for this example, I'm trying to find a way to automate the following:

intersect(ptareas[[1]],isect_polys[[1]])
intersect(ptareas[[1]],isect_polys[[2]])
intersect(ptareas[[1]],isect_polys[[3]])
intersect(ptareas[[1]],isect_polys[[4]])
intersect(ptareas[[1]],isect_polys[[5]])
intersect(ptareas[[1]],isect_polys[[6]])
intersect(ptareas[[2]],isect_polys[[1]])
intersect(ptareas[[2]],isect_polys[[2]])
intersect(ptareas[[2]],isect_polys[[3]])
intersect(ptareas[[2]],isect_polys[[4]])
intersect(ptareas[[2]],isect_polys[[5]])
intersect(ptareas[[2]],isect_polys[[6]])

If I were dealing with matrices, I'd simply make ptareas a one column matrix, and isect_polys a one row matrix, and then do something akin to the matrix multiplication below:

> m1<- matrix(c(1,2),2,1)
> m1
     [,1]
[1,]    1
[2,]    2
> m2<- matrix(c(1:6),1,6)
> m2
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
> m1 %*% m2
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    2    4    6    8   10   12

Unfortunately, I can't force r to let me treat lists of polygons in the same fashion, so I'm hoping that someone can explain how to achieve the same sort of pairwise interaction using one of the apply functions. As a novice r user, I find these functions difficult to implement when dealing with multiple lists. Since I can find no similar examples in previous posts, others must have been able to solve similar two-list problems with ease, and I am fairly certain that I'm missing something very basic. Any help will be greatly appreciated.

도움이 되었습니까?

해결책

It hard to do this using without a reproducible example. But I guess you want to do an outer intersection of all polygons from the 2 lists.

## First I generate combinations of the supplied
dd <- expand.grid(seq_along(ptareas),seq_along(isect_polys))
## Using mapply to test intersection (1-1,2-2,...
mapply(function(x,y)intersect(ptareas[[x]],isect_polys[[y]]),
       dd$Var1,dd$Var2)

Note side : The licence of the gpclib is amazing, maybe you should use rgeos package.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top