【发布时间】:2020-07-22 22:38:44
【问题描述】:
我想通过 GPS 坐标的数据框并删除所有彼此靠近的坐标。
pick first row
clalulate the distance between selected and the next row
if the distance is < mindist and current row is not the last row continue to next row
else select the current row (leave it in dataframe) and if the selected row is not the last row
repeat from the begining
结果应该是一个数据帧,其 gps 点至少彼此相距mindist
一种方法是:
node_distances <- function(node_coords)
{
n <- nrow(node_coords)
from <- 1:(n - 1)
to <- 2:n
return(c(0, geodist::geodist_vec(node_coords[from, ]$lon,node_coords[from, ]$lat, node_coords[to, ]$lon, node_coords[to, ]$lat, paired = TRUE, measure = "geodesic")))
}
distances %>% filter(dist < mindist)
但是这种方法只测试 2 行,这意味着它会在文件中产生很大的空白。
我开始编写嵌套循环,但他的决定很糟糕,不起作用而且速度很慢:
node_distances_hack <- function(node_coords)
{
n <- nrow(node_coords)
for(i in 1:n) {
print(node_coords[i,])
a<-i
distance_c<-0
mindist<-50
while(distance_c<mindist || a >= n){
distance_c<-geodist::geodist_vec(node_coords[i,]$lat,node_coords[i,]$lon,node_coords[a,]$lat,node_coords[a,]$lon, paired = TRUE, measure = "cheap")
a<-a+1
}
}
}
什么是更好的方法? 先感谢您, BR
【问题讨论】:
-
这可能最好使用
fuzzyjoin::geo_join来实现。此函数可以根据位置之间的距离连接两个数据帧。这个函数像标准的 dplyr 连接操作一样工作,除了你需要指定点之间的最小距离等。 -
嗨罗伯特,感谢您的回复:它如何在单个数据帧上工作?
-
您可以以此为起点:
tibble::tibble(longitude = c(0, 0,0,10, 10.1), latitude = c(0, 0.1, 10, 10, 10)) %>% fuzzyjoin::geo_left_join(df, max_dist = 10, unit = "km")。这将连接两个数据框,将其限制为相距不到 10 公里的坐标 -
@Robert 我在 UseMethod("tbl_vars") 中发现了一个错误 Fehler:nicht anwendbare Methode für 'tbl_vars' auf Objekt der Klasse "function" angewendet。如果我翻译正确,tibble (tbl_vars) 中的方法不能使用?