简体   繁体   English

计算R中的大圆距离

[英]Computing the Great Circle Distance in R

In the past, in MATLAB, I've calculated the index of a point given its lat and lon using a great circle distance calculation. 在过去,在MATLAB中,我使用大圆距离计算计算了一个点的索引,给出了它的纬度和经度。 I'll share my code with you. 我会和你分享我的代码。 I'm fairly stumped as to what the equivalent function in R would look like or whether on exists? 我对R中的等效函数看起来是什么或是否存在感到相当难过? I've found some code that shows the distance between two points, but none that help me to index my data. 我找到了一些代码来显示两点之间的距离,但没有一个能帮我索引数据。

This is my MATLAB code! 这是我的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);

The "dist" calculation is sort of the party piece here. “dist”计算是这里的派对。 You should be able to use the provided code to reproduce the results and see what I'm hoping to achieve in R. 您应该能够使用提供的代码重现结果,看看我希望在R中实现什么。

Again, what might a comparable function in R be given I have the following: A grid of latitude points A grid of longitude points Two points, in degrees, for the latitude and longitude of my position. 同样,R中可能具有可比较的函数我有以下内容:纬度点网格经度点网格我位置的纬度和经度的两个点(以度为单位)。

maybe this works: 也许这有效:

library(geoshpere)

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM