GeographicLib: CartConvert.cpp Source File

GeographicLib 2.5.2

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