Skip to content
Snippets Groups Projects
calculateGeodist.m 1.30 KiB
% GEODIST     calculate the distance and bearing between two positions
%
% SYNOPSIS:
%   function [dist,ang] = geodist(pos1,pos2,unit)
%
% DESCRIPTION:
%   Calculates the distance and bearing between pos1 and pos2. The optional
%   parameter 'unit' specifies if the positions are in degrees ('degree') or
%   in radiant ('rad'). If unit is not given, 'degree' would be the default.
%
%   Goedist returns distance from pos1 to pos2 (dist) and the bearing from
%   pos1 to pos2 (ang) in degrees rel. to north. 
%
%
%  rlat=[pos1(1),pos2(1)]
%  rlon=[pos1(2),pos2(2)]
%
%  calls function ellipk2 to calculate distance and bearing

function [dist,ang] = calculateGeodist(pos1,pos2,unit)

if nargin < 3
  unit='degree';
end

rlat=[pos1(1),pos2(1)];
rlon=[pos1(2),pos2(2)];

if strcmp(unit,'degree')
                    % rad = degree/180*pi !
  rlat = rlat / 180 * pi;
  rlon = rlon / 180 * pi;
end
  
s=size(rlat);
if s(1) > 1
                 % make sure that rlat and rlon have more rows than columns
   if s(1) < s(2)    
      rlat=rlat';
      rlon=rlon';
   end
end
st=[rlat(:,1) rlon(:,1)];
sc=[rlat(:,2) rlon(:,2)];
[dist,ang] = artoa.data.calculateEllipk2(st,sc);

%     dist is the distance from sc to st and ang is the bearing from sc to 
%     st in degrees rel. to north.