【问题标题】:Finding the index of all nearest elements in a table of (lat,long) coords, for given distance function [duplicate]对于给定的距离函数,在(lat,long)坐标表中查找所有最近元素的索引
【发布时间】:2019-06-10 19:54:45
【问题描述】:

给定一个特定的经纬度点c(lon, lat),以及一个包含经纬度列表的表data,我们想要:

  • 计算该点与数据中所有其他纬度和经度点之间的距离,并
  • 返回表格中距离 c(lon, lat) 1000 米以内的所有点

尝试使用distm函数,但我们只成功计算出c(lon, lat)到一点的距离:

library(spatial)
distm(x, y, fun=distGeo)
distm(data[,3:2], c(40.706914,-74.011322), fun = distHaversine)

【问题讨论】:

  • 您可能正在寻找 which() 函数。例如,要查找数值向量中等于 5 的所有元素,可以编写:which(vec == 5)。但是,如果没有更多信息,很难确切知道您需要什么(例如, distm() 函数来自哪个包?通过答案解决的具体问题是什么?
  • @MrLister:虽然这个更复杂:它正在使用任意 dist 函数在 2D 坐标向量中搜索最近的坐标。 (这不仅仅是在整数/字符串/因子的向量中搜索精确匹配的索引)。
  • 是spatial::distm()、geosphere::distm() 还是哪个包?请完成您的代码示例。

标签: r geospatial distance latitude-longitude spatial-query


【解决方案1】:

您可以使用distHaversine(x,y),其中x 有很多点(在您的示例中为data[,3:2]),y 是单个点。

可重现的例子:

set.seed(123)
y = c(40.706914,-74.011322)
data = data.frame(1:10, rnorm(10, y[1], 0.02), rnorm(10, y[2], 0.02))

which(distHaversine(data[,2:3], y) < 1000)
# [1] 2 4

【讨论】:

  • distHaversine 是矢量化的,然后呢?你可能想提一下
  • 另外,如果需要geosphere 包,请添加缺少的library 行。
猜你喜欢
  • 2013-11-10
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多