簡體   English   中英

如何獲得多邊形坐標的“平均”點?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM