GeographicLib: GeodSolve.cpp Source File

GeographicLib 2.5

Loading...
Searching...
No Matches
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-2023) <karney@alum.mit.edu> 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>
16#include <GeographicLib/Geodesic.hpp>
17#include <GeographicLib/GeodesicLine.hpp>
18#include <GeographicLib/DMS.hpp>
19#include <GeographicLib/Utility.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about potentially uninitialized local variables
23# pragma warning (disable: 4701)
24#endif
25
26#include "GeodSolve.usage"
27
28 typedef GeographicLib::Math::real real;
29
30std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep,
31 bool longfirst) {
32 using namespace GeographicLib;
33 std::string
34 latstr = dms ? DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) :
35 DMS::Encode(lat, prec + 5, DMS::NUMBER),
36 lonstr = dms ? DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
37 DMS::Encode(lon, prec + 5, DMS::NUMBER);
38 return
39 (longfirst ? lonstr : latstr) + " " + (longfirst ? latstr : lonstr);
40}
41
42std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
43 using namespace GeographicLib;
44 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
45 DMS::Encode(azi, prec + 5, DMS::NUMBER);
46}
47
48std::string DistanceStrings(real s12, real a12,
49 bool full, bool arcmode, int prec, bool dms) {
50 using namespace GeographicLib;
51 std::string s;
52 if (full || !arcmode)
53 s += Utility::str(s12, prec);
54 if (full)
55 s += " ";
56 if (full || arcmode)
57 s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
58 return s;
59}
60
61 real ReadDistance(const std::string& s, bool arcmode, bool fraction = false) {
62 using namespace GeographicLib;
63 return fraction ? Utility::fract<real>(s) :
64 (arcmode ? DMS::DecodeAngle(s) : Utility::val<real>(s));
65}
66
67 int main(int argc, const char* const argv[]) {
68 try {
69 using namespace GeographicLib;
70 enum { NONE = 0, LINE, DIRECT, INVERSE };
71 Utility::set_digits();
72 bool inverse = false, arcmode = false,
73 dms = false, full = false, exact = false, unroll = false,
74 longfirst = false, azi2back = false, fraction = false,
75 arcmodeline = false;
76 real
77 a = Constants::WGS84_a(),
78 f = Constants::WGS84_f();
79 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
80 mult = 1;
81 int linecalc = NONE, prec = 3;
82 std::string istring, ifile, ofile, cdelim;
83 char lsep = ';', dmssep = char(0);
84
85 for (int m = 1; m < argc; ++m) {
86 std::string arg(argv[m]);
87 if (arg == "-i") {
88 inverse = true;
89 linecalc = NONE;
90 } else if (arg == "-a")
91 arcmode = !arcmode;
92 else if (arg == "-F")
93 fraction = true;
94 else if (arg == "-L") {
95 inverse = false;
96 linecalc = LINE;
97 if (m + 3 >= argc) return usage(1, true);
98 try {
99 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
100 lat1, lon1, longfirst);
101 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
102 }
103 catch (const std::exception& e) {
104 std::cerr << "Error decoding arguments of -L: " << e.what() << "\n";
105 return 1;
106 }
107 m += 3;
108 } else if (arg == "-D") {
109 inverse = false;
110 linecalc = DIRECT;
111 if (m + 4 >= argc) return usage(1, true);
112 try {
113 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
114 lat1, lon1, longfirst);
115 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
116 s12 = ReadDistance(std::string(argv[m + 4]), arcmode);
117 arcmodeline = arcmode;
118 }
119 catch (const std::exception& e) {
120 std::cerr << "Error decoding arguments of -D: " << e.what() << "\n";
121 return 1;
122 }
123 m += 4;
124 } else if (arg == "-I") {
125 inverse = false;
126 linecalc = INVERSE;
127 if (m + 4 >= argc) return usage(1, true);
128 try {
129 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
130 lat1, lon1, longfirst);
131 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
132 lat2, lon2, longfirst);
133 }
134 catch (const std::exception& e) {
135 std::cerr << "Error decoding arguments of -I: " << e.what() << "\n";
136 return 1;
137 }
138 m += 4;
139 } else if (arg == "-e") {
140 if (m + 2 >= argc) return usage(1, true);
141 try {
142 a = Utility::val<real>(std::string(argv[m + 1]));
143 f = Utility::fract<real>(std::string(argv[m + 2]));
144 }
145 catch (const std::exception& e) {
146 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
147 return 1;
148 }
149 m += 2;
150 } else if (arg == "-u")
151 unroll = true;
152 else if (arg == "-d") {
153 dms = true;
154 dmssep = '\0';
155 } else if (arg == "-:") {
156 dms = true;
157 dmssep = ':';
158 } else if (arg == "-w")
159 longfirst = !longfirst;
160 else if (arg == "-b")
161 azi2back = true;
162 else if (arg == "-f")
163 full = true;
164 else if (arg == "-p") {
165 if (++m == argc) return usage(1, true);
166 try {
167 prec = Utility::val<int>(std::string(argv[m]));
168 }
169 catch (const std::exception&) {
170 std::cerr << "Precision " << argv[m] << " is not a number\n";
171 return 1;
172 }
173 } else if (arg == "-E")
174 exact = true;
175 else if (arg == "--input-string") {
176 if (++m == argc) return usage(1, true);
177 istring = argv[m];
178 } else if (arg == "--input-file") {
179 if (++m == argc) return usage(1, true);
180 ifile = argv[m];
181 } else if (arg == "--output-file") {
182 if (++m == argc) return usage(1, true);
183 ofile = argv[m];
184 } else if (arg == "--line-separator") {
185 if (++m == argc) return usage(1, true);
186 if (std::string(argv[m]).size() != 1) {
187 std::cerr << "Line separator must be a single character\n";
188 return 1;
189 }
190 lsep = argv[m][0];
191 } else if (arg == "--comment-delimiter") {
192 if (++m == argc) return usage(1, true);
193 cdelim = argv[m];
194 } else if (arg == "--version") {
195 std::cout << argv[0] << ": GeographicLib version "
196 << GEOGRAPHICLIB_VERSION_STRING << "\n";
197 return 0;
198 } else
199 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
200 }
201
202 if (!ifile.empty() && !istring.empty()) {
203 std::cerr << "Cannot specify --input-string and --input-file together\n";
204 return 1;
205 }
206 if (ifile == "-") ifile.clear();
207 std::ifstream infile;
208 std::istringstream instring;
209 if (!ifile.empty()) {
210 infile.open(ifile.c_str());
211 if (!infile.is_open()) {
212 std::cerr << "Cannot open " << ifile << " for reading\n";
213 return 1;
214 }
215 } else if (!istring.empty()) {
216 std::string::size_type m = 0;
217 while (true) {
218 m = istring.find(lsep, m);
219 if (m == std::string::npos)
220 break;
221 istring[m] = '\n';
222 }
223 instring.str(istring);
224 }
225 std::istream* input = !ifile.empty() ? &infile :
226 (!istring.empty() ? &instring : &std::cin);
227
228 std::ofstream outfile;
229 if (ofile == "-") ofile.clear();
230 if (!ofile.empty()) {
231 outfile.open(ofile.c_str());
232 if (!outfile.is_open()) {
233 std::cerr << "Cannot open " << ofile << " for writing\n";
234 return 1;
235 }
236 }
237 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
238
239 unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
240 Geodesic::AZIMUTH; // basic output quantities
241 outmask |= inverse ? Geodesic::DISTANCE : // distance-related flags
242 (arcmode ? Geodesic::NONE : Geodesic::DISTANCE_IN);
243 // longitude unrolling
244 outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
245 // full output -- don't use Geodesic::ALL since this includes DISTANCE_IN
246 outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
247 Geodesic::GEODESICSCALE | Geodesic::AREA) :
248 Geodesic::NONE;
249
250 const Geodesic geods(a, f, exact);
251 GeodesicLine ls;
252 if (linecalc) {
253 if (linecalc == LINE) fraction = false;
254 ls = linecalc == DIRECT ?
255 geods.GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
256 linecalc == INVERSE ?
257 geods.InverseLine(lat1, lon1, lat2, lon2, outmask) :
258 // linecalc == LINE
259 geods.Line(lat1, lon1, azi1, outmask);
260 mult = fraction ? ls.GenDistance(arcmode) : 1;
261 if (linecalc == INVERSE) azi1 = ls.Azimuth();
262 }
263
264 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
265 // 10^-11 sec (= 0.3 nm).
266 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
267 std::string s, eol, slat1, slon1, slat2, slon2, sazi1, ss12, strc;
268 std::istringstream str;
269 int retval = 0;
270 while (std::getline(*input, s)) {
271 try {
272 eol = "\n";
273 if (!cdelim.empty()) {
274 std::string::size_type m = s.find(cdelim);
275 if (m != std::string::npos) {
276 eol = " " + s.substr(m) + "\n";
277 s = s.substr(0, m);
278 }
279 }
280 str.clear(); str.str(s);
281 if (inverse) {
282 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
283 throw GeographicErr("Incomplete input: " + s);
284 if (str >> strc)
285 throw GeographicErr("Extraneous input: " + strc);
286 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
287 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
288 a12 = geods.GenInverse(lat1, lon1, lat2, lon2, outmask,
289 s12, azi1, azi2, m12, M12, M21, S12);
290 if (full) {
291 if (unroll) {
292 real e;
293 lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
294 lon2 += e;
295 } else {
296 lon1 = Math::AngNormalize(lon1);
297 lon2 = Math::AngNormalize(lon2);
298 }
299 *output << LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
300 << " ";
301 }
302 *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
303 if (full)
304 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
305 << " ";
306 if (azi2back) {
307 using std::copysign;
308 // map +/-0 -> -/+180; +/-180 -> -/+0
309 // this depends on abs(azi2) <= 180
310 azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
311 }
312 *output << AzimuthString(azi2, prec, dms, dmssep) << " "
313 << DistanceStrings(s12, a12, full, arcmode, prec, dms);
314 if (full)
315 *output << " " << Utility::str(m12, prec)
316 << " " << Utility::str(M12, prec+7)
317 << " " << Utility::str(M21, prec+7)
318 << " " << Utility::str(S12, std::max(prec-7, 0));
319 *output << eol;
320 } else {
321 if (linecalc) {
322 if (!(str >> ss12))
323 throw GeographicErr("Incomplete input: " + s);
324 if (str >> strc)
325 throw GeographicErr("Extraneous input: " + strc);
326 // In fraction mode input is read as a distance
327 s12 = ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
328 a12 = ls.GenPosition(arcmode, s12, outmask,
329 lat2, lon2, azi2, s12, m12, M12, M21, S12);
330 } else {
331 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
332 throw GeographicErr("Incomplete input: " + s);
333 if (str >> strc)
334 throw GeographicErr("Extraneous input: " + strc);
335 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
336 azi1 = DMS::DecodeAzimuth(sazi1);
337 s12 = ReadDistance(ss12, arcmode);
338 a12 = geods.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
339 lat2, lon2, azi2, s12, m12, M12, M21, S12);
340 }
341 if (full)
342 *output
343 << LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
344 prec, dms, dmssep, longfirst)
345 << " " << AzimuthString(azi1, prec, dms, dmssep) << " ";
346 if (azi2back) {
347 using std::copysign;
348 // map +/-0 -> -/+180; +/-180 -> -/+0
349 // this depends on abs(azi2) <= 180
350 azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
351 }
352 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
353 << " " << AzimuthString(azi2, prec, dms, dmssep);
354 if (full)
355 *output << " "
356 << DistanceStrings(s12, a12, full, arcmode, prec, dms)
357 << " " << Utility::str(m12, prec)
358 << " " << Utility::str(M12, prec+7)
359 << " " << Utility::str(M21, prec+7)
360 << " " << Utility::str(S12, std::max(prec-7, 0));
361 *output << eol;
362 }
363 }
364 catch (const std::exception& e) {
365 // Write error message cout so output lines match input lines
366 *output << "ERROR: " << e.what() << "\n";
367 retval = 1;
368 }
369 }
370 return retval;
371 }
372 catch (const std::exception& e) {
373 std::cerr << "Caught exception: " << e.what() << "\n";
374 return 1;
375 }
376 catch (...) {
377 std::cerr << "Caught unknown exception\n";
378 return 1;
379 }
380}
DMS.hpp
Header for GeographicLib::DMS class.
real
GeographicLib::Math::real real
Definition GeodSolve.cpp:28
DistanceStrings
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
Definition GeodSolve.cpp:48
main
int main(int argc, const char *const argv[])
Definition GeodSolve.cpp:67
ReadDistance
real ReadDistance(const std::string &s, bool arcmode, bool fraction=false)
Definition GeodSolve.cpp:61
AzimuthString
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition GeodSolve.cpp:42
LatLonString
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition GeodSolve.cpp:30
GeodesicLine.hpp
Header for GeographicLib::GeodesicLine class.
Geodesic.hpp
Header for GeographicLib::Geodesic class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition Constants.hpp:141
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition Constants.hpp:135
GeographicLib::DMS::DecodeAzimuth
static Math::real DecodeAzimuth(const std::string &azistr)
Definition DMS.cpp:402
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition DMS.hpp:50
GeographicLib::DMS::NUMBER
@ NUMBER
Definition DMS.hpp:61
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:45
GeographicLib::DMS::AZIMUTH
@ AZIMUTH
Definition DMS.hpp:56
GeographicLib::DMS::NONE
@ NONE
Definition DMS.hpp:40
GeographicLib::DMS::DecodeAngle
static Math::real DecodeAngle(const std::string &angstr)
Definition DMS.cpp:393
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.cpp:411
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition DMS.cpp:363
GeographicLib::GeodesicLine
A geodesic line.
Definition GeodesicLine.hpp:74
GeographicLib::GeodesicLine::Azimuth
Math::real Azimuth() const
Definition GeodesicLine.hpp:622
GeographicLib::GeodesicLine::GenDistance
Math::real GenDistance(bool arcmode) const
Definition GeodesicLine.hpp:706
GeographicLib::GeodesicLine::GenPosition
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
Definition GeodesicLine.cpp:142
GeographicLib::Geodesic
Geodesic calculations
Definition Geodesic.hpp:175
GeographicLib::Geodesic::InverseLine
GeodesicLine InverseLine(real lat1, real lon1, real lat2, real lon2, unsigned caps=ALL) const
Definition Geodesic.cpp:532
GeographicLib::Geodesic::Line
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition Geodesic.cpp:123
GeographicLib::Geodesic::GenDirectLine
GeodesicLine GenDirectLine(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned caps=ALL) const
Definition Geodesic.cpp:145
GeographicLib::Geodesic::DISTANCE
@ DISTANCE
Definition Geodesic.hpp:294
GeographicLib::Geodesic::LONG_UNROLL
@ LONG_UNROLL
Definition Geodesic.hpp:326
GeographicLib::Geodesic::REDUCEDLENGTH
@ REDUCEDLENGTH
Definition Geodesic.hpp:311
GeographicLib::Geodesic::LATITUDE
@ LATITUDE
Definition Geodesic.hpp:277
GeographicLib::Geodesic::AREA
@ AREA
Definition Geodesic.hpp:321
GeographicLib::Geodesic::LONGITUDE
@ LONGITUDE
Definition Geodesic.hpp:282
GeographicLib::Geodesic::AZIMUTH
@ AZIMUTH
Definition Geodesic.hpp:289
GeographicLib::Geodesic::NONE
@ NONE
Definition Geodesic.hpp:271
GeographicLib::Geodesic::DISTANCE_IN
@ DISTANCE_IN
Definition Geodesic.hpp:306
GeographicLib::Geodesic::GEODESICSCALE
@ GEODESICSCALE
Definition Geodesic.hpp:316
GeographicLib::Geodesic::GenDirect
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
Definition Geodesic.cpp:128
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:66
GeographicLib::Math::AngDiff
static T AngDiff(T x, T y, T &e)
Definition Math.cpp:77
GeographicLib::Math::hd
static constexpr int hd
degrees per half turn
Definition Math.hpp:148
GeographicLib::Math::real
double real
Definition Math.hpp:110
GeographicLib::Math::extra_digits
static int extra_digits()
Definition Math.cpp:46
GeographicLib::Utility::fract
static T fract(const std::string &s)
Definition Utility.hpp:298
GeographicLib::Utility::val
static T val(const std::string &s)
Definition Utility.hpp:225
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition Utility.cpp:184
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12