3

所以我有多个物种范围,如下所示(例如蓝色)这个范围从东到西横跨非洲:

示例物种范围

我可以通过gAreargeos包中使用来获得总面积。我想知道的是这个文件有多少个单独的多边形- 即总范围有多少不同的区域(这可能是岛屿,或者只是分开的人口)以及这些多边形的范围是什么。我一直在使用以下代码:

#Load example shapefile
shp <- readShapeSpatial("species1.shp")

#How many polygon slots are there?
length(shp@polygons)

>2

#How many polygons are in each slot
length(shp@polygons[[1]]@Polygons
length(shp@polygons[[2]]@Polygons

并获得特定区域的面积:

shp@polygons[[1]]@Polygons[[1]]@area

它是否正确?我担心范围中间的一个湖可能会自己构成一个多边形?我想最终得到一个大致如下的列表:

           Species A    Species B
Polygon 1      12          11
Polygon 2      13          10
Polygon 2      14          NA

如果我想为每个物种编译一个列表,如果上面的代码是正确的,那么有多少多边形及其各个范围将非常简单地传递给循环。

谢谢

4

2 回答 2

2

这是一个非常平淡无奇的解决方案,但它现在可以完成工作。

for(i in 1:length(shpfiles)){

shp <- shpfiles[[i]]

#1) Create master list of all polygon files within a shapefile

#How many lists of polygons are there within the shpfile
num.polygon.lists <- length(shp@polygons)


#Get all polygon files
master <- vector(mode="list")
for(i in 1:num.polygon.lists){
m <- shp@polygons[[i]]@Polygons

master[[i]] <- m
}

#Combine polygon files into a single list
len <- length(master)

if(len > 1) {

root <- master[[1]]
for(i in 2:length(master)){
root <- c(master[[i]], root)}

} else {root <- master[[1]]}

#Rename
polygon.files <- root

#2) Count number of polygon files that are not holes

#Create a matrix for the total number of polygon slots that are going to be counted
res <- matrix(NA,ncol=1 , nrow=length(polygon.files))

#Loop through polygons returning whether slot "hole" is TRUE/FALSE
for(i in 1:length(polygon.files)){

r <- isTRUE(polygon.files[[i]]@hole)

res[[i,1]] <- r
}

#Count number times "FALSE" appears - means polygon is not a hole
p.count <- table(res)["FALSE"]
p.count <- as.numeric(p.count)

print(p.count)

}

这是一个开始

于 2013-04-29T18:41:15.640 回答
2

我使用以下代码来找出 shapefile 的每一“行”中有多少个多部分多边形......

sapply(shapefile@polygons,函数(p)长度(p@Polygons))

于 2016-08-26T14:35:11.850 回答