编写有效的循环以比较GPS坐标 [英] Write an efficient loop to compare gps coordinates

查看:79
本文介绍了编写有效的循环以比较GPS坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历GPS坐标的数据框,并删除所有彼此靠近的坐标。

I want to go through a dataframe of GPS coordinates and remove all coordinates that are to close to each other.

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 彼此

The result should be a dataframe with gps points that are at least mindist away from each other

一个方法是:

 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行

But this aproach only tests 2 rows so that means it creates big gaps in the file.

我开始写嵌套循环,但是他的错误决定不起作用,而且很慢:

I started writig nested loops but his is bad decision that does not work and is slow:

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

What is the better approach? Thank you in advance, BR

推荐答案

您可以使用完全不使用循环来完成此操作 geodist :::: geodist_xy_vec 可以获取每对点之间的距离,因为这会生成一个成对矩阵。考虑以下函数:

You can do this without a loop at all by using geodist:::geodist_xy_vec to get the distances between each pair of points, since this generates a pairwise matrix. Consider this function:

remove_close <- function(df, CLOSE = 10000)
{
  dist_mat <- geodist:::geodist_xy_vec(df$lon, df$lat, df$lon, df$lat, "cheap")
  diag(dist_mat) <- CLOSE + 1
  clashes <- which(dist_mat < CLOSE, arr.ind = TRUE)
  duplicates <- unique(t(apply(clashes, 1, sort)))[, 2]
  df[-duplicates, ]
}

library(ggplot2)

set.seed(69)

df <- data.frame(lat  = runif(1000, 51, 54),
                 lon = runif(1000, 8, 13))

ggplot(df, aes(lon, lat)) + geom_point()


ggplot(remove_close(df), aes(lon, lat)) + geom_point()

reprex软件包(v0.3.0)

Created on 2020-07-22 by the reprex package (v0.3.0)

这篇关于编写有效的循环以比较GPS坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆