我有两个数据帧,它们都包含经度和纬度坐标。第一个数据框是对事件的观察,其中记录了位置和时间。第二个数据框是地理特征,其中记录了关于该特征的位置和信息。基于最短地理距离匹配数据帧
my_df_1 <- structure(list(START_LAT = c(-33.15, -35.6, -34.08333, -34.13333,
-34.31667, -47.38333, -47.53333, -34.08333, -47.38333, -47.15
), START_LONG = c(163, 165.18333, 162.88333, 162.58333, 162.76667,
148.98333, 148.66667, 162.9, 148.98333, 148.71667)), row.names = c(1175L,
528L, 1328L, 870L, 672L, 707L, 506L, 981L, 756L, 210L), class = "data.frame", .Names = c("START_LAT",
"START_LONG"))
my_df_2 <- structure(list(latitude = c(-42.7984, -34.195, -49.81, -35.417,
-28.1487, -44.657, -42.7898, -36.245, -39.1335, -31.8482), longitude = c(179.9874,
179.526, -176.68, 178.765, -168.0314, 174.695, -179.9873, 177.7873,
-170.0583, 173.2424), depth_top = c(935L, 2204L, 869L, 1973L,
4750L, 555L, 894L, 1500L, 4299L, 1303L)), row.names = c(580L,
1306L, 926L, 1102L, 60L, 1481L, 574L, 454L, 1168L, 144L), class = "data.frame", .Names = c("latitude",
"longitude", "depth_top"))
我需要做的是对于df1中的每个观察,我需要找出df2中的哪个特征在地理上最接近。理想情况下,我会得到一个新的列添加到df1,其中每行是距离df2最近的功能。
我通过这个问题How to assign several names to lat-lon observations工作,但无法弄清楚如何匹配到我的数据
真正dataframes有行1000,这就是为什么我不能用手工做这个
不知道'sf'和'st_distance()'。很棒。对于使用'Ubuntu 16.04'阅读此解决方案的其他人,请注意'sf'需要GDAL 2.x。您可以按照[这里]的说明(https://stackoverflow.com/questions/37294127/python-gdal-2-1-installation-on-ubuntu-16-04/41613466#41613466)进行安装。 –
@StevenBeaupré感谢您的意见和笔记。 'st_distance'的文档说,如果提供了未投影的长/长数据,'st_distance'使用'geosphere'软件包中的'distGeo'作为计算距离的默认方法。用户可以在'dist_fun'参数中的'geosphere'中指定其他方法。 – www