我目前正在尝试找到多边形列表的Area,X Centroid,Y Centroid和Perimeter。为了测试这个,我使用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(列表(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
关于如何使用相同的决赛桌列出所有多边形,我有点困惑。希望这是足够的信息!
谢谢, 吉姆
答案 0 :(得分:0)
这个怎么样:
l <- lapply(1:length(georgia.polys), function(x) {
Complete.List(list(georgia.polys[[x]]))
}
)
df <- do.call(rbind, l)
你需要迭代&#34; georgia.polys&#34;的元素。
答案 1 :(得分:0)
除非您重新发明轮子作家庭作业或R-exercise,否则只需使用您可以使用的工具:
library(sf)
library(GISTools)
library(tidyverse)
data(georgia)
georgia_polys <- map(georgia.polys, ~st_polygon(list(.x)))
map_df(georgia_polys, ~{
data_frame(
poly = list(.x),
perimeter = st_length(.x),
area = st_area(.x),
centroid = list(as_data_frame(st_coordinates(st_centroid(.x))))
) %>%
unnest(centroid)
}) -> xdf
xdf
## # A tibble: 159 x 5
## poly perimeter area X Y
## <list> <dbl> <dbl> <dbl> <dbl>
## 1 <S3: XY> 206874.1 1326077000 1288960 1056979.7
## 2 <S3: XY> 156744.9 891511462 1240651 999300.7
## 3 <S3: XY> 130088.2 740601936 1276765 1033212.9
## 4 <S3: XY> 185782.8 905136790 1093093 983270.9
## 5 <S3: XY> 151904.4 692189817 1179548 1190223.5
## 6 <S3: XY> 103520.6 605358990 1137903 1329516.0
## 7 <S3: XY> 101619.1 422588345 1123611 1286951.8
## 8 <S3: XY> 159108.3 1217187029 1017758 1301371.8
## 9 <S3: XY> 139681.3 657751019 1201770 1045931.2
## 10 <S3: XY> 178554.7 1184541018 1208139 992435.4
## # ... with 149 more rows