|
Hi Karen,
Working on a project and need to know the distance in miles between two locations. The information that I have in the file to use to do this is the longitude and latitude. I have seen the code in PC jargon, but have not been able to find anything about how to accomplish this in RPG.
[SNIP]
Anyway, here's the code. You should be able to compile it with CRTBNDRPG MYPGM SRCFILE(mylib/QRPGLESRC), and then run it...
H DFTACTGRP(*NO) BNDDIR('QC2LE') D distance PR 8F
D lat1 8F value
D lon1 8F value
D lat2 8F value
D lon2 8F value
D unit 1A const
D deg2rad PR 8F
D deg 8F value
D rad2deg PR 8F
D rad 8F value D pi s 8F inz(3.14159265358979323846)
D result s 20P10
D wait s 1A /free
result = distance(32.9697: -96.80322: 29.46786: -98.53506: 'm');
dsply (%char(result) + ' miles'); result = distance(32.9697: -96.80322: 29.46786: -98.53506: 'k');
dsply (%char(result) + ' kilometers'); result = distance(32.9697: -96.80322: 29.46786: -98.53506: 'n');
dsply (%char(result) + ' nautical miles'); dsply ' ' ' ' wait;
*inlr = *on;
/end-free *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* distance(): Calculate distance between two points
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P distance B export
D distance PI 8F
D lat1 8F value
D lon1 8F value
D lat2 8F value
D lon2 8F value
D unit 1A const D sin PR 8F extproc('sin')
D x 8F value
D cos PR 8F extproc('cos')
D x 8F value
D acos PR 8F extproc('acos')
D x 8F value D distance s 8F
D dist s 8F
D theta s 8F /free
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2))
+ cos(deg2rad(lat1)) * cos(deg2rad(lat2))
* cos(deg2rad(theta)); /if defined(DEBUGING)
dsply ('dist = ' + %editflt(dist));
/endif dist = acos(dist);
dist = rad2deg(dist); /if defined(DEBUGING)
dsply ('dist = ' + %editflt(dist));
/endifdistance = dist * 60 * 1.1515;
select;
when (unit = 'k' or unit = 'K');
distance = distance * 1.609344;
when (unit = 'n' or unit = 'N');
distance = distance * 0.8684;
endsl; return distance;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * deg2rad(): Convert degrees to radians *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P deg2rad B D deg2rad PI 8F D deg 8F value /free return (deg * pi / 180); /end-free P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rad2deg(): Convert radians to degrees *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P rad2deg B D rad2deg PI 8F D rad 8F value /free return (rad * 180 / pi); /end-free P E
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.