Planimeter.cpp
Go to the documentation of this file.
1 
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, const char* const argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  enum { GEODESIC, EXACT, AUTHALIC, RHUMB };
35  real
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  p.Reset(s, true, longfirst);
178  if (Math::isnan(p.Latitude()) || Math::isnan(p.Longitude()))
179  endpoly = true;
180  }
181  catch (const GeographicErr&) {
182  endpoly = true;
183  }
184  }
185  if (endpoly) {
186  num =
187  linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
188  linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
189  poly.Compute(reverse, sign, perimeter, area); // geodesic + authalic
190  if (num > 0) {
191  *output << num << " " << Utility::str(perimeter, prec);
192  if (!polyline) {
193  *output << " " << Utility::str(area, std::max(0, prec - 5));
194  }
195  *output << eol;
196  }
197  linetype == EXACT ? polye.Clear() :
198  linetype == RHUMB ? polyr.Clear() : poly.Clear();
199  eol = "\n";
200  } else {
201  linetype == EXACT ? polye.AddPoint(p.Latitude(), p.Longitude()) :
202  linetype == RHUMB ? polyr.AddPoint(p.Latitude(), p.Longitude()) :
203  poly.AddPoint(linetype == AUTHALIC ?
204  ellip.AuthalicLatitude(p.Latitude()) :
205  p.Latitude(),
206  p.Longitude());
207  }
208  }
209  num =
210  linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
211  linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
212  poly.Compute(reverse, sign, perimeter, area);
213  if (num > 0) {
214  *output << num << " " << Utility::str(perimeter, prec);
215  if (!polyline) {
216  *output << " " << Utility::str(area, std::max(0, prec - 5));
217  }
218  *output << eol;
219  }
220  linetype == EXACT ? polye.Clear() :
221  linetype == RHUMB ? polyr.Clear() : poly.Clear();
222  eol = "\n";
223  return 0;
224  }
225  catch (const std::exception& e) {
226  std::cerr << "Caught exception: " << e.what() << "\n";
227  return 1;
228  }
229  catch (...) {
230  std::cerr << "Caught unknown exception\n";
231  return 1;
232  }
233 }
Matrix3f m
Math::real Latitude() const
Definition: GeoCoords.hpp:278
#define max(a, b)
Definition: datatypes.h:20
static T pi()
Definition: Math.hpp:202
float real
Definition: datatypes.h:10
int main(int argc, const char *const argv[])
Definition: Planimeter.cpp:29
Header for GeographicLib::Utility class.
#define min(a, b)
Definition: datatypes.h:19
static bool isnan(T x)
Definition: Math.hpp:853
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
unsigned num
Definition: geodesic.h:217
Math::real AuthalicLatitude(real phi) const
Header for GeographicLib::GeoCoords class.
Array33i a
static int extra_digits()
Definition: Math.hpp:187
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC const SignReturnType sign() const
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Namespace for GeographicLib.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Header for GeographicLib::Ellipsoid class.
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Properties of an ellipsoid.
Definition: Ellipsoid.hpp:39
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
Exact geodesic calculations.
Header for GeographicLib::PolygonAreaT class.
Math::real Area() const
Exception handling for GeographicLib.
Definition: Constants.hpp:389
void reverse(const MatrixType &m)
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
float * p
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Geodesic calculations
Definition: Geodesic.hpp:172
Math::real Longitude() const
Definition: GeoCoords.hpp:283
Header for GeographicLib::DMS class.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:27