GeographicLib  1.43
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
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 
29 int main(int argc, char* argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
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;
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::num<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 == "-p") {
64  if (++m == argc) return usage(1, true);
65  try {
66  prec = Utility::num<int>(std::string(argv[m]));
67  }
68  catch (const std::exception&) {
69  std::cerr << "Precision " << argv[m] << " is not a number\n";
70  return 1;
71  }
72  } else if (arg == "-G")
73  linetype = GEODESIC;
74  else if (arg == "-E")
75  linetype = EXACT;
76  else if (arg == "-Q")
77  linetype = AUTHALIC;
78  else if (arg == "-R")
79  linetype = RHUMB;
80  else if (arg == "--input-string") {
81  if (++m == argc) return usage(1, true);
82  istring = argv[m];
83  } else if (arg == "--input-file") {
84  if (++m == argc) return usage(1, true);
85  ifile = argv[m];
86  } else if (arg == "--output-file") {
87  if (++m == argc) return usage(1, true);
88  ofile = argv[m];
89  } else if (arg == "--line-separator") {
90  if (++m == argc) return usage(1, true);
91  if (std::string(argv[m]).size() != 1) {
92  std::cerr << "Line separator must be a single character\n";
93  return 1;
94  }
95  lsep = argv[m][0];
96  } else if (arg == "--comment-delimiter") {
97  if (++m == argc) return usage(1, true);
98  cdelim = argv[m];
99  } else if (arg == "--version") {
100  std::cout << argv[0] << ": GeographicLib version "
101  << GEOGRAPHICLIB_VERSION_STRING << "\n";
102  return 0;
103  } else
104  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
105  }
106 
107  if (!ifile.empty() && !istring.empty()) {
108  std::cerr << "Cannot specify --input-string and --input-file together\n";
109  return 1;
110  }
111  if (ifile == "-") ifile.clear();
112  std::ifstream infile;
113  std::istringstream instring;
114  if (!ifile.empty()) {
115  infile.open(ifile.c_str());
116  if (!infile.is_open()) {
117  std::cerr << "Cannot open " << ifile << " for reading\n";
118  return 1;
119  }
120  } else if (!istring.empty()) {
121  std::string::size_type m = 0;
122  while (true) {
123  m = istring.find(lsep, m);
124  if (m == std::string::npos)
125  break;
126  istring[m] = '\n';
127  }
128  instring.str(istring);
129  }
130  std::istream* input = !ifile.empty() ? &infile :
131  (!istring.empty() ? &instring : &std::cin);
132 
133  std::ofstream outfile;
134  if (ofile == "-") ofile.clear();
135  if (!ofile.empty()) {
136  outfile.open(ofile.c_str());
137  if (!outfile.is_open()) {
138  std::cerr << "Cannot open " << ofile << " for writing\n";
139  return 1;
140  }
141  }
142  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
143 
144  const Ellipsoid ellip(a, f);
145  if (linetype == AUTHALIC) {
146  using std::sqrt;
147  a = sqrt(ellip.Area() / (4 * Math::pi()));
148  f = 0;
149  }
150  const Geodesic geod(a, f);
151  const GeodesicExact geode(a, f);
152  const Rhumb rhumb(a, f);
153  PolygonArea poly(geod, polyline);
154  PolygonAreaExact polye(geode, polyline);
155  PolygonAreaRhumb polyr(rhumb, polyline);
156  GeoCoords p;
157 
158  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
159  // 10^-11 sec (= 0.3 nm).
160  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
161  std::string s;
162  real perimeter, area;
163  unsigned num;
164  std::string eol("\n");
165  while (std::getline(*input, s)) {
166  if (!cdelim.empty()) {
167  std::string::size_type m = s.find(cdelim);
168  if (m != std::string::npos) {
169  eol = " " + s.substr(m) + "\n";
170  s = s.substr(0, m);
171  }
172  }
173  bool endpoly = s.empty();
174  if (!endpoly) {
175  try {
176  p.Reset(s);
177  if (Math::isnan(p.Latitude()) || Math::isnan(p.Longitude()))
178  endpoly = true;
179  }
180  catch (const GeographicErr&) {
181  endpoly = true;
182  }
183  }
184  if (endpoly) {
185  num =
186  linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
187  linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
188  poly.Compute(reverse, sign, perimeter, area); // geodesic + authalic
189  if (num > 0) {
190  *output << num << " " << Utility::str(perimeter, prec);
191  if (!polyline) {
192  *output << " " << Utility::str(area, std::max(0, prec - 5));
193  }
194  *output << eol;
195  }
196  linetype == EXACT ? polye.Clear() :
197  linetype == RHUMB ? polyr.Clear() : poly.Clear();
198  eol = "\n";
199  } else {
200  linetype == EXACT ? polye.AddPoint(p.Latitude(), p.Longitude()) :
201  linetype == RHUMB ? polyr.AddPoint(p.Latitude(), p.Longitude()) :
202  poly.AddPoint(linetype == AUTHALIC ?
203  ellip.AuthalicLatitude(p.Latitude()) :
204  p.Latitude(),
205  p.Longitude());
206  }
207  }
208  num =
209  linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
210  linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
211  poly.Compute(reverse, sign, perimeter, area);
212  if (num > 0) {
213  *output << num << " " << Utility::str(perimeter, prec);
214  if (!polyline) {
215  *output << " " << Utility::str(area, std::max(0, prec - 5));
216  }
217  *output << eol;
218  }
219  linetype == EXACT ? polye.Clear() :
220  linetype == RHUMB ? polyr.Clear() : poly.Clear();
221  eol = "\n";
222  return 0;
223  }
224  catch (const std::exception& e) {
225  std::cerr << "Caught exception: " << e.what() << "\n";
226  return 1;
227  }
228  catch (...) {
229  std::cerr << "Caught unknown exception\n";
230  return 1;
231  }
232 }
static T pi()
Definition: Math.hpp:214
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
static bool isnan(T x)
Definition: Math.hpp:646
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
Math::real AuthalicLatitude(real phi) const
Definition: Ellipsoid.cpp:72
Header for GeographicLib::GeoCoords class.
static int extra_digits()
Definition: Math.hpp:185
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Header for GeographicLib::Ellipsoid class.
int main(int argc, char *argv[])
Definition: CartConvert.cpp:30
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Properties of an ellipsoid.
Definition: Ellipsoid.hpp:39
Exact geodesic calculations.
Header for GeographicLib::PolygonAreaT class.
Math::real Area() const
Definition: Ellipsoid.cpp:40
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
Geodesic calculations
Definition: Geodesic.hpp:171
Header for GeographicLib::DMS class.