使用我从之前一个问题的接受答案生成的代码https://gis.stackexchange.com/questions/112905/map-location-of-unknown-point-with-distances-to-two-known-points,我不清楚结果是以什么单位产生的?它们似乎不是以经度/纬度的十进制值表示的,而我的输入是。
创建一个小的示例数据框:
函数
这是我希望一对行的输出以矩阵
输出每次都是一致的,但我不知道该怎么处理它。我原本期望的是以十进制形式的经度/纬度。
创建一个小的示例数据框:
group <- c("A", "B", "C", "D", "E")
distance <- c(709, 2283, 5515, 3035, 4471) ##in km
long <- c(20.5, 29.4, -14.3, 9.85, -5.5)
lat <- c(-19.6, 1.6, 10.9, 2.3, 7.5)
df <- data.frame(group, distance, long, lat)
> df
group distance long lat
1 A 709 20.5 -19.6
2 B 2283 29.4 1.6
3 C 5515 -14.3 10.9
4 D 3035 9.85 2.3
5 E 4471 -5.5 7.5
函数
combn
创建一个矩阵,其中包含所有成对行比较的索引,并且使用FUN
参数,我可以一次使用两个已知点和两个已知距离来确定未知点的位置。library(geosphere) ##load package for distVincentyEllipsoid function
locations <- combn(nrow(df), 2, FUN=\(x) {i <- x[1]; j <- x[2];
P0 <- df[i, c("long", "lat")]
x0 <- df[i, "long"]
y0 <- df[i, "lat"]
r0 <- df[i, "distance"]
P1 <- df[j, c("long", "lat")]
x1 <- df[j, "long"]
y1 <- df[j, "lat"]
r1 <- df[j, "distance"]
d <- abs(distVincentyEllipsoid(
P1,
P0,
a = 6378137,
b = 6356752.3142,
f = 1 / 298.257223563
) / 1000) ##d in km
a <- (r0 ^ 2 - r1 ^ 2 + d ^ 2) / (2 * d)
h <- (r0 ^ 2 - a ^ 2) ^ 2
x2 <- x0 + a * (x1 - x0) / d
y2 <- y0 + a * (y1 - y0) / d
x3.1 <- x2 + h * (y1 - y0) / d
y3.1 <- y2 + h * (x1 - x0) / d
x3.2 <- x2 - h * (y1 - y0) / d
y3.2 <- y2 - h * (x1 - x0) / d
locations <- cbind(x3.1, y3.1, x3.2, y3.2)} ) ##this creates an array
locations <- matrix(locations, ncol=4, byrow=TRUE) ##change the array into a matrix
这是我希望一对行的输出以矩阵
locations
的形式呈现的方式:P0 <- df[df$group == "A", c("long", "lat")]
x0 <- df[df$group == "A", "long"]
y0 <- df[df$group == "A", "lat"]
r0 <- df[df$group == "A", "distance"]
P1 <- df[df$group == "B", c("long", "lat")]
x1 <- df[df$group == "B", "long"]
y1 <- df[df$group == "B", "lat"]
r1 <- df[df$group == "B", "distance"]
d <- abs(distVincentyEllipsoid(P1, P0, a = 6378137, b = 6356752.3142, f = 1 / 298.257223563) / 1000)
a <- (r0 ^ 2 - r1 ^ 2 + d ^ 2) / (2 * d)
h <- (r0 ^ 2 - a ^ 2) ^ 2
x2 <- x0 + a * (x1 - x0) / d
y2 <- y0 + a * (y1 - y0) / d
x3.1 <- x2 + h * (y1 - y0) / d
y3.1 <- y2 + h * (x1 - x0) / d
x3.2 <- x2 - h * (y1 - y0) / d
y3.2 <- y2 - h * (x1 - x0) / d
locations <- cbind(x3.1, y3.1, x3.2, y3.2)
> locations
x3.1 y3.1 x3.2 y3.2
[1,] 1243177033 521899766 -1243176990 -521899800
输出每次都是一致的,但我不知道该怎么处理它。我原本期望的是以十进制形式的经度/纬度。
distVincentyEllipsoid {geosphere}
)中可以得知:"距离值与椭球体相同的单位相同(默认为米)"。因此,所有赋给d
的值都是以米为单位的。 - undefinedmaptools::gzAzimuth()
。 - undefined