24# pragma warning (disable: 4127 4701)
27#include "TransverseMercatorProj.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 bool exact =
true, extended =
false, reverse =
false, longfirst =
false;
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f(),
38 k0 = Constants::UTM_k0(),
41 std::string istring, ifile, ofile, cdelim;
44 for (
int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
48 else if (arg ==
"-t") {
51 }
else if (arg ==
"-s") {
54 }
else if (arg ==
"-l") {
55 if (++m >= argc)
return usage(1,
true);
58 lon0 = DMS::Decode(std::string(argv[m]), ind);
59 if (ind == DMS::LATITUDE)
61 lon0 = Math::AngNormalize(lon0);
63 catch (
const std::exception& e) {
64 std::cerr <<
"Error decoding argument of " << arg <<
": "
68 }
else if (arg ==
"-k") {
69 if (++m >= argc)
return usage(1,
true);
71 k0 = Utility::val<real>(std::string(argv[m]));
73 catch (
const std::exception& e) {
74 std::cerr <<
"Error decoding argument of " << arg <<
": "
78 }
else if (arg ==
"-e") {
79 if (m + 2 >= argc)
return usage(1,
true);
81 a = Utility::val<real>(std::string(argv[m + 1]));
82 f = Utility::fract<real>(std::string(argv[m + 2]));
84 catch (
const std::exception& e) {
85 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
89 }
else if (arg ==
"-w")
90 longfirst = !longfirst;
91 else if (arg ==
"-p") {
92 if (++m == argc)
return usage(1,
true);
94 prec = Utility::val<int>(std::string(argv[m]));
96 catch (
const std::exception&) {
97 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
100 }
else if (arg ==
"--input-string") {
101 if (++m == argc)
return usage(1,
true);
103 }
else if (arg ==
"--input-file") {
104 if (++m == argc)
return usage(1,
true);
106 }
else if (arg ==
"--output-file") {
107 if (++m == argc)
return usage(1,
true);
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";
116 }
else if (arg ==
"--comment-delimiter") {
117 if (++m == argc)
return usage(1,
true);
119 }
else if (arg ==
"--version") {
120 std::cout << argv[0] <<
": GeographicLib version "
121 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
124 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
127 if (!ifile.empty() && !istring.empty()) {
128 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
140 }
else if (!istring.empty()) {
141 std::string::size_type m = 0;
143 m = istring.find(lsep, m);
144 if (m == std::string::npos)
148 instring.str(istring);
150 std::istream* input = !ifile.empty() ? &infile :
151 (!istring.empty() ? &instring : &std::cin);
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";
162 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
168 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
169 std::string s, eol, stra, strb, strc;
170 std::istringstream str;
172 std::cout << std::fixed;
173 while (std::getline(*input, s)) {
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";
183 str.clear(); str.str(s);
185 if (!(str >> stra >> strb))
188 x = Utility::val<real>(stra);
189 y = Utility::val<real>(strb);
191 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
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;
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;
209 catch (
const std::exception& e) {
210 *output <<
"ERROR: " << e.what() <<
"\n";
216 catch (
const std::exception& e) {
217 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
221 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
int main(int argc, const char *const argv[])
Header for GeographicLib::TransverseMercator class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Transverse Mercator projection.
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Namespace for GeographicLib.