34 # pragma warning (disable: 4127)
37 #include "GeoConvert.usage"
39 int main(
int argc,
char* argv[]) {
41 using namespace GeographicLib;
44 int outputmode = GEOGRAPHIC;
47 bool centerp =
true, swaplatlong =
false;
48 std::string istring, ifile, ofile, cdelim;
49 char lsep =
';', dmssep = char(0);
50 bool sethemisphere =
false, northp =
false;
52 for (
int m = 1; m < argc; ++m) {
53 std::string arg(argv[m]);
55 outputmode = GEOGRAPHIC;
56 else if (arg ==
"-d") {
59 }
else if (arg ==
"-:") {
62 }
else if (arg ==
"-u")
67 outputmode = CONVERGENCE;
72 else if (arg ==
"-p") {
73 if (++m == argc)
return usage(1,
true);
75 prec = Utility::num<int>(std::string(argv[m]));
77 catch (
const std::exception&) {
78 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
81 }
else if (arg ==
"-z") {
82 if (++m == argc)
return usage(1,
true);
83 std::string zonestr(argv[m]);
88 catch (
const std::exception&) {
89 std::istringstream str(zonestr);
91 if (!(str >> zone) || (str >> c)) {
92 std::cerr <<
"Zone " << zonestr
93 <<
" is not a number or zone+hemisphere\n";
97 std::cerr <<
"Zone " << zone <<
" not in [0, 60]\n";
100 sethemisphere =
false;
102 }
else if (arg ==
"-s") {
104 sethemisphere =
false;
105 }
else if (arg ==
"-t") {
107 sethemisphere =
false;
108 }
else if (arg ==
"--input-string") {
109 if (++m == argc)
return usage(1,
true);
111 }
else if (arg ==
"--input-file") {
112 if (++m == argc)
return usage(1,
true);
114 }
else if (arg ==
"--output-file") {
115 if (++m == argc)
return usage(1,
true);
117 }
else if (arg ==
"--line-separator") {
118 if (++m == argc)
return usage(1,
true);
119 if (std::string(argv[m]).size() != 1) {
120 std::cerr <<
"Line separator must be a single character\n";
124 }
else if (arg ==
"--comment-delimiter") {
125 if (++m == argc)
return usage(1,
true);
127 }
else if (arg ==
"--version") {
129 << argv[0] <<
": GeographicLib version "
130 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
133 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
136 if (!ifile.empty() && !istring.empty()) {
137 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
140 if (ifile ==
"-") ifile.clear();
141 std::ifstream infile;
142 std::istringstream instring;
143 if (!ifile.empty()) {
144 infile.open(ifile.c_str());
145 if (!infile.is_open()) {
146 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
149 }
else if (!istring.empty()) {
150 std::string::size_type m = 0;
152 m = istring.find(lsep, m);
153 if (m == std::string::npos)
157 instring.str(istring);
159 std::istream* input = !ifile.empty() ? &infile :
160 (!istring.empty() ? &instring : &std::cin);
162 std::ofstream outfile;
163 if (ofile ==
"-") ofile.clear();
164 if (!ofile.empty()) {
165 outfile.open(ofile.c_str());
166 if (!outfile.is_open()) {
167 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
171 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
178 while (std::getline(*input, s)) {
179 std::string eol(
"\n");
181 if (!cdelim.empty()) {
182 std::string::size_type m = s.find(cdelim);
183 if (m != std::string::npos) {
184 eol =
" " + s.substr(m) +
"\n";
188 p.Reset(s, centerp, swaplatlong);
190 switch (outputmode) {
192 os = p.GeoRepresentation(prec, swaplatlong);
195 os = p.DMSRepresentation(prec, swaplatlong, dmssep);
199 ? p.AltUTMUPSRepresentation(northp, prec)
200 : p.AltUTMUPSRepresentation(prec));
203 os = p.AltMGRSRepresentation(prec);
208 gamma = p.AltConvergence(),
211 os = Utility::str<real>(gamma, prec1 + 5) +
" "
212 + Utility::str<real>(k, prec1 + 7);
216 catch (
const std::exception& e) {
218 os = std::string(
"ERROR: ") + e.what();
221 *output << os << eol;
225 catch (
const std::exception& e) {
226 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
230 std::cerr <<
"Caught unknown exception\n";
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
Conversion between geographic coordinates.
static const int extradigits
Header for GeographicLib::GeoCoords class.
Convert between geographic coordinates and UTM/UPS.
Convert between degrees and the DMS representation.
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
int main(int argc, char *argv[])
Convert between UTM/UPS and MGRS.
Header for GeographicLib::DMS class.