26# pragma warning (disable: 4127 4701)
29#include "GeodSolve.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);
47 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
48 DMS::Encode(azi, prec + 5, DMS::NUMBER);
52 bool full,
bool arcmode,
int prec,
bool dms) {
56 s += Utility::str(s12, prec);
60 s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
66 return fraction ? Utility::fract<real>(s) :
67 (arcmode ? DMS::DecodeAngle(s) : Utility::val<real>(s));
70int main(
int argc,
const char*
const argv[]) {
73 enum { NONE = 0, LINE, DIRECT, INVERSE };
74 Utility::set_digits();
75 bool inverse =
false, arcmode =
false,
76 dms =
false, full =
false, exact =
false, unroll =
false,
77 longfirst =
false, azi2back =
false, fraction =
false,
80 a = Constants::WGS84_a(),
81 f = Constants::WGS84_f();
82 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
84 int linecalc = NONE, prec = 3;
85 std::string istring, ifile, ofile, cdelim;
86 char lsep =
';', dmssep = char(0);
88 for (
int m = 1; m < argc; ++m) {
89 std::string arg(argv[m]);
93 }
else if (arg ==
"-a")
97 else if (arg ==
"-L" || arg ==
"-l") {
100 if (m + 3 >= argc)
return usage(1,
true);
102 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
103 lat1, lon1, longfirst);
104 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
106 catch (
const std::exception& e) {
107 std::cerr <<
"Error decoding arguments of -L: " << e.what() <<
"\n";
111 }
else if (arg ==
"-D") {
114 if (m + 4 >= argc)
return usage(1,
true);
116 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
117 lat1, lon1, longfirst);
118 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
120 arcmodeline = arcmode;
122 catch (
const std::exception& e) {
123 std::cerr <<
"Error decoding arguments of -D: " << e.what() <<
"\n";
127 }
else if (arg ==
"-I") {
130 if (m + 4 >= argc)
return usage(1,
true);
132 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
133 lat1, lon1, longfirst);
134 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
135 lat2, lon2, longfirst);
137 catch (
const std::exception& e) {
138 std::cerr <<
"Error decoding arguments of -I: " << e.what() <<
"\n";
142 }
else if (arg ==
"-e") {
143 if (m + 2 >= argc)
return usage(1,
true);
145 a = Utility::val<real>(std::string(argv[m + 1]));
146 f = Utility::fract<real>(std::string(argv[m + 2]));
148 catch (
const std::exception& e) {
149 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
153 }
else if (arg ==
"-u")
155 else if (arg ==
"-d") {
158 }
else if (arg ==
"-:") {
161 }
else if (arg ==
"-w")
162 longfirst = !longfirst;
163 else if (arg ==
"-b")
165 else if (arg ==
"-f")
167 else if (arg ==
"-p") {
168 if (++m == argc)
return usage(1,
true);
170 prec = Utility::val<int>(std::string(argv[m]));
172 catch (
const std::exception&) {
173 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
176 }
else if (arg ==
"-E")
178 else if (arg ==
"--input-string") {
179 if (++m == argc)
return usage(1,
true);
181 }
else if (arg ==
"--input-file") {
182 if (++m == argc)
return usage(1,
true);
184 }
else if (arg ==
"--output-file") {
185 if (++m == argc)
return usage(1,
true);
187 }
else if (arg ==
"--line-separator") {
188 if (++m == argc)
return usage(1,
true);
189 if (std::string(argv[m]).size() != 1) {
190 std::cerr <<
"Line separator must be a single character\n";
194 }
else if (arg ==
"--comment-delimiter") {
195 if (++m == argc)
return usage(1,
true);
197 }
else if (arg ==
"--version") {
198 std::cout << argv[0] <<
": GeographicLib version "
199 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
202 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
205 if (!ifile.empty() && !istring.empty()) {
206 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
209 if (ifile ==
"-") ifile.clear();
210 std::ifstream infile;
211 std::istringstream instring;
212 if (!ifile.empty()) {
213 infile.open(ifile.c_str());
214 if (!infile.is_open()) {
215 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
218 }
else if (!istring.empty()) {
219 std::string::size_type m = 0;
221 m = istring.find(lsep, m);
222 if (m == std::string::npos)
226 instring.str(istring);
228 std::istream* input = !ifile.empty() ? &infile :
229 (!istring.empty() ? &instring : &std::cin);
231 std::ofstream outfile;
232 if (ofile ==
"-") ofile.clear();
233 if (!ofile.empty()) {
234 outfile.open(ofile.c_str());
235 if (!outfile.is_open()) {
236 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
240 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
243 unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
245 outmask |= inverse ? Geodesic::DISTANCE :
246 (arcmode ? Geodesic::NONE : Geodesic::DISTANCE_IN);
248 outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
250 outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
251 Geodesic::GEODESICSCALE | Geodesic::AREA) :
259 if (linecalc == LINE) fraction =
false;
261 le = linecalc == DIRECT ?
262 geode.
GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
263 linecalc == INVERSE ?
264 geode.
InverseLine(lat1, lon1, lat2, lon2, outmask) :
266 geode.
Line(lat1, lon1, azi1, outmask);
268 if (linecalc == INVERSE) azi1 = le.
Azimuth();
270 ls = linecalc == DIRECT ?
271 geods.GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
272 linecalc == INVERSE ?
273 geods.InverseLine(lat1, lon1, lat2, lon2, outmask) :
275 geods.Line(lat1, lon1, azi1, outmask);
277 if (linecalc == INVERSE) azi1 = ls.
Azimuth();
283 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
284 std::string s, eol, slat1, slon1, slat2, slon2, sazi1, ss12, strc;
285 std::istringstream str;
287 while (std::getline(*input, s)) {
290 if (!cdelim.empty()) {
291 std::string::size_type m = s.find(cdelim);
292 if (m != std::string::npos) {
293 eol =
" " + s.substr(m) +
"\n";
297 str.clear(); str.str(s);
299 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
303 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
304 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
306 geode.GenInverse(lat1, lon1, lat2, lon2, outmask,
307 s12, azi1, azi2, m12, M12, M21, S12) :
308 geods.GenInverse(lat1, lon1, lat2, lon2, outmask,
309 s12, azi1, azi2, m12, M12, M21, S12);
313 lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
316 lon1 = Math::AngNormalize(lon1);
317 lon2 = Math::AngNormalize(lon2);
319 *output <<
LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
324 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
330 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
335 *output <<
" " << Utility::str(m12, prec)
336 <<
" " << Utility::str(M12, prec+7)
337 <<
" " << Utility::str(M21, prec+7)
338 <<
" " << Utility::str(S12, std::max(prec-7, 0));
347 s12 =
ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
350 lat2, lon2, azi2, s12, m12, M12, M21, S12) :
352 lat2, lon2, azi2, s12, m12, M12, M21, S12);
354 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
358 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
359 azi1 = DMS::DecodeAzimuth(sazi1);
362 geode.
GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
363 lat2, lon2, azi2, s12, m12, M12, M21, S12) :
364 geods.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
365 lat2, lon2, azi2, s12, m12, M12, M21, S12);
369 <<
LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
370 prec, dms, dmssep, longfirst)
376 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
378 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
383 <<
" " << Utility::str(m12, prec)
384 <<
" " << Utility::str(M12, prec+7)
385 <<
" " << Utility::str(M21, prec+7)
386 <<
" " << Utility::str(S12, std::max(prec-7, 0));
390 catch (
const std::exception& e) {
392 *output <<
"ERROR: " << e.what() <<
"\n";
398 catch (
const std::exception& e) {
399 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
403 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
int main(int argc, const char *const argv[])
real ReadDistance(const std::string &s, bool arcmode, bool fraction=false)
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::GeodesicExact class.
Header for GeographicLib::GeodesicLineExact class.
Header for GeographicLib::GeodesicLine class.
Header for GeographicLib::Geodesic class.
Header for GeographicLib::Utility class.
Exact geodesic calculations.
GeodesicLineExact InverseLine(real lat1, real lon1, real lat2, real lon2, unsigned caps=ALL) const
GeodesicLineExact GenDirectLine(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned caps=ALL) const
Math::real GenDirect(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
GeodesicLineExact Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Math::real Azimuth() const
Math::real GenDistance(bool arcmode) const
Math::real Azimuth() const
Math::real GenDistance(bool arcmode) const
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Exception handling for GeographicLib.
Namespace for GeographicLib.