首页 > 解决方案 > 在R中计算大圆距离

问题描述

过去,在 MATLAB 中,我使用大圆距离计算计算了给定其纬度和经度的点的索引。我会和你分享我的代码。我对 R 中的等效函数会是什么样子或者 on 是否存在感到困惑?我找到了一些显示两点之间距离的代码,但没有一个代码可以帮助我索引我的数据。

这是我的 MATLAB 代码!

%% Define latlon grid and coordinates (lon follows lat) 

lon_grid = transpose([40.1 40.12 40.14; 40.3 40.32 40.34; 40.5 40.52 40.54]);
lat_grid = transpose([30 30.2 30.4;30.02 30.22 30.42; 30.04 30.24 30.44]);
coord = [30.4125 40.4043];

%% Compute great circle distance
dist = distance('gc',coord(1),coord(2),lat_grid,lon_grid);

%% Retrieve index of minimum distance
[value,array_index] = min(distance(:));
[i,j] = ind2sub(size(dist),array_index);

“dist”计算是这里的派对作品。您应该能够使用提供的代码来重现结果并查看我希望在 R 中实现的目标。

同样,在 R 中可能会给出一个可比较的函数,我有以下内容: 纬度点网格 经度点网格 两个点,以度为单位,表示我所在位置的纬度和经度。

标签: rmatlablatitude-longitudegreat-circle

解决方案


也许这有效:

library(geoshpere)

dist<-apply(coord, 1, FUN=function(p) distHaversine(p, lonlat_matrix))


推荐阅读