R:显示多边形几何要素列表

3

我现在正在尝试查找一组多边形的面积、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

2个回答

0
这个怎么样?
l <- lapply(1:length(georgia.polys), function(x) { 
  Complete.List(list(georgia.polys[[x]]))
}
)
df <- do.call(rbind, l)

你需要遍历 "georgia.polys" 的元素。

0

除非你是为了作业或练习而重新发明轮子,否则请使用你手边的工具:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接