我想遍历 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