简体   繁体   English

R 如何计算具有经度和纬度的几个点之间的距离(以公里为单位)

[英]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_sfcrs=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. rownamescolnames可用于再次附加名称。 The coordinates stay in the same order.坐标保持相同的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何循环列并使用 R 中的经纬度计算距离 - How loop over columns and calculate distance using lat and long in R 如何用R计算参考线(或点)与长/纬点数据框之间的道路网络距离? - How to calculate road network distances between a reference line(or point) and a dataframe of long/lat points with R? 如何使用R计算多个lon lat向量之间的距离 - How to calculate distance between multiple lon lat vectors using R 如何计算带有正负坐标的R中的点之间的距离 - How to calculate distance between points in R with negative and positive coordinate R-如何计算两组坐标点之间的距离? - R - How to calculate distance between two sets of coordinate points? 如何计算R中沿线的两个点之间的地理距离? - How to calculate geographic distance between two points along a line in R? 如何使用R计算点与参考点之间的距离? - How to Calculate Distance between points from a reference point using R? 如何在 R Studio 中计算点与固定位置之间的距离 - How to calculate the distance between points and a fixed location in R Studio 如何使用 leaflet、geosphere 和 R shiny 计算 2 点之间的距离 - How to calculate distance between 2 points with leaflet, geosphere, and R shiny 计算数据框中两个长纬度坐标之间的距离 - Calculate distance between two long lat coordinates in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM