Distance Between Points Command
Moderator: DPlotAdmin
Distance Between Points Command
Do you have a command that can programatically report the distance between two geodectic points, i.e., longitudes and latitudes, in an XY plot?
- DPlotAdmin
- Posts: 2312
- Joined: Tue Jun 24, 2003 9:34 pm
- Location: Vicksburg, Mississippi
- Contact:
No, but there isn't that much to it and you can incorporate this into your own code easily enough. In FORTRAN:
latlontodistance returns the distance between 2 points in miles, given the input latitude and longitude of 2 points in degrees.
Code: Select all
c
c==============================================================================
c
real*8 function r_earth(lat)
implicit none
real*8 lat ! latitude in radians
real*8 a, b
parameter (a=3963.191) ! equatorial radius in miles
parameter (b=3949.903) ! polar radius in miles
real*8 r
r = sqrt( ( (a*a*cos(lat))**2 + (b*b*sin(lat))**2 ) /
1 ( (a*cos(lat))**2 + (b*sin(lat))**2 )
2 )
r_earth = r
return
end
c
c==============================================================================
c
real*8 function latlontodistance(lat1, lon1, lat2, lon2)
implicit none
real*8 lat1, lon1, lat2, lon2 ! latitude and longitude in degrees
real*8 lat1r, lat2r, lon1r, lon2r
real*8 d, pi
real*8 dlon, dlat, a, c
real*8 r_earth
external r_earth
if((lat1 .eq. lat2) .and. (lon1 .eq. lon2)) then
d = 0.0
else
pi = 4.0*atan(1.D0)
lat1r = lat1 * pi/1.8D2
lon1r = lon1 * pi/1.8D2
lat2r = lat2 * pi/1.8D2
lon2r = lon2 * pi/1.8D2
c Haversine formula:
dlon = lon2r - lon1r
dlat = lat2r - lat1r
a = (sin(dlat/2))**2 +
1 (cos(lat1r) * cos(lat2r) * (sin(dlon/2))**2)
c = 2 * atan2(sqrt(a), sqrt(1.D0-a))
d = r_earth(lat1r) * c
endif
latlontodistance = d
return
end
Visualize Your Data
support@dplot.com
support@dplot.com