我现在正在尝试查找一组多边形的面积、X重心、Y重心和周长。为了测试这个问题,我正在使用GISTools包中的Georgia数据。具体来说,我正在使用159个多边形组成的georgia.polys
列表。
到目前为止,代码如下:
>library(GISTools)
>data(georgia)
对于区域:
>polygon.area <- function(co_ord){
n = dim(co_ord)[1]
sum.of = 0
for(i in 1:(n-1)){
sum.of = sum.of + (co_ord[i,1]*co_ord[i+1,2])-(co_ord[i+1,1]*co_ord[i,2])
}
return((sum.of*0.5)*-1)
}
对于X和Y的质心:
>Cen_x <- function(cen_x_coord){
n = dim(cen_x_coord)[1]
sum_cen_x = 0
for(i in 1:(n-1)){
sum_cen_x = sum_cen_x + ((cen_x_coord[i,1]+cen_x_coord[i+1,1]))*((cen_x_coord[i,1]*cen_x_coord[i+1,2])-(cen_x_coord[i+1,1]*cen_x_coord[i,2]))
}
return((sum_cen_x/(6*polygon.area(cen_x_coord))*-1))
}
>Cen_y <- function(cen_y_coord){
n = dim(cen_y_coord)[1]
sum_cen_y = 0
for(i in 1:(n-1)){
sum_cen_y = sum_cen_y + ((cen_y_coord[i,2]+cen_y_coord[i+1,2]))*((cen_y_coord[i,1]*cen_y_coord[i+1,2])-(cen_y_coord[i+1,1]*cen_y_coord[i,2]))
}
return((sum_cen_y/(6*polygon.area(cen_y_coord))*-1))
}
对于周长:
>Polygon.Perim <- function(perim_coord){
n = dim(perim_coord)[1]
sum.of.perim = 0
for(i in 1:(n-1)){
sum.of.perim = sum.of.perim + sqrt((perim_coord[i+1,1]-perim_coord[i,1])^2+(perim_coord[i+1,2]-perim_coord[i,2])^2)
}
return(sum.of.perim)
}
完整列表:
>Complete.List <- function(Poly.Dataframe){
Actual.DF = data.frame(matrix(ncol = 4, nrow = 0))
colnames(Actual.DF) = c("Polygon Area", "Centroid of X", "Centroid of Y", "Perimeter")
for(i in 1:length(Poly.Dataframe)){
Other.DF = as.data.frame(Poly.Dataframe[i])
a = polygon.area(Other.DF)
x = Cen_x(Other.DF)
y = Cen_y(Other.DF)
p = Polygon.Perim(Other.DF)
Actual.DF[nrow(Actual.DF)+ 1,] = c(a,x,y,p)
}
return(Actual.DF)
}
完成后,我可以对列表中的单个多边形运行以下测试:
>Complete.List(list(georgia.polys[[2]]))
这将返回:
Polygon Area Centroid of X Centroid of Y Perimeter
1 891511462 1240651 999300.7 156744.9
然而,当我尝试获取多边形几何图形的完整列表时,出现了以下错误。到目前为止,我已经尝试了两种方法:
>Complete.List(list(georgia.polys[[1:159]]))
会返回错误信息:
Error in Multiple[[1:159]] : recursive indexing failed at level 2
或者:
Complete.List(list(georgia.polys))
这会返回错误:
Error in data.frame(c(1292287.01256335, 1292653.93730068, 1292949.41616757, :
arguments imply differing number of rows: 125, 99, 53, 124, 116, 57, 88, 48, 69, 160, 107, 26, 163, 151, 287, 190, 136, 77, 93, 227, 37, 56, 30, 256, 180, 96, 32, 47, 68, 73, 64, 59, 92, 67, 87, 115, 108, 117, 43, 14, 50, 54, 91, 44, 89, 58, 205, 133, 111, 71, 72, 150, 97, 138, 75, 25, 105, 74, 95, 17, 22, 119, 155, 60, 94, 109, 42, 76, 221, 176, 106, 143, 126, 82, 24, 51, 85, 100, 128, 62, 40, 187, 35, 218, 86, 83, 114, 132, 208, 34, 65, 27, 123, 189, 171, 165, 113, 121, 137, 102
我有点困惑如何使用相同的最终表格列出所有的多边形。希望这些信息足够了解问题!
谢谢, Jim