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 }
geod_polygon::num
unsigned num
Definition: geodesic.h:217
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.hpp:187
GeographicLib::GeodesicExact
Exact geodesic calculations.
Definition: GeodesicExact.hpp:80
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
GeographicLib::Rhumb
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
sign
const EIGEN_DEVICE_FUNC SignReturnType sign() const
Definition: ArrayCwiseUnaryOps.h:219
real
float real
Definition: datatypes.h:10
GeoCoords.hpp
Header for GeographicLib::GeoCoords class.
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:389
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
GeographicLib::Math::real
double real
Definition: Math.hpp:129
GeographicLib::GeoCoords
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
arg
Definition: cast.h:1412
Utility.hpp
Header for GeographicLib::Utility class.
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
PolygonArea.hpp
Header for GeographicLib::PolygonAreaT class.
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
GeographicLib::Math::isnan
static bool isnan(T x)
Definition: Math.hpp:853
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Ellipsoid.hpp
Header for GeographicLib::Ellipsoid class.
GeographicLib::Ellipsoid::AuthalicLatitude
Math::real AuthalicLatitude(real phi) const
Definition: src/Ellipsoid.cpp:72
GeographicLib::Math::pi
static T pi()
Definition: Math.hpp:202
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition: Constants.hpp:159
main
int main(int argc, const char *const argv[])
Definition: Planimeter.cpp:29
p
float * p
Definition: Tutorial_Map_using.cpp:9
GEOGRAPHICLIB_VERSION_STRING
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
min
#define min(a, b)
Definition: datatypes.h:19
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition: Constants.hpp:169
reverse
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:16
GeographicLib::Ellipsoid
Properties of an ellipsoid.
Definition: Ellipsoid.hpp:39
max
#define max(a, b)
Definition: datatypes.h:20
real
Definition: main.h:100
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
GeographicLib::Ellipsoid::Area
Math::real Area() const
Definition: src/Ellipsoid.cpp:40
DMS.hpp
Header for GeographicLib::DMS class.
GeographicLib::Geodesic
Geodesic calculations
Definition: Geodesic.hpp:172


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:35