R语言 如何得到多边形坐标的“平均”点?

mo49yndu  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(264)

我通过合并一个形状文件(美国邮政编码)和一个数据集创建了一个数据框(projects)。下面的代码行返回了我的数据框的X和Y坐标。我想得到多边形坐标(纬度和经度)的"平均"点,并将它们添加到projects数据框中。我尝试了st_centroid(),但得到了一个错误:
wk_handle. wk_wkb(wkb,s2_geography_writer(oriented =定向,:循环0无效:边1616具有与边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
xxhby3vn

xxhby3vn1#

这是关于整个数据集的单个质心还是每个(多个)多边形的质心?提供的数据似乎暗示了前者,因为它只列出了坐标对,尽管问题指出 * 多边形 *。
使用sf包中的数据集。第一个示例从多多边形的sf对象开始,取并集并从中找到单个质心:

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年1月21日,使用reprex v2.0.2

相关问题