24# pragma warning (disable: 4127 4701)
27#include "ConicProj.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 bool lcc =
false, albers =
false, reverse =
false, longfirst =
false;
35 real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
37 a = Constants::WGS84_a(),
38 f = Constants::WGS84_f();
40 std::string istring, ifile, ofile, cdelim;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
47 else if (arg ==
"-c" || arg ==
"-a") {
50 if (m + 2 >= argc)
return usage(1,
true);
52 for (
int i = 0; i < 2; ++i) {
54 (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
55 if (ind == DMS::LONGITUDE)
59 catch (
const std::exception& e) {
60 std::cerr <<
"Error decoding arguments of " << arg <<
": "
64 }
else if (arg ==
"-l") {
65 if (++m == argc)
return usage(1,
true);
68 lon0 = DMS::Decode(std::string(argv[m]), ind);
69 if (ind == DMS::LATITUDE)
71 lon0 = Math::AngNormalize(lon0);
73 catch (
const std::exception& e) {
74 std::cerr <<
"Error decoding argument of " << arg <<
": "
78 }
else if (arg ==
"-k") {
79 if (++m == argc)
return usage(1,
true);
81 k1 = Utility::val<real>(std::string(argv[m]));
83 catch (
const std::exception& e) {
84 std::cerr <<
"Error decoding argument of " << arg <<
": "
88 }
else if (arg ==
"-e") {
89 if (m + 2 >= argc)
return usage(1,
true);
91 a = Utility::val<real>(std::string(argv[m + 1]));
92 f = Utility::fract<real>(std::string(argv[m + 2]));
94 catch (
const std::exception& e) {
95 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
99 }
else if (arg ==
"-w")
100 longfirst = !longfirst;
101 else if (arg ==
"-p") {
102 if (++m == argc)
return usage(1,
true);
104 prec = Utility::val<int>(std::string(argv[m]));
106 catch (
const std::exception&) {
107 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
110 }
else if (arg ==
"--input-string") {
111 if (++m == argc)
return usage(1,
true);
113 }
else if (arg ==
"--input-file") {
114 if (++m == argc)
return usage(1,
true);
116 }
else if (arg ==
"--output-file") {
117 if (++m == argc)
return usage(1,
true);
119 }
else if (arg ==
"--line-separator") {
120 if (++m == argc)
return usage(1,
true);
121 if (std::string(argv[m]).size() != 1) {
122 std::cerr <<
"Line separator must be a single character\n";
126 }
else if (arg ==
"--comment-delimiter") {
127 if (++m == argc)
return usage(1,
true);
129 }
else if (arg ==
"--version") {
130 std::cout << argv[0] <<
": GeographicLib version "
131 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
134 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
137 if (!ifile.empty() && !istring.empty()) {
138 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
141 if (ifile ==
"-") ifile.clear();
142 std::ifstream infile;
143 std::istringstream instring;
144 if (!ifile.empty()) {
145 infile.open(ifile.c_str());
146 if (!infile.is_open()) {
147 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
150 }
else if (!istring.empty()) {
151 std::string::size_type m = 0;
153 m = istring.find(lsep, m);
154 if (m == std::string::npos)
158 instring.str(istring);
160 std::istream* input = !ifile.empty() ? &infile :
161 (!istring.empty() ? &instring : &std::cin);
163 std::ofstream outfile;
164 if (ofile ==
"-") ofile.clear();
165 if (!ofile.empty()) {
166 outfile.open(ofile.c_str());
167 if (!outfile.is_open()) {
168 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
172 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
174 if (!(lcc || albers)) {
175 std::cerr <<
"Must specify \"-c lat1 lat2\" or "
176 <<
"\"-a lat1 lat2\"\n";
189 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
190 std::string s, eol, stra, strb, strc;
191 std::istringstream str;
193 while (std::getline(*input, s)) {
196 if (!cdelim.empty()) {
197 std::string::size_type m = s.find(cdelim);
198 if (m != std::string::npos) {
199 eol =
" " + s.substr(m) +
"\n";
203 str.clear(); str.str(s);
204 real lat, lon, x, y, gamma, k;
205 if (!(str >> stra >> strb))
208 x = Utility::val<real>(stra);
209 y = Utility::val<real>(strb);
211 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
216 lproj.
Reverse(lon0, x, y, lat, lon, gamma, k);
218 aproj.
Reverse(lon0, x, y, lat, lon, gamma, k);
219 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
220 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
221 << Utility::str(gamma, prec + 6) <<
" "
222 << Utility::str(k, prec + 6) << eol;
225 lproj.
Forward(lon0, lat, lon, x, y, gamma, k);
227 aproj.
Forward(lon0, lat, lon, x, y, gamma, k);
228 *output << Utility::str(x, prec) <<
" "
229 << Utility::str(y, prec) <<
" "
230 << Utility::str(gamma, prec + 6) <<
" "
231 << Utility::str(k, prec + 6) << eol;
234 catch (
const std::exception& e) {
235 *output <<
"ERROR: " << e.what() <<
"\n";
241 catch (
const std::exception& e) {
242 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
246 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::AlbersEqualArea class.
int main(int argc, const char *const argv[])
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
Albers equal area conic 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
Exception handling for GeographicLib.
Namespace for GeographicLib.