r - 如何从 R 中的 x 和 y 点计算多边形的面积?
问题描述
我正在寻找从一组 x 和 y 点(下面的示例 1 坐标)计算不同形状的面积。
x_coordinates_1 <- c(786.0, 712.3, 717.7, 804.9, 866.1, 877.5, 866.0, 823.2, 765.5, 791.8, 830.3, 846.9, 937.1, 941.1, 983.2, 1020.5, 997.1, 996.9, 921.5, 921.2, 850.6, 850.6, 786.0)
y_coordinates_1 <- c(139.8, 245.3, 291.7, 335.6, 352.7, 402.4, 492.9, 560.1, 603.6, 631.7, 617.8, 618.1, 538.5, 476.4, 443.0, 338.4, 232.7, 232.7, 145.0, 145.0, 121.0, 120.7, 139.8)
我曾尝试使用 rgeos,但被卡住了。还有另一种方法可以做到这一点吗?
非常感谢
解决方案
您可以使用该sf
包来计算多边形的面积。首先,您必须将 x 和 y 坐标列表转换为多边形,然后计算其面积。
library(sf)
x_coordinates_1<-c(786.0,712.3,717.7,804.9,866.1,877.5,866.0,823.2,765.5,791.8,830.3,846.9,937.1,941.1,983.2,1020.5,997.1,996.9,921.5,921.2,850.6,850.6,786.0)
y_coordinates_1<-c(139.8,245.3,291.7,335.6,352.7,402.4,492.9,560.1,603.6,631.7,617.8,618.1,538.5,476.4,443.0,338.4,232.7,232.7,145.0,145.0,121.0,120.7,139.8)
# Transform your list of points to a polygon, create a simple feature from them and set its crs according to its EPSG code (here I set it to UTM 15 N)
polygon <- st_sfc(st_polygon(list(cbind(x_coordinates_1,y_coordinates_1)))) %>%
st_set_crs(32615)
# Plot the polygon to check if geoemtry is the expected
plot(polygon,axes = TRUE)
# Calculate area of the polygon
st_area(multipoint)
# 78579.21
推荐阅读
- javascript - 为什么如果点击了 onclick,sweetalert 无法显示
- javascript - 产生方波
- c# - 重命名exe后如何保留用户设置
- c++ - 安装使用 Bazel 构建的库
- google-cloud-platform - 问:我怎样才能得到“glossary_id”
- udp - 使用 netcat 和 UDP 从 Wifi 接收数据数组
- c++ - 如何轻松将字符串转换为键盘字符
- python - 张量流中的 Seq2Seq 训练助手模块
- python - 错误:在 Postgres Docker 容器中找不到 pg_config 可执行文件
- django - 如果不退出,则重定向到模型创建