R语言 有没有可能根据彼此之间的距离重新排列GPS点

fcwjkofz  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(124)

bounty还有6天到期。回答此问题可获得+100声望奖励。Andreas希望引起更多关注此问题。

我有一个GPS点的数组,我从开放的街道Map数据。如果我创建了一个跟踪这些点的结果看起来像点的“秩序”

我尝试的一种方法是:使用TSP对这些点进行排序,如下所述:
Order Points with TSP
我做错了什么?也许有更简单/不同的解决方案?
下面是示例数据和我的TSP代码:

library(TSP)

trace <-
  structure(
    list(
      counter = 1:29,
      lon = c(
        11.8296776,
        11.8296602,
        11.8296602,
        11.8296602,
        11.8296673,
        11.8296697,
        11.829711,
        11.8297067,
        11.8296776,
        11.830006,
        11.8299073,
        11.8298583,
        11.8298363,
        11.8297687,
        11.8297067,
        11.8310606,
        11.8310617,
        11.8310268,
        11.8309893,
        11.8309043,
        11.8307988,
        11.8305494,
        11.8302034,
        11.8301046,
        11.830006,
        11.8309893,
        11.8310215,
        11.8310483,
        11.8310606
      ),
      lat = c(
        48.1080396999118,
        48.1082178999118,
        48.1083925999117,
        48.1085890999117,
        48.1087772999116,
        48.1088399999116,
        48.1091400999115,
        48.1077663999119,
        48.1080396999118,
        48.1064633999122,
        48.1067714999121,
        48.1069627999121,
        48.107048999912,
        48.1074419999119,
        48.1077663999119,
        48.1033010999129,
        48.1034692999129,
        48.1037970999128,
        48.1040262999128,
        48.1042792999127,
        48.1045636999126,
        48.1051546999125,
        48.1059033999123,
        48.1061551999123,
        48.1064633999122,
        48.1025808999131,
        48.1027420999131,
        48.103014399913,
        48.1033010999129
      )
    ),
    row.names = c(NA,-29L),
    class = c("data.table", "data.frame"))

xytsp<-ETSP(trace)
xytour <- solve_TSP(xytsp)
reordered_trace <- trace[xytour, ]
reordered_trace$counter<-NULL
reordered_trace<-rowid_to_column(reordered_trace, "counter")
writeGPX(x =as.data.frame(reordered_trace),filename = "reordered_trace",type = "t" )

结果是:

以下是我想要的:

更新:一种似乎很有希望的方法:

trace$counter<-NULL
my.dist <- function(p1 = c(x,y), p2 = c(0,0)) sqrt((p1[1]-p2[1])^2 + (p1[2] - p2[2])^2)
names(trace) <- c("x", "y")
dists.to.origin <- apply(as.data.frame(trace), 1, my.dist)

reordered_trace <- trace[order(dists.to.origin),]
names(reordered_trace) <- c("lon", "lat")
reordered_trace<-rowid_to_column(reordered_trace, "counter")
writeGPX(x =as.data.frame(reordered_trace),filename = "reordered_trace",type = "t" )

此方法有效,但仅适用于短线:

更新:可悲的是,保罗的方法也没有奏效:

jm81lzqq

jm81lzqq1#

好吧,我想我有一个值得一试的东西:
按照示例代码创建数据框,然后:

xytsp <- ETSP(trace)

xytsp <- insert_dummy(as.TSP(xytsp), label = "cut")
# this converts to a TSP
# then adds a 'dummy' node to the chart with 0 distance to all other nodes.  
# This is where we will cut to make it a one-way path.

xytour <- solve_TSP(xytsp, method = "farthest_insertion")
# the farthest_insertion method basically finds the end points of the graph (those with the longest distance between them) then
# works out an efficient way to slot the other nodes into the middle
# it is not foolproof for some shapes, but worth a try.

path <- cut_tour(xytour, "cut")
# cut the tour so it doesn't need a return.

reordered_trace <- trace[labels(path), ]

然后继续按上述操作,根据需要导出/绘制。
请让我知道这是否适用于您的真实的示例。

相关问题