GeographicLib 2.1.2
GeodSolve.cpp
Go to the documentation of this file.
1/**
2 * \file GeodSolve.cpp
3 * \brief Command line utility for geodesic calculations
4 *
5 * Copyright (c) Charles Karney (2009-2019) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * See the <a href="GeodSolve.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
20#include <GeographicLib/DMS.hpp>
22
23#if defined(_MSC_VER)
24// Squelch warnings about constant conditional expressions and potentially
25// uninitialized local variables
26# pragma warning (disable: 4127 4701)
27#endif
28
29#include "GeodSolve.usage"
30
32
33std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep,
34 bool longfirst) {
35 using namespace GeographicLib;
36 std::string
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);
41 return
42 (longfirst ? lonstr : latstr) + " " + (longfirst ? latstr : lonstr);
43}
44
45std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
46 using namespace GeographicLib;
47 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
48 DMS::Encode(azi, prec + 5, DMS::NUMBER);
49}
50
51std::string DistanceStrings(real s12, real a12,
52 bool full, bool arcmode, int prec, bool dms) {
53 using namespace GeographicLib;
54 std::string s;
55 if (full || !arcmode)
56 s += Utility::str(s12, prec);
57 if (full)
58 s += " ";
59 if (full || arcmode)
60 s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
61 return s;
62}
63
64real ReadDistance(const std::string& s, bool arcmode, bool fraction = false) {
65 using namespace GeographicLib;
66 return fraction ? Utility::fract<real>(s) :
67 (arcmode ? DMS::DecodeAngle(s) : Utility::val<real>(s));
68}
69
70int main(int argc, const char* const argv[]) {
71 try {
72 using namespace GeographicLib;
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,
78 arcmodeline = false;
79 real
80 a = Constants::WGS84_a(),
81 f = Constants::WGS84_f();
82 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
83 mult = 1;
84 int linecalc = NONE, prec = 3;
85 std::string istring, ifile, ofile, cdelim;
86 char lsep = ';', dmssep = char(0);
87
88 for (int m = 1; m < argc; ++m) {
89 std::string arg(argv[m]);
90 if (arg == "-i") {
91 inverse = true;
92 linecalc = NONE;
93 } else if (arg == "-a")
94 arcmode = !arcmode;
95 else if (arg == "-F")
96 fraction = true;
97 else if (arg == "-L" || arg == "-l") { // -l is DEPRECATED
98 inverse = false;
99 linecalc = LINE;
100 if (m + 3 >= argc) return usage(1, true);
101 try {
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]));
105 }
106 catch (const std::exception& e) {
107 std::cerr << "Error decoding arguments of -L: " << e.what() << "\n";
108 return 1;
109 }
110 m += 3;
111 } else if (arg == "-D") {
112 inverse = false;
113 linecalc = DIRECT;
114 if (m + 4 >= argc) return usage(1, true);
115 try {
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]));
119 s12 = ReadDistance(std::string(argv[m + 4]), arcmode);
120 arcmodeline = arcmode;
121 }
122 catch (const std::exception& e) {
123 std::cerr << "Error decoding arguments of -D: " << e.what() << "\n";
124 return 1;
125 }
126 m += 4;
127 } else if (arg == "-I") {
128 inverse = false;
129 linecalc = INVERSE;
130 if (m + 4 >= argc) return usage(1, true);
131 try {
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);
136 }
137 catch (const std::exception& e) {
138 std::cerr << "Error decoding arguments of -I: " << e.what() << "\n";
139 return 1;
140 }
141 m += 4;
142 } else if (arg == "-e") {
143 if (m + 2 >= argc) return usage(1, true);
144 try {
145 a = Utility::val<real>(std::string(argv[m + 1]));
146 f = Utility::fract<real>(std::string(argv[m + 2]));
147 }
148 catch (const std::exception& e) {
149 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
150 return 1;
151 }
152 m += 2;
153 } else if (arg == "-u")
154 unroll = true;
155 else if (arg == "-d") {
156 dms = true;
157 dmssep = '\0';
158 } else if (arg == "-:") {
159 dms = true;
160 dmssep = ':';
161 } else if (arg == "-w")
162 longfirst = !longfirst;
163 else if (arg == "-b")
164 azi2back = true;
165 else if (arg == "-f")
166 full = true;
167 else if (arg == "-p") {
168 if (++m == argc) return usage(1, true);
169 try {
170 prec = Utility::val<int>(std::string(argv[m]));
171 }
172 catch (const std::exception&) {
173 std::cerr << "Precision " << argv[m] << " is not a number\n";
174 return 1;
175 }
176 } else if (arg == "-E")
177 exact = true;
178 else if (arg == "--input-string") {
179 if (++m == argc) return usage(1, true);
180 istring = argv[m];
181 } else if (arg == "--input-file") {
182 if (++m == argc) return usage(1, true);
183 ifile = argv[m];
184 } else if (arg == "--output-file") {
185 if (++m == argc) return usage(1, true);
186 ofile = argv[m];
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";
191 return 1;
192 }
193 lsep = argv[m][0];
194 } else if (arg == "--comment-delimiter") {
195 if (++m == argc) return usage(1, true);
196 cdelim = argv[m];
197 } else if (arg == "--version") {
198 std::cout << argv[0] << ": GeographicLib version "
199 << GEOGRAPHICLIB_VERSION_STRING << "\n";
200 return 0;
201 } else
202 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
203 }
204
205 if (!ifile.empty() && !istring.empty()) {
206 std::cerr << "Cannot specify --input-string and --input-file together\n";
207 return 1;
208 }
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";
216 return 1;
217 }
218 } else if (!istring.empty()) {
219 std::string::size_type m = 0;
220 while (true) {
221 m = istring.find(lsep, m);
222 if (m == std::string::npos)
223 break;
224 istring[m] = '\n';
225 }
226 instring.str(istring);
227 }
228 std::istream* input = !ifile.empty() ? &infile :
229 (!istring.empty() ? &instring : &std::cin);
230
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";
237 return 1;
238 }
239 }
240 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
241
242 // GeodesicExact mask values are the same as Geodesic
243 unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
244 Geodesic::AZIMUTH; // basic output quantities
245 outmask |= inverse ? Geodesic::DISTANCE : // distance-related flags
246 (arcmode ? Geodesic::NONE : Geodesic::DISTANCE_IN);
247 // longitude unrolling
248 outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
249 // full output -- don't use Geodesic::ALL since this includes DISTANCE_IN
250 outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
251 Geodesic::GEODESICSCALE | Geodesic::AREA) :
252 Geodesic::NONE;
253
254 const Geodesic geods(a, f);
255 const GeodesicExact geode(a, f);
256 GeodesicLine ls;
258 if (linecalc) {
259 if (linecalc == LINE) fraction = false;
260 if (exact) {
261 le = linecalc == DIRECT ?
262 geode.GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
263 linecalc == INVERSE ?
264 geode.InverseLine(lat1, lon1, lat2, lon2, outmask) :
265 // linecalc == LINE
266 geode.Line(lat1, lon1, azi1, outmask);
267 mult = fraction ? le.GenDistance(arcmode) : 1;
268 if (linecalc == INVERSE) azi1 = le.Azimuth();
269 } else {
270 ls = linecalc == DIRECT ?
271 geods.GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
272 linecalc == INVERSE ?
273 geods.InverseLine(lat1, lon1, lat2, lon2, outmask) :
274 // linecalc == LINE
275 geods.Line(lat1, lon1, azi1, outmask);
276 mult = fraction ? ls.GenDistance(arcmode) : 1;
277 if (linecalc == INVERSE) azi1 = ls.Azimuth();
278 }
279 }
280
281 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
282 // 10^-11 sec (= 0.3 nm).
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;
286 int retval = 0;
287 while (std::getline(*input, s)) {
288 try {
289 eol = "\n";
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";
294 s = s.substr(0, m);
295 }
296 }
297 str.clear(); str.str(s);
298 if (inverse) {
299 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
300 throw GeographicErr("Incomplete input: " + s);
301 if (str >> strc)
302 throw GeographicErr("Extraneous input: " + strc);
303 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
304 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
305 a12 = exact ?
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);
310 if (full) {
311 if (unroll) {
312 real e;
313 lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
314 lon2 += e;
315 } else {
316 lon1 = Math::AngNormalize(lon1);
317 lon2 = Math::AngNormalize(lon2);
318 }
319 *output << LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
320 << " ";
321 }
322 *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
323 if (full)
324 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
325 << " ";
326 if (azi2back) {
327 using std::copysign;
328 // map +/-0 -> -/+180; +/-180 -> -/+0
329 // this depends on abs(azi2) <= 180
330 azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
331 }
332 *output << AzimuthString(azi2, prec, dms, dmssep) << " "
333 << DistanceStrings(s12, a12, full, arcmode, prec, dms);
334 if (full)
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));
339 *output << eol;
340 } else {
341 if (linecalc) {
342 if (!(str >> ss12))
343 throw GeographicErr("Incomplete input: " + s);
344 if (str >> strc)
345 throw GeographicErr("Extraneous input: " + strc);
346 // In fraction mode input is read as a distance
347 s12 = ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
348 a12 = exact ?
349 le.GenPosition(arcmode, s12, outmask,
350 lat2, lon2, azi2, s12, m12, M12, M21, S12) :
351 ls.GenPosition(arcmode, s12, outmask,
352 lat2, lon2, azi2, s12, m12, M12, M21, S12);
353 } else {
354 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
355 throw GeographicErr("Incomplete input: " + s);
356 if (str >> strc)
357 throw GeographicErr("Extraneous input: " + strc);
358 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
359 azi1 = DMS::DecodeAzimuth(sazi1);
360 s12 = ReadDistance(ss12, arcmode);
361 a12 = exact ?
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);
366 }
367 if (full)
368 *output
369 << LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
370 prec, dms, dmssep, longfirst)
371 << " " << AzimuthString(azi1, prec, dms, dmssep) << " ";
372 if (azi2back) {
373 using std::copysign;
374 // map +/-0 -> -/+180; +/-180 -> -/+0
375 // this depends on abs(azi2) <= 180
376 azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
377 }
378 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
379 << " " << AzimuthString(azi2, prec, dms, dmssep);
380 if (full)
381 *output << " "
382 << DistanceStrings(s12, a12, full, arcmode, prec, dms)
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));
387 *output << eol;
388 }
389 }
390 catch (const std::exception& e) {
391 // Write error message cout so output lines match input lines
392 *output << "ERROR: " << e.what() << "\n";
393 retval = 1;
394 }
395 }
396 return retval;
397 }
398 catch (const std::exception& e) {
399 std::cerr << "Caught exception: " << e.what() << "\n";
400 return 1;
401 }
402 catch (...) {
403 std::cerr << "Caught unknown exception\n";
404 return 1;
405 }
406}
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
Definition: GeodSolve.cpp:51
int main(int argc, const char *const argv[])
Definition: GeodSolve.cpp:70
real ReadDistance(const std::string &s, bool arcmode, bool fraction=false)
Definition: GeodSolve.cpp:64
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: GeodSolve.cpp:45
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition: GeodSolve.cpp:33
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 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
Geodesic calculations
Definition: Geodesic.hpp:172
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Namespace for GeographicLib.
Definition: Accumulator.cpp:12