GeographicLib 1.52
Loading...
Searching...
No Matches
Planimeter.cpp
Go to the documentation of this file.
1/**
2 * \file Planimeter.cpp
3 * \brief Command line utility for measuring the area of geodesic polygons
4 *
5 * Copyright (c) Charles Karney (2010-2020) <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="Planimeter.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
17#include <GeographicLib/DMS.hpp>
21
22#if defined(_MSC_VER)
23// Squelch warnings about constant conditional expressions
24# pragma warning (disable: 4127)
25#endif
26
27#include "Planimeter.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 enum { GEODESIC, EXACT, AUTHALIC, RHUMB };
35 real
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
38 bool reverse = false, sign = true, polyline = false, longfirst = false;
39 int linetype = GEODESIC;
40 int prec = 6;
41 std::string istring, ifile, ofile, cdelim;
42 char lsep = ';';
43
44 for (int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
46 if (arg == "-r")
47 reverse = !reverse;
48 else if (arg == "-s")
49 sign = !sign;
50 else if (arg == "-l")
51 polyline = !polyline;
52 else if (arg == "-e") {
53 if (m + 2 >= argc) return usage(1, true);
54 try {
55 a = Utility::val<real>(std::string(argv[m + 1]));
56 f = Utility::fract<real>(std::string(argv[m + 2]));
57 }
58 catch (const std::exception& e) {
59 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
60 return 1;
61 }
62 m += 2;
63 } else if (arg == "-w")
64 longfirst = !longfirst;
65 else if (arg == "-p") {
66 if (++m == argc) return usage(1, true);
67 try {
68 prec = Utility::val<int>(std::string(argv[m]));
69 }
70 catch (const std::exception&) {
71 std::cerr << "Precision " << argv[m] << " is not a number\n";
72 return 1;
73 }
74 } else if (arg == "-G")
75 linetype = GEODESIC;
76 else if (arg == "-E")
77 linetype = EXACT;
78 else if (arg == "-Q")
79 linetype = AUTHALIC;
80 else if (arg == "-R")
81 linetype = RHUMB;
82 else if (arg == "--input-string") {
83 if (++m == argc) return usage(1, true);
84 istring = argv[m];
85 } else if (arg == "--input-file") {
86 if (++m == argc) return usage(1, true);
87 ifile = argv[m];
88 } else if (arg == "--output-file") {
89 if (++m == argc) return usage(1, true);
90 ofile = argv[m];
91 } else if (arg == "--line-separator") {
92 if (++m == argc) return usage(1, true);
93 if (std::string(argv[m]).size() != 1) {
94 std::cerr << "Line separator must be a single character\n";
95 return 1;
96 }
97 lsep = argv[m][0];
98 } else if (arg == "--comment-delimiter") {
99 if (++m == argc) return usage(1, true);
100 cdelim = argv[m];
101 } else if (arg == "--version") {
102 std::cout << argv[0] << ": GeographicLib version "
103 << GEOGRAPHICLIB_VERSION_STRING << "\n";
104 return 0;
105 } else
106 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
107 }
108
109 if (!ifile.empty() && !istring.empty()) {
110 std::cerr << "Cannot specify --input-string and --input-file together\n";
111 return 1;
112 }
113 if (ifile == "-") ifile.clear();
114 std::ifstream infile;
115 std::istringstream instring;
116 if (!ifile.empty()) {
117 infile.open(ifile.c_str());
118 if (!infile.is_open()) {
119 std::cerr << "Cannot open " << ifile << " for reading\n";
120 return 1;
121 }
122 } else if (!istring.empty()) {
123 std::string::size_type m = 0;
124 while (true) {
125 m = istring.find(lsep, m);
126 if (m == std::string::npos)
127 break;
128 istring[m] = '\n';
129 }
130 instring.str(istring);
131 }
132 std::istream* input = !ifile.empty() ? &infile :
133 (!istring.empty() ? &instring : &std::cin);
134
135 std::ofstream outfile;
136 if (ofile == "-") ofile.clear();
137 if (!ofile.empty()) {
138 outfile.open(ofile.c_str());
139 if (!outfile.is_open()) {
140 std::cerr << "Cannot open " << ofile << " for writing\n";
141 return 1;
142 }
143 }
144 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
145
146 const Ellipsoid ellip(a, f);
147 if (linetype == AUTHALIC) {
148 using std::sqrt;
149 a = sqrt(ellip.Area() / (4 * Math::pi()));
150 f = 0;
151 }
152 const Geodesic geod(a, f);
153 const GeodesicExact geode(a, f);
154 const Rhumb rhumb(a, f);
155 PolygonArea poly(geod, polyline);
156 PolygonAreaExact polye(geode, polyline);
157 PolygonAreaRhumb polyr(rhumb, polyline);
158 GeoCoords p;
159
160 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
161 // 10^-11 sec (= 0.3 nm).
162 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
163 std::string s, eol("\n");
164 real perimeter, area;
165 unsigned num;
166 while (std::getline(*input, s)) {
167 if (!cdelim.empty()) {
168 std::string::size_type m = s.find(cdelim);
169 if (m != std::string::npos) {
170 eol = " " + s.substr(m) + "\n";
171 s = s.substr(0, m);
172 }
173 }
174 bool endpoly = s.empty();
175 if (!endpoly) {
176 try {
177 using std::isnan;
178 p.Reset(s, true, longfirst);
179 if (isnan(p.Latitude()) || isnan(p.Longitude()))
180 endpoly = true;
181 }
182 catch (const GeographicErr&) {
183 endpoly = true;
184 }
185 }
186 if (endpoly) {
187 num =
188 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
189 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
190 poly.Compute(reverse, sign, perimeter, area); // geodesic + authalic
191 if (num > 0) {
192 *output << num << " " << Utility::str(perimeter, prec);
193 if (!polyline) {
194 *output << " " << Utility::str(area, std::max(0, prec - 5));
195 }
196 *output << eol;
197 }
198 linetype == EXACT ? polye.Clear() :
199 linetype == RHUMB ? polyr.Clear() : poly.Clear();
200 eol = "\n";
201 } else {
202 linetype == EXACT ? polye.AddPoint(p.Latitude(), p.Longitude()) :
203 linetype == RHUMB ? polyr.AddPoint(p.Latitude(), p.Longitude()) :
204 poly.AddPoint(linetype == AUTHALIC ?
205 ellip.AuthalicLatitude(p.Latitude()) :
206 p.Latitude(),
207 p.Longitude());
208 }
209 }
210 num =
211 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
212 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
213 poly.Compute(reverse, sign, perimeter, area);
214 if (num > 0) {
215 *output << num << " " << Utility::str(perimeter, prec);
216 if (!polyline) {
217 *output << " " << Utility::str(area, std::max(0, prec - 5));
218 }
219 *output << eol;
220 }
221 linetype == EXACT ? polye.Clear() :
222 linetype == RHUMB ? polyr.Clear() : poly.Clear();
223 eol = "\n";
224 return 0;
225 }
226 catch (const std::exception& e) {
227 std::cerr << "Caught exception: " << e.what() << "\n";
228 return 1;
229 }
230 catch (...) {
231 std::cerr << "Caught unknown exception\n";
232 return 1;
233 }
234}
Header for GeographicLib::DMS class.
Header for GeographicLib::Ellipsoid class.
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
int main(int argc, const char *const argv[])
Definition: Planimeter.cpp:29
Header for GeographicLib::PolygonAreaT class.
Header for GeographicLib::Utility class.
Properties of an ellipsoid.
Definition: Ellipsoid.hpp:39
Math::real Area() const
Definition: Ellipsoid.cpp:40
Math::real AuthalicLatitude(real phi) const
Definition: Ellipsoid.cpp:72
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
Math::real Latitude() const
Definition: GeoCoords.hpp:278
Math::real Longitude() const
Definition: GeoCoords.hpp:283
Exact geodesic calculations.
Geodesic calculations
Definition: Geodesic.hpp:172
Exception handling for GeographicLib.
Definition: Constants.hpp:315
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
Namespace for GeographicLib.
Definition: Accumulator.cpp:12