从多边形 r 中提取坐标

extracting coordinates from polygon r

我正在尝试从多个多边形中提取坐标,这些多边形最初包含在 SpatialPolygons 对象中:

 Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
 Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
 Srs1 = Polygons(list(Sr1), "s1")
 Srs2 = Polygons(list(Sr2), "s2")
 SpP = SpatialPolygons(list(Srs1,Srs2), 1:2)

我正在尝试从 SpP 对象中提取 Sr1 和 Sr2 的坐标。我在堆栈交换的其他地方看到了这段代码:

Coords<-SpP@polygons[[2]]@Polygons[[1]]@coords

我无法理解方括号中的索引不匹配,但是它运行了。但是输出与我在 Sr1 或 Sr2 中指定的坐标不匹配。我已经尝试了所有索引的组合,但无法得到我要找的答案!

确定吗?它们看起来一样(只作为 "answer" 发帖,因为评论太长了):

library(sp)

Sr1 <- Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 <- Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Srs1 <- Polygons(list(Sr1), "s1")
Srs2 <- Polygons(list(Sr2), "s2")
SpP <- SpatialPolygons(list(Srs1, Srs2), 1:2)

SpP@polygons[[1]]@Polygons[[1]]@coords

##      [,1] [,2]
## [1,]    2    2
## [2,]    1    4
## [3,]    4    5
## [4,]    4    3
## [5,]    2    2

Sr1@coords
##      [,1] [,2]
## [1,]    2    2
## [2,]    4    3
## [3,]    4    5
## [4,]    1    4
## [5,]    2    2


SpP@polygons[[2]]@Polygons[[1]]@coords
##      [,1] [,2]
## [1,]    5    2
## [2,]    2    2
## [3,]    4    3
## [4,]    5    2

Sr2@coords
##      [,1] [,2]
## [1,]    5    2
## [2,]    4    3
## [3,]    2    2
## [4,]    5    2