23# pragma warning (disable: 4127)
26#include "GeoConvert.usage"
28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
34 int outputmode = GEOGRAPHIC;
36 int zone = UTMUPS::MATCH;
37 bool centerp =
true, longfirst =
false;
38 std::string istring, ifile, ofile, cdelim;
39 char lsep =
';', dmssep = char(0);
40 bool sethemisphere =
false, northp =
false, abbrev =
true, latch =
false;
42 for (
int m = 1; m < argc; ++m) {
43 std::string arg(argv[m]);
45 outputmode = GEOGRAPHIC;
46 else if (arg ==
"-d") {
49 }
else if (arg ==
"-:") {
52 }
else if (arg ==
"-u")
57 outputmode = CONVERGENCE;
60 else if (arg ==
"-z") {
61 if (++m == argc)
return usage(1,
true);
62 std::string zonestr(argv[m]);
64 UTMUPS::DecodeZone(zonestr, zone, northp);
67 catch (
const std::exception&) {
68 std::istringstream str(zonestr);
70 if (!(str >> zone) || (str >> c)) {
71 std::cerr <<
"Zone " << zonestr
72 <<
" is not a number or zone+hemisphere\n";
75 if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
76 std::cerr <<
"Zone " << zone <<
" not in [0, 60]\n";
79 sethemisphere =
false;
82 }
else if (arg ==
"-s") {
83 zone = UTMUPS::STANDARD;
84 sethemisphere =
false;
86 }
else if (arg ==
"-S") {
87 zone = UTMUPS::STANDARD;
88 sethemisphere =
false;
90 }
else if (arg ==
"-t") {
92 sethemisphere =
false;
94 }
else if (arg ==
"-T") {
96 sethemisphere =
false;
98 }
else if (arg ==
"-w")
99 longfirst = !longfirst;
100 else if (arg ==
"-p") {
101 if (++m == argc)
return usage(1,
true);
103 prec = Utility::val<int>(std::string(argv[m]));
105 catch (
const std::exception&) {
106 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
109 }
else if (arg ==
"-l")
111 else if (arg ==
"-a")
113 else if (arg ==
"--input-string") {
114 if (++m == argc)
return usage(1,
true);
116 }
else if (arg ==
"--input-file") {
117 if (++m == argc)
return usage(1,
true);
119 }
else if (arg ==
"--output-file") {
120 if (++m == argc)
return usage(1,
true);
122 }
else if (arg ==
"--line-separator") {
123 if (++m == argc)
return usage(1,
true);
124 if (std::string(argv[m]).size() != 1) {
125 std::cerr <<
"Line separator must be a single character\n";
129 }
else if (arg ==
"--comment-delimiter") {
130 if (++m == argc)
return usage(1,
true);
132 }
else if (arg ==
"--version") {
133 std::cout << argv[0] <<
": GeographicLib version "
134 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
138 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
141 if (!ifile.empty() && !istring.empty()) {
142 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
145 if (ifile ==
"-") ifile.clear();
146 std::ifstream infile;
147 std::istringstream instring;
148 if (!ifile.empty()) {
149 infile.open(ifile.c_str());
150 if (!infile.is_open()) {
151 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
154 }
else if (!istring.empty()) {
155 std::string::size_type m = 0;
157 m = istring.find(lsep, m);
158 if (m == std::string::npos)
162 instring.str(istring);
164 std::istream* input = !ifile.empty() ? &infile :
165 (!istring.empty() ? &instring : &std::cin);
167 std::ofstream outfile;
168 if (ofile ==
"-") ofile.clear();
169 if (!ofile.empty()) {
170 outfile.open(ofile.c_str());
171 if (!outfile.is_open()) {
172 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
176 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
183 while (std::getline(*input, s)) {
186 if (!cdelim.empty()) {
187 std::string::size_type m = s.find(cdelim);
188 if (m != std::string::npos) {
189 eol =
" " + s.substr(m) +
"\n";
193 p.
Reset(s, centerp, longfirst);
195 switch (outputmode) {
215 int prec1 = std::max(-5, std::min(Math::extra_digits() + 8, prec));
216 os = Utility::str(gamma, prec1 + 5) +
" "
217 + Utility::str(k, prec1 + 7);
221 zone < UTMUPS::MINZONE && p.
AltZone() >= UTMUPS::MINZONE) {
224 sethemisphere =
true;
228 catch (
const std::exception& e) {
230 os = std::string(
"ERROR: ") + e.what();
233 *output << os << eol;
237 catch (
const std::exception& e) {
238 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
242 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
int main(int argc, const char *const argv[])
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
Header for GeographicLib::MGRS class.
Header for GeographicLib::Utility class.
Convert between degrees and the DMS representation.
Conversion between geographic coordinates.
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
std::string AltMGRSRepresentation(int prec=0) const
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
void SetAltZone(int zone=UTMUPS::STANDARD) const
Math::real AltConvergence() const
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Math::real AltScale() const
Convert between UTM/UPS and MGRS.
Convert between geographic coordinates and UTM/UPS.
Namespace for GeographicLib.