GeographicLib: TransverseMercatorProj.cpp Source File

GeographicLib 2.7

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