使用地圈计算 GPS 点之间的最小距离会给出不准确的值
Calculate minimum distance between GPS points using geosphere gives inaccuate values
我正在尝试计算 R 中一系列 GPS 点的最近邻点之间的平均距离。我找到了两个代码来获取这些值。但它似乎没有给出以米为单位的正确距离。当我对照 Google 地图进行检查时,它已经离开了。
我找到了这个答案:R - Finding closest neighboring point and number of neighbors within a given radius, coordinates lat-long
library(geosphere)
sp.groups <- groups
coordinates(sp.groups) <- ~Long+Lat
class(sp.groups)
d<- distm(sp.groups)
min.d<- apply(d, 1, function(x) order(x, decreasing=F)[2])
min.d
mean(min.d)
groupdist<- cbind(groups, groups[min.d,], apply(d, 1, function(x) order(x, decreasing=F)[2]))
colnames(groupdist)<- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','dist')
此处使用包 rgeos,但结果相同:Calculate the distance between two points of two datasets (nearest neighbor)
library(rgeos)
sp.groups <- groups
coordinates(sp.groups) <- ~Long+Lat
proj4string(sp.groups) <-CRS("+proj=utm +datum=WGS84")
class(sp.groups)
d<- gDistance(sp.groups, byid=TRUE)
min.d<- apply(d, 1, function(x) order(x, decreasing=F)[2])
min.d
mean(min.d)
groupdist<- cbind(groups, groups[min.d,], apply(d, 1, function(x) order(x, decreasing=F)[2]))
colnames(groupdist)<- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','dist')
当我去检查 Google 地球时,距离可能会很远。它甚至为 160-200 米的最近邻居提供了不同的值。还有一些最近的邻居没有相同的距离值,请参见 K11 和 K3,然后是 K3 和 K11。这是我得到的结果,我添加了来自 Google 地图的预期值:
Group Lat Long neighbor n.lat n.long dist GMaps
K1 -26.96538 21.80965 K34 -26.96503 21.80940 27 44
K10 -26.96575 21.81132 K1 -26.96538 21.80965 1 172
K11 -26.96249 21.81120 K3 -26.96387 21.81053 22 166
K24 -26.96033 21.81090 K11 -26.96249 21.81120 3 240
K3 -26.96387 21.81053 K11 -26.96249 21.81120 3 166
K34 -26.96503 21.80940 K1 -26.96538 21.80965 1 44
怎么了?
我的数据
groups<-data.frame(Group = c('K1', 'K10', 'K11', 'K24', 'K3', 'K34'),
Lat = c(-26.96538, -26.96575, -26.96249, -26.96033, -26.96387, -25.96503),
Longitude = c(21.80965, 21.81132, 21.81120, 21.80190, 21.81053, 21.80940))
我不确定你在这里试图计算什么,但是 distance
列只是指距离最小的点的 position/number。我添加了一个带有实际最小距离的数字,看起来像您预期的结果。
library(geodist, include.only = NULL)
library(sp, include.only = NULL)
groups <- data.frame(Group = c('K1', 'K10', 'K11', 'K24', 'K3', 'K34'),
Lat = c(-26.96538, -26.96575, -26.96249, -26.96033, -26.96387, -25.96503),
Long = c(21.80965, 21.81132, 21.81120, 21.80190, 21.81053, 21.80940))
sp.groups <- groups
sp::coordinates(sp.groups) <- ~Long+Lat
# mindistance Matrix
d <- geodist::geodist(groups, measure = "cheap")
# position of minimum distance
diag(d) <- Inf
min.d <- max.col(-d)
min.d
#> [1] 2 1 5 5 3 4
groupdist <- cbind(groups, groups[min.d,], min.d)
colnames(groupdist) <- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','closest_to')
# get minimum distance for each pair of coordinates
groupdist$distance <- d[cbind(seq_along(min.d), min.d)]
groupdist
#> Group Lat Long neighbor n.lat n.long closest_to distance
#> 2 K1 -26.96538 21.80965 K10 -26.96575 21.81132 2 171.1554
#> 1 K10 -26.96575 21.81132 K1 -26.96538 21.80965 1 171.1554
#> 5 K11 -26.96249 21.81120 K3 -26.96387 21.81053 5 167.2225
#> 5.1 K24 -26.96033 21.80190 K3 -26.96387 21.81053 5 944.4119
#> 3 K3 -26.96387 21.81053 K11 -26.96249 21.81120 3 167.2225
#> 4 K34 -25.96503 21.80940 K24 -26.96033 21.80190 4 110613.1362
由 reprex 包 (v2.0.1) 创建于 2021-08-22
我正在尝试计算 R 中一系列 GPS 点的最近邻点之间的平均距离。我找到了两个代码来获取这些值。但它似乎没有给出以米为单位的正确距离。当我对照 Google 地图进行检查时,它已经离开了。
我找到了这个答案:R - Finding closest neighboring point and number of neighbors within a given radius, coordinates lat-long
library(geosphere)
sp.groups <- groups
coordinates(sp.groups) <- ~Long+Lat
class(sp.groups)
d<- distm(sp.groups)
min.d<- apply(d, 1, function(x) order(x, decreasing=F)[2])
min.d
mean(min.d)
groupdist<- cbind(groups, groups[min.d,], apply(d, 1, function(x) order(x, decreasing=F)[2]))
colnames(groupdist)<- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','dist')
此处使用包 rgeos,但结果相同:Calculate the distance between two points of two datasets (nearest neighbor)
library(rgeos)
sp.groups <- groups
coordinates(sp.groups) <- ~Long+Lat
proj4string(sp.groups) <-CRS("+proj=utm +datum=WGS84")
class(sp.groups)
d<- gDistance(sp.groups, byid=TRUE)
min.d<- apply(d, 1, function(x) order(x, decreasing=F)[2])
min.d
mean(min.d)
groupdist<- cbind(groups, groups[min.d,], apply(d, 1, function(x) order(x, decreasing=F)[2]))
colnames(groupdist)<- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','dist')
当我去检查 Google 地球时,距离可能会很远。它甚至为 160-200 米的最近邻居提供了不同的值。还有一些最近的邻居没有相同的距离值,请参见 K11 和 K3,然后是 K3 和 K11。这是我得到的结果,我添加了来自 Google 地图的预期值:
Group Lat Long neighbor n.lat n.long dist GMaps
K1 -26.96538 21.80965 K34 -26.96503 21.80940 27 44
K10 -26.96575 21.81132 K1 -26.96538 21.80965 1 172
K11 -26.96249 21.81120 K3 -26.96387 21.81053 22 166
K24 -26.96033 21.81090 K11 -26.96249 21.81120 3 240
K3 -26.96387 21.81053 K11 -26.96249 21.81120 3 166
K34 -26.96503 21.80940 K1 -26.96538 21.80965 1 44
怎么了?
我的数据
groups<-data.frame(Group = c('K1', 'K10', 'K11', 'K24', 'K3', 'K34'),
Lat = c(-26.96538, -26.96575, -26.96249, -26.96033, -26.96387, -25.96503),
Longitude = c(21.80965, 21.81132, 21.81120, 21.80190, 21.81053, 21.80940))
我不确定你在这里试图计算什么,但是 distance
列只是指距离最小的点的 position/number。我添加了一个带有实际最小距离的数字,看起来像您预期的结果。
library(geodist, include.only = NULL)
library(sp, include.only = NULL)
groups <- data.frame(Group = c('K1', 'K10', 'K11', 'K24', 'K3', 'K34'),
Lat = c(-26.96538, -26.96575, -26.96249, -26.96033, -26.96387, -25.96503),
Long = c(21.80965, 21.81132, 21.81120, 21.80190, 21.81053, 21.80940))
sp.groups <- groups
sp::coordinates(sp.groups) <- ~Long+Lat
# mindistance Matrix
d <- geodist::geodist(groups, measure = "cheap")
# position of minimum distance
diag(d) <- Inf
min.d <- max.col(-d)
min.d
#> [1] 2 1 5 5 3 4
groupdist <- cbind(groups, groups[min.d,], min.d)
colnames(groupdist) <- c(colnames(groups), 'neighbor', 'n.lat', 'n.long','closest_to')
# get minimum distance for each pair of coordinates
groupdist$distance <- d[cbind(seq_along(min.d), min.d)]
groupdist
#> Group Lat Long neighbor n.lat n.long closest_to distance
#> 2 K1 -26.96538 21.80965 K10 -26.96575 21.81132 2 171.1554
#> 1 K10 -26.96575 21.81132 K1 -26.96538 21.80965 1 171.1554
#> 5 K11 -26.96249 21.81120 K3 -26.96387 21.81053 5 167.2225
#> 5.1 K24 -26.96033 21.80190 K3 -26.96387 21.81053 5 944.4119
#> 3 K3 -26.96387 21.81053 K11 -26.96249 21.81120 3 167.2225
#> 4 K34 -25.96503 21.80940 K24 -26.96033 21.80190 4 110613.1362
由 reprex 包 (v2.0.1) 创建于 2021-08-22