【发布时间】:2019-08-26 03:27:28
【问题描述】:
我正在尝试使用geosphere 来计算两个大圆之间的交点,这些圆以这样的数据框格式给出:
library(dplyr)
library(geosphere)
df <- data.frame(
# first line
ln1_lonb = 1:4,
ln1_lone = 2:5,
ln1_latb = 10:13,
ln1_late = 11:14,
# second line
ln2_lonb = 5:8,
ln2_lone = 6:9,
ln2_latb = 14:17,
ln2_late = 15:18
)
我尝试使用geosphere 中的gcintersect 函数,该函数将矩阵作为输入。为了在数据框中使用它,我使用了cbind,但似乎 mutate 不适用于此:
df %>%
mutate(
int_points = gcIntersect(
cbind(ln1_lonb, ln1_latb),
cbind(ln1_lone, ln1_late),
cbind(ln2_lonb, ln2_latb),
cbind(ln2_lone, ln2_late)
)
)
>Error: Column `int_points` must be length 4 (the number of rows) or one, not 16
错误可能是由于输出比预期的长(数据帧的行数)。所以我试着把它放在一个列表中:
df %>%
mutate(
int_points = list(gcIntersect(
cbind(ln1_lonb, ln1_latb),
cbind(ln1_lone, ln1_late),
cbind(ln2_lonb, ln2_latb),
cbind(ln2_lone, ln2_late)
))
)
在这里,我再次看到输出是所有组合,而不是获取每行 2 个交点的 4 个坐标。
预期的输出将是新列中单元格中的列表,其中包含两个点的坐标。
是否有不使用循环(或purrr)的解决方案,因为这将比mutate 慢得多。
第一行的 int_points 的值应该和这个的输出一样:
gcIntersect(cbind(1,2), cbind(10,11), cbind(5,6), cbind(14,15))
【问题讨论】:
标签: r spatial intersection geosphere