GeographicLib 2.3

GeoConvert.cpp
Go to the documentation of this file.
1/**
2 * \file GeoConvert.cpp
3 * \brief Command line utility for geographic coordinate conversions
4 *
5 * Copyright (c) Charles Karney (2008-2017) <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="GeoConvert.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
16#include <GeographicLib/GeoCoords.hpp>
17#include <GeographicLib/DMS.hpp>
18#include <GeographicLib/Utility.hpp>
19#include <GeographicLib/MGRS.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional expressions
23# pragma warning (disable: 4127)
24#endif
25
26#include "GeoConvert.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 enum { GEOGRAPHIC, DMS, UTMUPS, MGRS, CONVERGENCE };
34 int outputmode = GEOGRAPHIC;
35 int prec = 0;
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;
41
42 for (int m = 1; m < argc; ++m) {
43 std::string arg(argv[m]);
44 if (arg == "-g")
45 outputmode = GEOGRAPHIC;
46 else if (arg == "-d") {
47 outputmode = DMS;
48 dmssep = '\0';
49 } else if (arg == "-:") {
50 outputmode = DMS;
51 dmssep = ':';
52 } else if (arg == "-u")
53 outputmode = UTMUPS;
54 else if (arg == "-m")
55 outputmode = MGRS;
56 else if (arg == "-c")
57 outputmode = CONVERGENCE;
58 else if (arg == "-n")
59 centerp = false;
60 else if (arg == "-z") {
61 if (++m == argc) return usage(1, true);
62 std::string zonestr(argv[m]);
63 try {
64 UTMUPS::DecodeZone(zonestr, zone, northp);
65 sethemisphere = true;
66 }
67 catch (const std::exception&) {
68 std::istringstream str(zonestr);
69 char c;
70 if (!(str >> zone) || (str >> c)) {
71 std::cerr << "Zone " << zonestr
72 << " is not a number or zone+hemisphere\n";
73 return 1;
74 }
75 if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
76 std::cerr << "Zone " << zone << " not in [0, 60]\n";
77 return 1;
78 }
79 sethemisphere = false;
80 }
81 latch = false;
82 } else if (arg == "-s") {
83 zone = UTMUPS::STANDARD;
84 sethemisphere = false;
85 latch = false;
86 } else if (arg == "-S") {
87 zone = UTMUPS::STANDARD;
88 sethemisphere = false;
89 latch = true;
90 } else if (arg == "-t") {
91 zone = UTMUPS::UTM;
92 sethemisphere = false;
93 latch = false;
94 } else if (arg == "-T") {
95 zone = UTMUPS::UTM;
96 sethemisphere = false;
97 latch = true;
98 } else if (arg == "-w")
99 longfirst = !longfirst;
100 else if (arg == "-p") {
101 if (++m == argc) return usage(1, true);
102 try {
103 prec = Utility::val<int>(std::string(argv[m]));
104 }
105 catch (const std::exception&) {
106 std::cerr << "Precision " << argv[m] << " is not a number\n";
107 return 1;
108 }
109 } else if (arg == "-l")
110 abbrev = false;
111 else if (arg == "-a")
112 abbrev = true;
113 else if (arg == "--input-string") {
114 if (++m == argc) return usage(1, true);
115 istring = argv[m];
116 } else if (arg == "--input-file") {
117 if (++m == argc) return usage(1, true);
118 ifile = argv[m];
119 } else if (arg == "--output-file") {
120 if (++m == argc) return usage(1, true);
121 ofile = argv[m];
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";
126 return 1;
127 }
128 lsep = argv[m][0];
129 } else if (arg == "--comment-delimiter") {
130 if (++m == argc) return usage(1, true);
131 cdelim = argv[m];
132 } else if (arg == "--version") {
133 std::cout << argv[0] << ": GeographicLib version "
134 << GEOGRAPHICLIB_VERSION_STRING << "\n";
135 MGRS::Check();
136 return 0;
137 } else
138 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
139 }
140
141 if (!ifile.empty() && !istring.empty()) {
142 std::cerr << "Cannot specify --input-string and --input-file together\n";
143 return 1;
144 }
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";
152 return 1;
153 }
154 } else if (!istring.empty()) {
155 std::string::size_type m = 0;
156 while (true) {
157 m = istring.find(lsep, m);
158 if (m == std::string::npos)
159 break;
160 istring[m] = '\n';
161 }
162 instring.str(istring);
163 }
164 std::istream* input = !ifile.empty() ? &infile :
165 (!istring.empty() ? &instring : &std::cin);
166
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";
173 return 1;
174 }
175 }
176 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
177
178 GeoCoords p;
179 std::string s, eol;
180 std::string os;
181 int retval = 0;
182
183 while (std::getline(*input, s)) {
184 eol = "\n";
185 try {
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";
190 s = s.substr(0, m);
191 }
192 }
193 p.Reset(s, centerp, longfirst);
194 p.SetAltZone(zone);
195 switch (outputmode) {
196 case GEOGRAPHIC:
197 os = p.GeoRepresentation(prec, longfirst);
198 break;
199 case DMS:
200 os = p.DMSRepresentation(prec, longfirst, dmssep);
201 break;
202 case UTMUPS:
203 os = (sethemisphere
204 ? p.AltUTMUPSRepresentation(northp, prec, abbrev)
205 : p.AltUTMUPSRepresentation(prec, abbrev));
206 break;
207 case MGRS:
208 os = p.AltMGRSRepresentation(prec);
209 break;
210 case CONVERGENCE:
211 {
212 real
213 gamma = p.AltConvergence(),
214 k = p.AltScale();
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);
218 }
219 }
220 if (latch &&
221 zone < UTMUPS::MINZONE && p.AltZone() >= UTMUPS::MINZONE) {
222 zone = p.AltZone();
223 northp = p.Northp();
224 sethemisphere = true;
225 latch = false;
226 }
227 }
228 catch (const std::exception& e) {
229 // Write error message to cout so output lines match input lines
230 os = std::string("ERROR: ") + e.what();
231 retval = 1;
232 }
233 *output << os << eol;
234 }
235 return retval;
236 }
237 catch (const std::exception& e) {
238 std::cerr << "Caught exception: " << e.what() << "\n";
239 return 1;
240 }
241 catch (...) {
242 std::cerr << "Caught unknown exception\n";
243 return 1;
244 }
245}
DMS.hpp
Header for GeographicLib::DMS class.
main
int main(int argc, const char *const argv[])
Definition: GeoConvert.cpp:28
GeoCoords.hpp
Header for GeographicLib::GeoCoords class.
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:29
MGRS.hpp
Header for GeographicLib::MGRS class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS
Convert between degrees and the DMS representation.
Definition: DMS.hpp:34
GeographicLib::GeoCoords
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
GeographicLib::GeoCoords::DMSRepresentation
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition: GeoCoords.cpp:68
GeographicLib::GeoCoords::AltZone
int AltZone() const
Definition: GeoCoords.hpp:353
GeographicLib::GeoCoords::Reset
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
GeographicLib::GeoCoords::AltMGRSRepresentation
std::string AltMGRSRepresentation(int prec=0) const
Definition: GeoCoords.cpp:85
GeographicLib::GeoCoords::GeoRepresentation
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
Definition: GeoCoords.cpp:61
GeographicLib::GeoCoords::SetAltZone
void SetAltZone(int zone=UTMUPS::STANDARD) const
Definition: GeoCoords.hpp:335
GeographicLib::GeoCoords::Northp
bool Northp() const
Definition: GeoCoords.hpp:306
GeographicLib::GeoCoords::AltConvergence
Math::real AltConvergence() const
Definition: GeoCoords.hpp:368
GeographicLib::GeoCoords::AltUTMUPSRepresentation
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition: GeoCoords.cpp:134
GeographicLib::GeoCoords::AltScale
Math::real AltScale() const
Definition: GeoCoords.hpp:373
GeographicLib::MGRS
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:74
GeographicLib::Math::real
double real
Definition: Math.hpp:100
GeographicLib::UTMUPS
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12