24# pragma warning (disable: 4127)
27#include "Planimeter.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 enum { GEODESIC, EXACT, AUTHALIC, RHUMB };
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
38 bool reverse =
false, sign =
true, polyline =
false, longfirst =
false;
39 int linetype = GEODESIC;
41 std::string istring, ifile, ofile, cdelim;
44 for (
int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
52 else if (arg ==
"-e") {
53 if (m + 2 >= argc)
return usage(1,
true);
55 a = Utility::val<real>(std::string(argv[m + 1]));
56 f = Utility::fract<real>(std::string(argv[m + 2]));
58 catch (
const std::exception& e) {
59 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
63 }
else if (arg ==
"-w")
64 longfirst = !longfirst;
65 else if (arg ==
"-p") {
66 if (++m == argc)
return usage(1,
true);
68 prec = Utility::val<int>(std::string(argv[m]));
70 catch (
const std::exception&) {
71 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
74 }
else if (arg ==
"-G")
82 else if (arg ==
"--input-string") {
83 if (++m == argc)
return usage(1,
true);
85 }
else if (arg ==
"--input-file") {
86 if (++m == argc)
return usage(1,
true);
88 }
else if (arg ==
"--output-file") {
89 if (++m == argc)
return usage(1,
true);
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";
98 }
else if (arg ==
"--comment-delimiter") {
99 if (++m == argc)
return usage(1,
true);
101 }
else if (arg ==
"--version") {
102 std::cout << argv[0] <<
": GeographicLib version "
103 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
106 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
109 if (!ifile.empty() && !istring.empty()) {
110 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
122 }
else if (!istring.empty()) {
123 std::string::size_type m = 0;
125 m = istring.find(lsep, m);
126 if (m == std::string::npos)
130 instring.str(istring);
132 std::istream* input = !ifile.empty() ? &infile :
133 (!istring.empty() ? &instring : &std::cin);
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";
144 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
147 if (linetype == AUTHALIC) {
149 a = sqrt(ellip.
Area() / (4 * Math::pi()));
154 const Rhumb rhumb(a, f);
155 PolygonArea poly(geod, polyline);
156 PolygonAreaExact polye(geode, polyline);
157 PolygonAreaRhumb polyr(rhumb, polyline);
162 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
163 std::string s, eol(
"\n");
164 real perimeter, area;
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";
174 bool endpoly = s.empty();
178 p.
Reset(s,
true, longfirst);
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);
192 *output << num <<
" " << Utility::str(perimeter, prec);
194 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
198 linetype == EXACT ? polye.Clear() :
199 linetype == RHUMB ? polyr.Clear() : poly.Clear();
204 poly.AddPoint(linetype == AUTHALIC ?
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);
215 *output << num <<
" " << Utility::str(perimeter, prec);
217 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
221 linetype == EXACT ? polye.Clear() :
222 linetype == RHUMB ? polyr.Clear() : poly.Clear();
226 catch (
const std::exception& e) {
227 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
231 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
Header for GeographicLib::Ellipsoid class.
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
int main(int argc, const char *const argv[])
Header for GeographicLib::PolygonAreaT class.
Header for GeographicLib::Utility class.
Properties of an ellipsoid.
Math::real AuthalicLatitude(real phi) const
Conversion between geographic coordinates.
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Math::real Latitude() const
Math::real Longitude() const
Exact geodesic calculations.
Exception handling for GeographicLib.
Solve of the direct and inverse rhumb problems.
Namespace for GeographicLib.