获取一个数据框中坐标的深度值,最接近另一个数据框中的坐标
get depth value at coordinate in one dataframe, closest to coordinate in another dataframe
我对 R 和空间数据还很陌生,需要一些帮助!
我有一个带有站坐标(lat-long)的数据框,我需要在另一个数据框的最近坐标处应用深度值。
第一个数据框:
stations = c(stationID, Latitude, Longitude) (16 rows)
第二个数据框:
depths = c(Longitude, latitude, depth) (2943485 rows)
最理想的是,我需要一个新的数据框,例如:
df = c(stationID, Latitude, Longitude, depth) (16 rows)
我很迷茫,所以任何帮助将不胜感激!
如果可以治疗您的疾病,您可以使用 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
我对 R 和空间数据还很陌生,需要一些帮助! 我有一个带有站坐标(lat-long)的数据框,我需要在另一个数据框的最近坐标处应用深度值。
第一个数据框:
stations = c(stationID, Latitude, Longitude) (16 rows)
第二个数据框:
depths = c(Longitude, latitude, depth) (2943485 rows)
最理想的是,我需要一个新的数据框,例如:
df = c(stationID, Latitude, Longitude, depth) (16 rows)
我很迷茫,所以任何帮助将不胜感激!
如果可以治疗您的疾病,您可以使用 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