[英]how to get the 'mean' point of the polygon's coordinates?
我通过合并形状文件(US Zip 代码)和数据集创建了一个数据框( projects
)。 下面的代码行返回我的数据框的 X 和 Y 坐标。 我想获取多边形坐标(纬度和经度)的“平均”点并将它们添加到projects
数据框中。 我尝试了st_centroid()
但出现错误:
wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, : Loop 0 is not valid: Edge 1616 has duplicate vertex with edge 2124 – 错误
#> head(st_coordinates(projects))
X Y L1 L2 L3
[1,] -71.36374 41.85854 1 1 1
[2,] -71.36449 41.85847 1 1 1
[3,] -71.36473 41.85844 1 1 1
[4,] -71.36580 41.85834 1 1 1
[5,] -71.36573 41.85828 1 1 1
[6,] -71.36562 41.85818 1 1 1
这是关于整个数据集的单个质心还是为每个(多)多边形找到一个质心? 提供的数据似乎暗示前者,因为它仅列出坐标对,但问题是多边形。
使用来自sf
package 的数据集。第一个示例以 sf object 的多面体开始,采用联合并从中找到单个质心:
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
### centroid of multiploygons
head(nc[c("NAME", "geometry")])
#> Simple feature collection with 6 features and 1 field
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
#> Geodetic CRS: NAD27
#> NAME geometry
#> 1 Ashe MULTIPOLYGON (((-81.47276 3...
#> 2 Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3 Surry MULTIPOLYGON (((-80.45634 3...
#> 4 Currituck MULTIPOLYGON (((-76.00897 3...
#> 5 Northampton MULTIPOLYGON (((-77.21767 3...
#> 6 Hertford MULTIPOLYGON (((-76.74506 3...
c_multiploy <- st_union(nc) |> st_centroid()
c_multiploy
#> Geometry set for 1 feature
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -79.40065 ymin: 35.55937 xmax: -79.40065 ymax: 35.55937
#> Geodetic CRS: NAD27
#> POINT (-79.40065 35.55937)
# multiploygons (grey):
plot(nc$geometry, lwd = .15, border = "grey70", main = paste0("c_multiploy = ", c_multiploy))
# union of multiploygons (blue):
plot(st_union(nc), lwd = 2, border = "blue", add =TRUE)
# centroid of union (red):
plot(st_union(nc) |> st_centroid(), pch = 4, cex = 3, lwd = 5, col = "red", add =TRUE)
第二个例子首先生成多边形的质心,所以如果这是你想要的,你可以停在那里。 然后找到一个联合(多点)以获得单个质心。
### centroid of points:
nc_points <- st_centroid(nc)
head(nc_points[c("NAME", "geometry")])
#> Simple feature collection with 6 features and 1 field
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -81.49823 ymin: 36.36142 xmax: -76.02719 ymax: 36.49111
#> Geodetic CRS: NAD27
#> NAME geometry
#> 1 Ashe POINT (-81.49823 36.4314)
#> 2 Alleghany POINT (-81.12513 36.49111)
#> 3 Surry POINT (-80.68573 36.41252)
#> 4 Currituck POINT (-76.02719 36.40714)
#> 5 Northampton POINT (-77.41046 36.42236)
#> 6 Hertford POINT (-76.99472 36.36142)
c_points <- st_union(nc_points) |> st_centroid()
c_points
#> Geometry set for 1 feature
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -79.5111 ymin: 35.64247 xmax: -79.5111 ymax: 35.64247
#> Geodetic CRS: NAD27
#> POINT (-79.5111 35.64247)
# points (grey):
plot(nc_points$geometry, pch = 1, cex = 3, col = "grey70", main = paste0("c_points = ", c_points))
# union of points (blue):
plot(st_union(nc_points), pch = 3, cex = 1, col = "blue", add = TRUE)
# centroid of union (red):
plot(st_union(nc_points) |> st_centroid(), pch = 4, cex = 3, lwd = 5, add =TRUE, col = "red")
创建于 2023-01-21,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.