利用R质心函数问题计算蒙大拿州中心

时间:2018-02-01 02:20:28

标签: r geography geographic-distance geosphere

我试图使用 geosphere 包中的 R centroid功能找到蒙大拿州的中心。纬度/经度坐标存储在xx中存储的形状文件中,当计算得到百慕大附近的结果时。

有关如何解决此问题的任何建议吗?

geosphere::centroid(as.matrix(xx))
#           lon      lat
#[1,] -62.60957 28.57984
range(xx$long)
#-116.0492 -104.0391
range(xx$lat)
#44.38032 49.00091

此处xxhttps://pastebin.com/uFgmph9g

供参考 enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您可以通过平均所有点的坐标来计算质心的坐标,如此链接所示(https://gis.stackexchange.com/questions/6025/find-the-centroid-of-a-cluster-of-points)。

cen <- colMeans(xx)
cen
#       long        lat 
# -110.27848   46.91494 

这是一个可视化,以查看质心计算是否有意义。红点是质心。

library(sf)
library(leaflet)
library(mapview)

xx2 <- st_as_sf(xx, coords = c("long", "lat"), crs = 4326)
cen2 <- st_as_sf(as.data.frame(t(cen)), coords = c("long", "lat"), crs = 4326)

mapview(xx2) + mapview(cen2, col.regions = "red")

enter image description here