【问题标题】:How do I sort points in clockwise order in R with respect to the center?如何在 R 中相对于中心按顺时针顺序对点进行排序?
【发布时间】:2021-09-21 15:36:37
【问题描述】:

我有一个带有 X 和 Y 坐标的数据集,我正在尝试找出一种方法来从中心在 R 中以顺时针(或逆时针)方向对它们进行排序。假设以经纬度的中位数为中心。

样本数据:

df <- structure(list(name = c("A", "B", "C", "D", "E", "F", "G", "H", 
                              "I", "J"), lat = c(22.57, 22.69, 22.72, 22.5, 22.66, 22.19, 22.6, 
                                                 22.27, 22.31, 22.15), lon = c(88.69, 88.84, 88.77, 88.85, 88.63, 
                                                                               88.91, 88.54, 88.62, 88.78, 88.66)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                        -10L))

我正在寻找的是,上图中的点可以这样排序:G -> E -> A-> C -> B 等等。

到目前为止,我已经尝试使用 arc tan 函数计算极坐标位置,然后对它们进行排序,但这并没有产生好的结果。

我也尝试过orderPoints 功能,但那里的排序也令人怀疑。我得到的顺序是这样的:D-> F-> G-> B-> H-> J-> I-> E-> C-> A。虽然其中一些似乎是有序的,但其他的是看着远方。

【问题讨论】:

  • 中心是什么?
  • 经纬度的中位数。已编辑。

标签: r sorting geospatial spatial


【解决方案1】:

(正如@thelatemail 所指出的那样,答案将取决于哪个点被假定为排列点的中心。下面的方法在中点附近取一个点,选择提供您期望的顺序。实际中点将在 E 之前指示 A。)

df$angle = atan2(df$lat - 22.5,
                 df$lon - 88.68)
library(ggplot2)
ggplot(df[order(df$angle, decreasing = TRUE), ], 
       aes(lon, lat, color = angle, label = name)) +
  geom_path(arrow = arrow(type = "closed", length = unit(0.05, "npc"))) + 
  geom_text(size = 8, color = "black") +
  annotate("point", x = 88.68, y = 22.5, color = "red")

【讨论】:

  • 要获取名称的顺序,则为:df$name[order(atan2(df$lat - cent$lat, df$lon - cent$lon))] where cent &lt;- data.frame(lat = 22.5, lon = 88.68)
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 2018-06-05
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
相关资源
最近更新 更多