GeographicLib 2.3

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