25# pragma warning (disable: 4127 4701)
28#include "RhumbSolve.usage"
37 latstr = dms ? DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) :
38 DMS::Encode(lat, prec + 5, DMS::NUMBER),
39 lonstr = dms ? DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
40 DMS::Encode(lon, prec + 5, DMS::NUMBER);
42 (longfirst ? lonstr : latstr) +
" " + (longfirst ? latstr : lonstr);
46 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
47 DMS::Encode(azi, prec + 5, DMS::NUMBER);
50int main(
int argc,
const char*
const argv[]) {
52 Utility::set_digits();
53 bool linecalc =
false, inverse =
false, dms =
false, exact =
true,
56 a = Constants::WGS84_a(),
57 f = Constants::WGS84_f();
58 real lat1, lon1, azi12 = Math::NaN(), lat2, lon2, s12, S12;
60 std::string istring, ifile, ofile, cdelim;
61 char lsep =
';', dmssep = char(0);
63 for (
int m = 1; m < argc; ++m) {
64 std::string arg(argv[m]);
68 }
else if (arg ==
"-L" || arg ==
"-l") {
71 if (m + 3 >= argc)
return usage(1,
true);
73 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
74 lat1, lon1, longfirst);
75 azi12 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
77 catch (
const std::exception& e) {
78 std::cerr <<
"Error decoding arguments of -L: " << e.what() <<
"\n";
82 }
else if (arg ==
"-e") {
83 if (m + 2 >= argc)
return usage(1,
true);
85 a = Utility::val<real>(std::string(argv[m + 1]));
86 f = Utility::fract<real>(std::string(argv[m + 2]));
88 catch (
const std::exception& e) {
89 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
94 else if (arg ==
"-d") {
97 }
else if (arg ==
"-:") {
100 }
else if (arg ==
"-w")
101 longfirst = !longfirst;
102 else if (arg ==
"-p") {
103 if (++m == argc)
return usage(1,
true);
105 prec = Utility::val<int>(std::string(argv[m]));
107 catch (
const std::exception&) {
108 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
111 }
else if (arg ==
"-s")
113 else if (arg ==
"--input-string") {
114 if (++m == argc)
return usage(1,
true);
116 }
else if (arg ==
"--input-file") {
117 if (++m == argc)
return usage(1,
true);
119 }
else if (arg ==
"--output-file") {
120 if (++m == argc)
return usage(1,
true);
122 }
else if (arg ==
"--line-separator") {
123 if (++m == argc)
return usage(1,
true);
124 if (std::string(argv[m]).size() != 1) {
125 std::cerr <<
"Line separator must be a single character\n";
129 }
else if (arg ==
"--comment-delimiter") {
130 if (++m == argc)
return usage(1,
true);
132 }
else if (arg ==
"--version") {
133 std::cout << argv[0] <<
": GeographicLib version "
134 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
137 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
140 if (!ifile.empty() && !istring.empty()) {
141 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
144 if (ifile ==
"-") ifile.clear();
145 std::ifstream infile;
146 std::istringstream instring;
147 if (!ifile.empty()) {
148 infile.open(ifile.c_str());
149 if (!infile.is_open()) {
150 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
153 }
else if (!istring.empty()) {
154 std::string::size_type m = 0;
156 m = istring.find(lsep, m);
157 if (m == std::string::npos)
161 instring.str(istring);
163 std::istream* input = !ifile.empty() ? &infile :
164 (!istring.empty() ? &instring : &std::cin);
166 std::ofstream outfile;
167 if (ofile ==
"-") ofile.clear();
168 if (!ofile.empty()) {
169 outfile.open(ofile.c_str());
170 if (!outfile.is_open()) {
171 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
175 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
177 const Rhumb rh(a, f, exact);
179 rh.
Line(0, 0, Math::qd));
182 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
183 std::string s, eol, slat1, slon1, slat2, slon2, sazi, ss12, strc;
184 std::istringstream str;
186 while (std::getline(*input, s)) {
189 if (!cdelim.empty()) {
190 std::string::size_type m = s.find(cdelim);
191 if (m != std::string::npos) {
192 eol =
" " + s.substr(m) +
"\n";
196 str.clear(); str.str(s);
203 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
204 <<
" " << Utility::str(S12, std::max(prec-7, 0)) << eol;
205 }
else if (inverse) {
206 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
210 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
211 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
212 rh.
Inverse(lat1, lon1, lat2, lon2, s12, azi12, S12);
214 << Utility::str(s12, prec) <<
" "
215 << Utility::str(S12, std::max(prec-7, 0)) << eol;
217 if (!(str >> slat1 >> slon1 >> sazi >> s12))
221 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
222 azi12 = DMS::DecodeAzimuth(sazi);
223 rh.
Direct(lat1, lon1, azi12, s12, lat2, lon2, S12);
224 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
225 <<
" " << Utility::str(S12, std::max(prec-7, 0)) << eol;
228 catch (
const std::exception& e) {
230 *output <<
"ERROR: " << e.what() <<
"\n";
236 catch (
const std::exception& e) {
237 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
241 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
int main(int argc, const char *const argv[])
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Header for GeographicLib::Rhumb and GeographicLib::RhumbLine classes.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Find a sequence of points on a single rhumb line.
void Position(real s12, real &lat2, real &lon2, real &S12) const
Solve of the direct and inverse rhumb problems.
RhumbLine Line(real lat1, real lon1, real azi12) const
void Direct(real lat1, real lon1, real azi12, real s12, real &lat2, real &lon2, real &S12) const
void Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi12, real &S12) const
Namespace for GeographicLib.