GeographicLib 2.4

Loading...
Searching...
No Matches
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) <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="ConicProj.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/LambertConformalConic.hpp>
17#include <GeographicLib/AlbersEqualArea.hpp>
18#include <GeographicLib/DMS.hpp>
19#include <GeographicLib/Utility.hpp>
20
21#if defined(_MSC_VER)
22// Squelch warnings about potentially uninitialized local variables
23# pragma warning (disable: 4701)
24#endif
25
26#include "ConicProj.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 bool lcc = false, albers = false, reverse = false, longfirst = false;
34 real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
35 real
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
38 int prec = 6;
39 std::string istring, ifile, ofile, cdelim;
40 char lsep = ';';
41
42 for (int m = 1; m < argc; ++m) {
43 std::string arg(argv[m]);
44 if (arg == "-r")
45 reverse = true;
46 else if (arg == "-c" || arg == "-a") {
47 lcc = arg == "-c";
48 albers = arg == "-a";
49 if (m + 2 >= argc) return usage(1, true);
50 try {
51 for (int i = 0; i < 2; ++i) {
52 DMS::flag ind;
53 (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
54 if (ind == DMS::LONGITUDE)
55 throw GeographicErr("Bad hemisphere");
56 }
57 }
58 catch (const std::exception& e) {
59 std::cerr << "Error decoding arguments of " << arg << ": "
60 << e.what() << "\n";
61 return 1;
62 }
63 } else if (arg == "-l") {
64 if (++m == argc) return usage(1, true);
65 try {
66 DMS::flag ind;
67 lon0 = DMS::Decode(std::string(argv[m]), ind);
68 if (ind == DMS::LATITUDE)
69 throw GeographicErr("Bad hemisphere");
70 lon0 = Math::AngNormalize(lon0);
71 }
72 catch (const std::exception& e) {
73 std::cerr << "Error decoding argument of " << arg << ": "
74 << e.what() << "\n";
75 return 1;
76 }
77 } else if (arg == "-k") {
78 if (++m == argc) return usage(1, true);
79 try {
80 k1 = Utility::val<real>(std::string(argv[m]));
81 }
82 catch (const std::exception& e) {
83 std::cerr << "Error decoding argument of " << arg << ": "
84 << e.what() << "\n";
85 return 1;
86 }
87 } else if (arg == "-e") {
88 if (m + 2 >= argc) return usage(1, true);
89 try {
90 a = Utility::val<real>(std::string(argv[m + 1]));
91 f = Utility::fract<real>(std::string(argv[m + 2]));
92 }
93 catch (const std::exception& e) {
94 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
95 return 1;
96 }
97 m += 2;
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 == "--input-string") {
110 if (++m == argc) return usage(1, true);
111 istring = argv[m];
112 } else if (arg == "--input-file") {
113 if (++m == argc) return usage(1, true);
114 ifile = argv[m];
115 } else if (arg == "--output-file") {
116 if (++m == argc) return usage(1, true);
117 ofile = argv[m];
118 } else if (arg == "--line-separator") {
119 if (++m == argc) return usage(1, true);
120 if (std::string(argv[m]).size() != 1) {
121 std::cerr << "Line separator must be a single character\n";
122 return 1;
123 }
124 lsep = argv[m][0];
125 } else if (arg == "--comment-delimiter") {
126 if (++m == argc) return usage(1, true);
127 cdelim = argv[m];
128 } else if (arg == "--version") {
129 std::cout << argv[0] << ": GeographicLib version "
130 << GEOGRAPHICLIB_VERSION_STRING << "\n";
131 return 0;
132 } else
133 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
134 }
135
136 if (!ifile.empty() && !istring.empty()) {
137 std::cerr << "Cannot specify --input-string and --input-file together\n";
138 return 1;
139 }
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";
147 return 1;
148 }
149 } else if (!istring.empty()) {
150 std::string::size_type m = 0;
151 while (true) {
152 m = istring.find(lsep, m);
153 if (m == std::string::npos)
154 break;
155 istring[m] = '\n';
156 }
157 instring.str(istring);
158 }
159 std::istream* input = !ifile.empty() ? &infile :
160 (!istring.empty() ? &instring : &std::cin);
161
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";
168 return 1;
169 }
170 }
171 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
172
173 if (!(lcc || albers)) {
174 std::cerr << "Must specify \"-c lat1 lat2\" or "
175 << "\"-a lat1 lat2\"\n";
176 return 1;
177 }
178
179 const LambertConformalConic lproj =
180 lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
181 : LambertConformalConic(1, 0, 0, 0, 1);
182 const AlbersEqualArea aproj =
183 albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
184 : AlbersEqualArea(1, 0, 0, 0, 1);
185
186 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
187 // 10^-11 sec (= 0.3 nm).
188 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
189 std::string s, eol, stra, strb, strc;
190 std::istringstream str;
191 int retval = 0;
192 while (std::getline(*input, s)) {
193 try {
194 eol = "\n";
195 if (!cdelim.empty()) {
196 std::string::size_type m = s.find(cdelim);
197 if (m != std::string::npos) {
198 eol = " " + s.substr(m) + "\n";
199 s = s.substr(0, m);
200 }
201 }
202 str.clear(); str.str(s);
203 real lat, lon, x, y, gamma, k;
204 if (!(str >> stra >> strb))
205 throw GeographicErr("Incomplete input: " + s);
206 if (reverse) {
207 x = Utility::val<real>(stra);
208 y = Utility::val<real>(strb);
209 } else
210 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
211 if (str >> strc)
212 throw GeographicErr("Extraneous input: " + strc);
213 if (reverse) {
214 if (lcc)
215 lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
216 else
217 aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
218 *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
219 << Utility::str(longfirst ? lat : lon, prec + 5) << " "
220 << Utility::str(gamma, prec + 6) << " "
221 << Utility::str(k, prec + 6) << eol;
222 } else {
223 if (lcc)
224 lproj.Forward(lon0, lat, lon, x, y, gamma, k);
225 else
226 aproj.Forward(lon0, lat, lon, x, y, gamma, k);
227 *output << Utility::str(x, prec) << " "
228 << Utility::str(y, prec) << " "
229 << Utility::str(gamma, prec + 6) << " "
230 << Utility::str(k, prec + 6) << eol;
231 }
232 }
233 catch (const std::exception& e) {
234 *output << "ERROR: " << e.what() << "\n";
235 retval = 1;
236 }
237 }
238 return retval;
239 }
240 catch (const std::exception& e) {
241 std::cerr << "Caught exception: " << e.what() << "\n";
242 return 1;
243 }
244 catch (...) {
245 std::cerr << "Caught unknown exception\n";
246 return 1;
247 }
248}
AlbersEqualArea.hpp
Header for GeographicLib::AlbersEqualArea class.
main
int main(int argc, const char *const argv[])
Definition ConicProj.cpp:28
DMS.hpp
Header for GeographicLib::DMS class.
real
GeographicLib::Math::real real
Definition GeodSolve.cpp:28
LambertConformalConic.hpp
Header for GeographicLib::LambertConformalConic class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::AlbersEqualArea
Albers equal area conic projection.
Definition AlbersEqualArea.hpp:60
GeographicLib::AlbersEqualArea::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition AlbersEqualArea.cpp:521
GeographicLib::AlbersEqualArea::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition AlbersEqualArea.cpp:496
GeographicLib::DMS::flag
flag
Definition DMS.hpp:35
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
GeographicLib::LambertConformalConic
Lambert conformal conic projection.
Definition LambertConformalConic.hpp:63
GeographicLib::LambertConformalConic::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition LambertConformalConic.cpp:382
GeographicLib::LambertConformalConic::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition LambertConformalConic.cpp:340
GeographicLib::Math::real
double real
Definition Math.hpp:110
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12