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