25# pragma warning (disable: 4127 4701)
28#include "TransverseMercatorProj.usage"
30int main(
int argc,
const char*
const argv[]) {
34 Utility::set_digits();
35 bool exact =
true, extended =
false, series =
false, reverse =
false,
38 a = Constants::WGS84_a(),
39 f = Constants::WGS84_f(),
40 k0 = Constants::UTM_k0(),
43 std::string istring, ifile, ofile, cdelim;
46 for (
int m = 1; m < argc; ++m) {
47 std::string arg(argv[m]);
50 else if (arg ==
"-t") {
54 }
else if (arg ==
"-s") {
58 }
else if (arg ==
"-l") {
59 if (++m >= argc)
return usage(1,
true);
62 lon0 = DMS::Decode(std::string(argv[m]), ind);
63 if (ind == DMS::LATITUDE)
65 lon0 = Math::AngNormalize(lon0);
67 catch (
const std::exception& e) {
68 std::cerr <<
"Error decoding argument of " << arg <<
": "
72 }
else if (arg ==
"-k") {
73 if (++m >= argc)
return usage(1,
true);
75 k0 = Utility::val<real>(std::string(argv[m]));
77 catch (
const std::exception& e) {
78 std::cerr <<
"Error decoding argument of " << arg <<
": "
82 }
else if (arg ==
"-e") {
83 if (m + 2 >= argc)
return usage(1,
true);
85 a = Utility::val<real>(std::string(argv[m + 1]));
86 f = Utility::fract<real>(std::string(argv[m + 2]));
88 catch (
const std::exception& e) {
89 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
93 }
else if (arg ==
"-w")
94 longfirst = !longfirst;
95 else if (arg ==
"-p") {
96 if (++m == argc)
return usage(1,
true);
98 prec = Utility::val<int>(std::string(argv[m]));
100 catch (
const std::exception&) {
101 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
104 }
else if (arg ==
"--input-string") {
105 if (++m == argc)
return usage(1,
true);
107 }
else if (arg ==
"--input-file") {
108 if (++m == argc)
return usage(1,
true);
110 }
else if (arg ==
"--output-file") {
111 if (++m == argc)
return usage(1,
true);
113 }
else if (arg ==
"--line-separator") {
114 if (++m == argc)
return usage(1,
true);
115 if (std::string(argv[m]).size() != 1) {
116 std::cerr <<
"Line separator must be a single character\n";
120 }
else if (arg ==
"--comment-delimiter") {
121 if (++m == argc)
return usage(1,
true);
123 }
else if (arg ==
"--version") {
124 std::cout << argv[0] <<
": GeographicLib version "
125 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
128 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
131 if (!ifile.empty() && !istring.empty()) {
132 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
135 if (ifile ==
"-") ifile.clear();
136 std::ifstream infile;
137 std::istringstream instring;
138 if (!ifile.empty()) {
139 infile.open(ifile.c_str());
140 if (!infile.is_open()) {
141 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
144 }
else if (!istring.empty()) {
145 std::string::size_type m = 0;
147 m = istring.find(lsep, m);
148 if (m == std::string::npos)
152 instring.str(istring);
154 std::istream* input = !ifile.empty() ? &infile :
155 (!istring.empty() ? &instring : &std::cin);
157 std::ofstream outfile;
158 if (ofile ==
"-") ofile.clear();
159 if (!ofile.empty()) {
160 outfile.open(ofile.c_str());
161 if (!outfile.is_open()) {
162 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
166 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
177 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
178 std::string s, eol, stra, strb, strc;
179 std::istringstream str;
181 std::cout << std::fixed;
182 while (std::getline(*input, s)) {
185 if (!cdelim.empty()) {
186 std::string::size_type m = s.find(cdelim);
187 if (m != std::string::npos) {
188 eol =
" " + s.substr(m) +
"\n";
192 str.clear(); str.str(s);
194 if (!(str >> stra >> strb))
197 x = Utility::val<real>(stra);
198 y = Utility::val<real>(strb);
200 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
206 TMS.
Reverse(lon0, x, y, lat, lon, gamma, k);
208 TME.
Reverse(lon0, x, y, lat, lon, gamma, k);
209 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
210 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
211 << Utility::str(gamma, prec + 6) <<
" "
212 << Utility::str(k, prec + 6) << eol;
215 TMS.
Forward(lon0, lat, lon, x, y, gamma, k);
217 TME.
Forward(lon0, lat, lon, x, y, gamma, k);
218 *output << Utility::str(x, prec) <<
" "
219 << Utility::str(y, prec) <<
" "
220 << Utility::str(gamma, prec + 6) <<
" "
221 << Utility::str(k, prec + 6) << eol;
224 catch (
const std::exception& e) {
225 *output <<
"ERROR: " << e.what() <<
"\n";
231 catch (
const std::exception& e) {
232 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
236 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Header for GeographicLib::TransverseMercatorExact class.
int main(int argc, const char *const argv[])
Header for GeographicLib::TransverseMercator class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
An exact implementation of the transverse Mercator projection.
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
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.