[英]R how to calculate the distance in km between several points that have long and lat
I have three points with long and lat(three islands, see matrix bellow) and, I would like to calculate the distance in km between them.我有三个点,经度和纬度(三个岛,见下面的矩阵),我想计算它们之间的距离(以公里为单位)。 Here is my matrix (list1):
这是我的矩阵(list1):
list1 <- data.frame(longitude = c(-3.0000,
-1.3333,
-6.5667),
latitude = c(59.0000,
60.3333,
62.2500),
name= c('Orkney',
'Shetlands',
'Faroe'))
I just want to know the distance between the Faroe Islands and the Shetlands, the distance between the shetlands and Orkney and so on and so on... I am a very beginner in R so I am not familiar with the language... can someone help me?我只想知道法罗群岛和设得兰群岛之间的距离,设得兰群岛和奥克尼群岛之间的距离等等......我是R的初学者所以我对语言不熟悉......可以谁来帮帮我?
What you're looking for is the distance matrix.您正在寻找的是距离矩阵。 It can be done with
geodist
package可以用
geodist
package 来完成
library(geodist)
distance_matrix <- geodist(list1, measure = 'geodesic' )/1000 #converting it to km
#also, check for other measures in the description
colnames(distance_matrix) <- list1$name
rownames(distance_matrix) <- list1$name
Output: Output:
Orkney Shetlands Faroe
Orkney 0.0000 175.7363 411.2688
Shetlands 175.7363 0.0000 352.4388
Faroe 411.2688 352.4388 0.0000
These are two possible solutions:这是两种可能的解决方案:
list1 <- data.frame(longitude = c(-3.0000,
-1.3333,
-6.5667),
latitude = c(59.0000,
60.3333,
62.2500),
name= c('Orkney',
'Shetlands',
'Faroe'))
require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
st_distance(st_as_sf(list1, coords = 1:2, crs=4326))
#> Units: [m]
#> [,1] [,2] [,3]
#> [1,] 0.0 175316.8 410279.5
#> [2,] 175316.8 0.0 351338.2
#> [3,] 410279.5 351338.2 0.0
require(geosphere)
#> Loading required package: geosphere
do.call(rbind,lapply(split(list1[,1:2],1:3), distHaversine, p2=list1[,1:2]))
#> [,1] [,2] [,3]
#> 1 0.0 175512.9 410738.5
#> 2 175512.9 0.0 351731.2
#> 3 410738.5 351731.2 0.0
Created on 2021-09-30 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2021-09-30
Here coords=1:2
indicates the columns that contain the longitude and latitude this can also done by name for more details see ?st_as_sf
, crs=4326
stands for the coordinate reference system used (epsg code: https://epsg.io/4326 ).这里
coords=1:2
表示包含经度和纬度的列,这也可以通过名称完成更多详细信息,请参阅?st_as_sf
, crs=4326
代表使用的坐标参考系统(epsg 代码: https://epsg.io/ 4326 )。 st_distance
automatically attaches units. st_distance
自动附加单位。 If you want different units the units package has example how to convert to different units.如果您想要不同的单位,单位 package 有如何转换为不同单位的示例。
The second example is slightly less fancy and only works for long lat data since it uses distHaversine
(geosphere contains alternative distance functions).第二个例子稍微没那么花哨,只适用于长纬度数据,因为它使用
distHaversine
(geosphere 包含替代距离函数)。 lapply
is use to calculate each time the distance between one and all other coordinates. lapply
用于每次计算一个坐标和所有其他坐标之间的距离。
rownames
and colnames
can be used to attach the names again. rownames
和colnames
可用于再次附加名称。 The coordinates stay in the same order.坐标保持相同的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.