GeographicLib 2.1.2
ConicProj.cpp
Go to the documentation of this file.
1/**
2 * \file ConicProj.cpp
3 * \brief Command line utility for conical projections
4 *
5 * Copyright (c) Charles Karney (2009-2017) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * See the <a href="ConicProj.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
18#include <GeographicLib/DMS.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional expressions and potentially
23// uninitialized local variables
24# pragma warning (disable: 4127 4701)
25#endif
26
27#include "ConicProj.usage"
28
29int main(int argc, const char* const argv[]) {
30 try {
31 using namespace GeographicLib;
32 typedef Math::real real;
33 Utility::set_digits();
34 bool lcc = false, albers = false, reverse = false, longfirst = false;
35 real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
36 real
37 a = Constants::WGS84_a(),
38 f = Constants::WGS84_f();
39 int prec = 6;
40 std::string istring, ifile, ofile, cdelim;
41 char lsep = ';';
42
43 for (int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
45 if (arg == "-r")
46 reverse = true;
47 else if (arg == "-c" || arg == "-a") {
48 lcc = arg == "-c";
49 albers = arg == "-a";
50 if (m + 2 >= argc) return usage(1, true);
51 try {
52 for (int i = 0; i < 2; ++i) {
53 DMS::flag ind;
54 (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
55 if (ind == DMS::LONGITUDE)
56 throw GeographicErr("Bad hemisphere");
57 }
58 }
59 catch (const std::exception& e) {
60 std::cerr << "Error decoding arguments of " << arg << ": "
61 << e.what() << "\n";
62 return 1;
63 }
64 } else if (arg == "-l") {
65 if (++m == argc) return usage(1, true);
66 try {
67 DMS::flag ind;
68 lon0 = DMS::Decode(std::string(argv[m]), ind);
69 if (ind == DMS::LATITUDE)
70 throw GeographicErr("Bad hemisphere");
71 lon0 = Math::AngNormalize(lon0);
72 }
73 catch (const std::exception& e) {
74 std::cerr << "Error decoding argument of " << arg << ": "
75 << e.what() << "\n";
76 return 1;
77 }
78 } else if (arg == "-k") {
79 if (++m == argc) return usage(1, true);
80 try {
81 k1 = Utility::val<real>(std::string(argv[m]));
82 }
83 catch (const std::exception& e) {
84 std::cerr << "Error decoding argument of " << arg << ": "
85 << e.what() << "\n";
86 return 1;
87 }
88 } else if (arg == "-e") {
89 if (m + 2 >= argc) return usage(1, true);
90 try {
91 a = Utility::val<real>(std::string(argv[m + 1]));
92 f = Utility::fract<real>(std::string(argv[m + 2]));
93 }
94 catch (const std::exception& e) {
95 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
96 return 1;
97 }
98 m += 2;
99 } else if (arg == "-w")
100 longfirst = !longfirst;
101 else if (arg == "-p") {
102 if (++m == argc) return usage(1, true);
103 try {
104 prec = Utility::val<int>(std::string(argv[m]));
105 }
106 catch (const std::exception&) {
107 std::cerr << "Precision " << argv[m] << " is not a number\n";
108 return 1;
109 }
110 } else if (arg == "--input-string") {
111 if (++m == argc) return usage(1, true);
112 istring = argv[m];
113 } else if (arg == "--input-file") {
114 if (++m == argc) return usage(1, true);
115 ifile = argv[m];
116 } else if (arg == "--output-file") {
117 if (++m == argc) return usage(1, true);
118 ofile = argv[m];
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";
123 return 1;
124 }
125 lsep = argv[m][0];
126 } else if (arg == "--comment-delimiter") {
127 if (++m == argc) return usage(1, true);
128 cdelim = argv[m];
129 } else if (arg == "--version") {
130 std::cout << argv[0] << ": GeographicLib version "
131 << GEOGRAPHICLIB_VERSION_STRING << "\n";
132 return 0;
133 } else
134 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
135 }
136
137 if (!ifile.empty() && !istring.empty()) {
138 std::cerr << "Cannot specify --input-string and --input-file together\n";
139 return 1;
140 }
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";
148 return 1;
149 }
150 } else if (!istring.empty()) {
151 std::string::size_type m = 0;
152 while (true) {
153 m = istring.find(lsep, m);
154 if (m == std::string::npos)
155 break;
156 istring[m] = '\n';
157 }
158 instring.str(istring);
159 }
160 std::istream* input = !ifile.empty() ? &infile :
161 (!istring.empty() ? &instring : &std::cin);
162
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";
169 return 1;
170 }
171 }
172 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
173
174 if (!(lcc || albers)) {
175 std::cerr << "Must specify \"-c lat1 lat2\" or "
176 << "\"-a lat1 lat2\"\n";
177 return 1;
178 }
179
180 const LambertConformalConic lproj =
181 lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
182 : LambertConformalConic(1, 0, 0, 0, 1);
183 const AlbersEqualArea aproj =
184 albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
185 : AlbersEqualArea(1, 0, 0, 0, 1);
186
187 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
188 // 10^-11 sec (= 0.3 nm).
189 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
190 std::string s, eol, stra, strb, strc;
191 std::istringstream str;
192 int retval = 0;
193 while (std::getline(*input, s)) {
194 try {
195 eol = "\n";
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";
200 s = s.substr(0, m);
201 }
202 }
203 str.clear(); str.str(s);
204 real lat, lon, x, y, gamma, k;
205 if (!(str >> stra >> strb))
206 throw GeographicErr("Incomplete input: " + s);
207 if (reverse) {
208 x = Utility::val<real>(stra);
209 y = Utility::val<real>(strb);
210 } else
211 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
212 if (str >> strc)
213 throw GeographicErr("Extraneous input: " + strc);
214 if (reverse) {
215 if (lcc)
216 lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
217 else
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;
223 } else {
224 if (lcc)
225 lproj.Forward(lon0, lat, lon, x, y, gamma, k);
226 else
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;
232 }
233 }
234 catch (const std::exception& e) {
235 *output << "ERROR: " << e.what() << "\n";
236 retval = 1;
237 }
238 }
239 return retval;
240 }
241 catch (const std::exception& e) {
242 std::cerr << "Caught exception: " << e.what() << "\n";
243 return 1;
244 }
245 catch (...) {
246 std::cerr << "Caught unknown exception\n";
247 return 1;
248 }
249}
Header for GeographicLib::AlbersEqualArea class.
int main(int argc, const char *const argv[])
Definition: ConicProj.cpp:29
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::LambertConformalConic class.
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.
Definition: Constants.hpp:316
Lambert conformal 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
Namespace for GeographicLib.
Definition: Accumulator.cpp:12