我正在尝试从最初包含在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中指定的坐标不匹配。我已经尝试了索引的所有组合,无法得到我正在寻找的答案!
答案 0 :(得分:0)
你确定吗?它们看起来一样(只是作为“答案”发布,因为它的评论时间太长了):
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