R:显示多边形几何体特征列表
我目前正试图找到多边形列表的面积、X形心、Y形心和周长。为了测试这一点,我使用了GISTools包中的Georgia数据。具体来说,我使用的是由159个多边形组成的R:显示多边形几何体特征列表,r,list,R,List,我目前正试图找到多边形列表的面积、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
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)
}
返回:
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
或:
完成。列表(列表(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
对于如何使用相同的最终表格列出所有多边形,我有点困惑。希望这是足够的信息
谢谢,
吉姆这个怎么样:
l <- lapply(1:length(georgia.polys), function(x) {
Complete.List(list(georgia.polys[[x]]))
}
)
df <- do.call(rbind, l)
l除非你是为了做家庭作业或练习而发明轮子,否则就使用你可以使用的工具:
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
库(sf)
图书馆(GISTools)
图书馆(tidyverse)
数据(格鲁吉亚)
佐治亚州奥普拉斯%
unnest(质心)
})->xdf
xdf
###A tibble:159 x 5
##多边形周长面积xy
##
## 1 206874.1 1326077000 1288960 1056979.7
## 2 156744.9 891511462 1240651 999300.7
## 3 130088.2 740601936 1276765 1033212.9
## 4 185782.8 905136790 1093093 983270.9
## 5 151904.4 692189817 1179548 1190223.5
## 6 103520.6 605358990 1137903 1329516.0
## 7 101619.1 422588345 1123611 1286951.8
## 8 159108.3 1217187029 1017758 1301371.8
## 9 139681.3 657751019 1201770 1045931.2
## 10 178554.7 1184541018 1208139 992435.4
## # ... 还有149行
Error in Multiple[[1:159]] : recursive indexing failed at level 2
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
l <- lapply(1:length(georgia.polys), function(x) {
Complete.List(list(georgia.polys[[x]]))
}
)
df <- do.call(rbind, l)
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