【问题标题】:get depth value at coordinate in one dataframe, closest to coordinate in another dataframe在一个数据帧中的坐标处获取深度值,最接近另一个数据帧中的坐标
【发布时间】:2020-10-31 06:09:25
【问题描述】:

我对 R 和空间数据还很陌生,需要一些帮助! 我有一个带有站点坐标(经纬度)的数据框,我需要在另一个数据框的最近坐标处应用深度值。

第一个数据框:

stations = c(stationID, Latitude, Longitude) (16 rows)

第二个数据框:

depths = c(Longitude, latitude, depth) (2943485 rows)

理想情况下,我需要一个新的数据框,例如:

df = c(stationID, Latitude, Longitude, depth) (16 rows)

我很迷茫,所以任何帮助将不胜感激!

【问题讨论】:

标签: r coordinates spatial


【解决方案1】:

你可以使用spatstat 非常有效地做到这一点,如果可以 lat,lon 数据作为平面坐标。所以我们假设你没有越过日期线 (+/-180 度经度)并且 1 度纬度等于 1 度 经度。这仅在赤道附近是正确的。远离赤道的你 应该真正将其中一个坐标乘以适当的因子或 理想情况下,您应该将数据投影到平面坐标系。

随机生成 16 个站点和近 300 万深度的数据 测量下面找到最近邻居的代码不到一半 在我的笔记本电脑上一秒钟。

library(spatstat)
stations <- data.frame(stationID = 1:16, 
                       Latitude = runif(16, 0, 10), 
                       Longitude = runif(16, 0, 10))
Wstations <- bounding.box.xy(stations$Longitude, stations$Latitude)
Xstations <- ppp(stations$Longitude, stations$Latitude, window = Wstations)
N <- 2943485
depths <- data.frame(Longitude = runif(N, 0, 10), 
                     Latitude = runif(N, 0, 10), 
                     depth = runif(N, 0, 1000))
Wdepths <- bounding.box.xy(depths$Longitude, depths$Latitude)
Xdepths <- ppp(depths$Longitude, depths$Latitude, window = Wdepths)
id <- nncross(Xstations, Xdepths, what = "which")
stations$depths <- depths$depth[id]
head(stations)
#>   stationID  Latitude Longitude   depths
#> 1         1 5.7992147 5.6716015 435.1266
#> 2         2 9.2218643 6.0519959 154.5833
#> 3         3 7.6444158 3.2228619 963.7626
#> 4         4 3.8993755 0.9428149 204.9189
#> 5         5 7.9673171 0.9801396 933.5696
#> 6         6 0.9453616 8.0829834 603.0325

【讨论】:

  • 你真是个英雄!它就像一个魅力 - 感谢一百万!
  • 很高兴它有帮助。请记住,它将您的坐标视为普通的 x,y。它对球形地球一无所知......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多