GeographicLib 2.1.2
CartConvert.cpp
Go to the documentation of this file.
1/**
2 * \file CartConvert.cpp
3 * \brief Command line utility for geodetic to cartesian coordinate conversions
4 *
5 * Copyright (c) Charles Karney (2009-2017) <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="CartConvert.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
18#include <GeographicLib/DMS.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional expressions and potentially
23// uninitialized local variables
24# pragma warning (disable: 4127 4701)
25#endif
26
27#include "CartConvert.usage"
28
29int main(int argc, const char* const argv[]) {
30 try {
31 using namespace GeographicLib;
32 typedef Math::real real;
33 Utility::set_digits();
34 bool localcartesian = false, reverse = false, longfirst = false;
35 real
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
38 int prec = 6;
39 real lat0 = 0, lon0 = 0, h0 = 0;
40 std::string istring, ifile, ofile, cdelim;
41 char lsep = ';';
42
43 for (int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
45 if (arg == "-r")
46 reverse = true;
47 else if (arg == "-l") {
48 localcartesian = true;
49 if (m + 3 >= argc) return usage(1, true);
50 try {
51 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
52 lat0, lon0, longfirst);
53 h0 = Utility::val<real>(std::string(argv[m + 3]));
54 }
55 catch (const std::exception& e) {
56 std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
57 return 1;
58 }
59 m += 3;
60 } else if (arg == "-e") {
61 if (m + 2 >= argc) return usage(1, true);
62 try {
63 a = Utility::val<real>(std::string(argv[m + 1]));
64 f = Utility::fract<real>(std::string(argv[m + 2]));
65 }
66 catch (const std::exception& e) {
67 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
68 return 1;
69 }
70 m += 2;
71 } else if (arg == "-w")
72 longfirst = !longfirst;
73 else if (arg == "-p") {
74 if (++m == argc) return usage(1, true);
75 try {
76 prec = Utility::val<int>(std::string(argv[m]));
77 }
78 catch (const std::exception&) {
79 std::cerr << "Precision " << argv[m] << " is not a number\n";
80 return 1;
81 }
82 } else if (arg == "--input-string") {
83 if (++m == argc) return usage(1, true);
84 istring = argv[m];
85 } else if (arg == "--input-file") {
86 if (++m == argc) return usage(1, true);
87 ifile = argv[m];
88 } else if (arg == "--output-file") {
89 if (++m == argc) return usage(1, true);
90 ofile = argv[m];
91 } else if (arg == "--line-separator") {
92 if (++m == argc) return usage(1, true);
93 if (std::string(argv[m]).size() != 1) {
94 std::cerr << "Line separator must be a single character\n";
95 return 1;
96 }
97 lsep = argv[m][0];
98 } else if (arg == "--comment-delimiter") {
99 if (++m == argc) return usage(1, true);
100 cdelim = argv[m];
101 } else if (arg == "--version") {
102 std::cout << argv[0] << ": GeographicLib version "
103 << GEOGRAPHICLIB_VERSION_STRING << "\n";
104 return 0;
105 } else
106 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
107 }
108
109 if (!ifile.empty() && !istring.empty()) {
110 std::cerr << "Cannot specify --input-string and --input-file together\n";
111 return 1;
112 }
113 if (ifile == "-") ifile.clear();
114 std::ifstream infile;
115 std::istringstream instring;
116 if (!ifile.empty()) {
117 infile.open(ifile.c_str());
118 if (!infile.is_open()) {
119 std::cerr << "Cannot open " << ifile << " for reading\n";
120 return 1;
121 }
122 } else if (!istring.empty()) {
123 std::string::size_type m = 0;
124 while (true) {
125 m = istring.find(lsep, m);
126 if (m == std::string::npos)
127 break;
128 istring[m] = '\n';
129 }
130 instring.str(istring);
131 }
132 std::istream* input = !ifile.empty() ? &infile :
133 (!istring.empty() ? &instring : &std::cin);
134
135 std::ofstream outfile;
136 if (ofile == "-") ofile.clear();
137 if (!ofile.empty()) {
138 outfile.open(ofile.c_str());
139 if (!outfile.is_open()) {
140 std::cerr << "Cannot open " << ofile << " for writing\n";
141 return 1;
142 }
143 }
144 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
145
146 const Geocentric ec(a, f);
147 const LocalCartesian lc(lat0, lon0, h0, ec);
148
149 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
150 // 10^-11 sec (= 0.3 nm).
151 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
152 std::string s, eol, stra, strb, strc, strd;
153 std::istringstream str;
154 int retval = 0;
155 while (std::getline(*input, s)) {
156 try {
157 eol = "\n";
158 if (!cdelim.empty()) {
159 std::string::size_type m = s.find(cdelim);
160 if (m != std::string::npos) {
161 eol = " " + s.substr(m) + "\n";
162 s = s.substr(0, m);
163 }
164 }
165 str.clear(); str.str(s);
166 // initial values to suppress warnings
167 real lat, lon, h, x = 0, y = 0, z = 0;
168 if (!(str >> stra >> strb >> strc))
169 throw GeographicErr("Incomplete input: " + s);
170 if (reverse) {
171 x = Utility::val<real>(stra);
172 y = Utility::val<real>(strb);
173 z = Utility::val<real>(strc);
174 } else {
175 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
176 h = Utility::val<real>(strc);
177 }
178 if (str >> strd)
179 throw GeographicErr("Extraneous input: " + strd);
180 if (reverse) {
181 if (localcartesian)
182 lc.Reverse(x, y, z, lat, lon, h);
183 else
184 ec.Reverse(x, y, z, lat, lon, h);
185 *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
186 << Utility::str(longfirst ? lat : lon, prec + 5) << " "
187 << Utility::str(h, prec) << eol;
188 } else {
189 if (localcartesian)
190 lc.Forward(lat, lon, h, x, y, z);
191 else
192 ec.Forward(lat, lon, h, x, y, z);
193 *output << Utility::str(x, prec) << " "
194 << Utility::str(y, prec) << " "
195 << Utility::str(z, prec) << eol;
196 }
197 }
198 catch (const std::exception& e) {
199 *output << "ERROR: " << e.what() << "\n";
200 retval = 1;
201 }
202 }
203 return retval;
204 }
205 catch (const std::exception& e) {
206 std::cerr << "Caught exception: " << e.what() << "\n";
207 return 1;
208 }
209 catch (...) {
210 std::cerr << "Caught unknown exception\n";
211 return 1;
212 }
213}
int main(int argc, const char *const argv[])
Definition: CartConvert.cpp:29
Header for GeographicLib::DMS class.
Header for GeographicLib::Geocentric class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::LocalCartesian class.
Header for GeographicLib::Utility class.
Geocentric coordinates
Definition: Geocentric.hpp:67
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Definition: Geocentric.hpp:195
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Definition: Geocentric.hpp:132
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Local cartesian coordinates.
void Reverse(real x, real y, real z, real &lat, real &lon, real &h) const
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
Namespace for GeographicLib.
Definition: Accumulator.cpp:12