24# pragma warning (disable: 4127)
27#include "Planimeter.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 enum { GEODESIC, AUTHALIC, RHUMB };
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
38 bool reverse =
false, sign =
true, polyline =
false, longfirst =
false,
39 exact =
false, geoconvert_compat =
false;
40 int linetype = GEODESIC;
42 std::string istring, ifile, ofile, cdelim;
45 for (
int m = 1; m < argc; ++m) {
46 std::string arg(argv[m]);
53 else if (arg ==
"-e") {
54 if (m + 2 >= argc)
return usage(1,
true);
56 a = Utility::val<real>(std::string(argv[m + 1]));
57 f = Utility::fract<real>(std::string(argv[m + 2]));
59 catch (
const std::exception& e) {
60 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
64 }
else if (arg ==
"-w")
65 longfirst = !longfirst;
66 else if (arg ==
"-p") {
67 if (++m == argc)
return usage(1,
true);
69 prec = Utility::val<int>(std::string(argv[m]));
71 catch (
const std::exception&) {
72 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
75 }
else if (arg ==
"-G")
83 else if (arg ==
"--geoconvert-input")
84 geoconvert_compat =
true;
85 else if (arg ==
"--input-string") {
86 if (++m == argc)
return usage(1,
true);
88 }
else if (arg ==
"--input-file") {
89 if (++m == argc)
return usage(1,
true);
91 }
else if (arg ==
"--output-file") {
92 if (++m == argc)
return usage(1,
true);
94 }
else if (arg ==
"--line-separator") {
95 if (++m == argc)
return usage(1,
true);
96 if (std::string(argv[m]).size() != 1) {
97 std::cerr <<
"Line separator must be a single character\n";
101 }
else if (arg ==
"--comment-delimiter") {
102 if (++m == argc)
return usage(1,
true);
104 }
else if (arg ==
"--version") {
105 std::cout << argv[0] <<
": GeographicLib version "
106 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
109 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
112 if (!ifile.empty() && !istring.empty()) {
113 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
116 if (ifile ==
"-") ifile.clear();
117 std::ifstream infile;
118 std::istringstream instring;
119 if (!ifile.empty()) {
120 infile.open(ifile.c_str());
121 if (!infile.is_open()) {
122 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
125 }
else if (!istring.empty()) {
126 std::string::size_type m = 0;
128 m = istring.find(lsep, m);
129 if (m == std::string::npos)
133 instring.str(istring);
135 std::istream* input = !ifile.empty() ? &infile :
136 (!istring.empty() ? &instring : &std::cin);
138 std::ofstream outfile;
139 if (ofile ==
"-") ofile.clear();
140 if (!ofile.empty()) {
141 outfile.open(ofile.c_str());
142 if (!outfile.is_open()) {
143 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
147 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
151 if (linetype == AUTHALIC) {
157 const Geodesic geod(a, linetype != RHUMB ? f : 0,
158 exact && linetype != RHUMB);
159 const Rhumb rhumb(a, linetype == RHUMB ? f : 0,
160 exact && linetype == RHUMB);
167 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
168 std::string s, eol(
"\n");
169 real perimeter, area;
171 std::istringstream str;
172 std::string slat, slon, junk;
173 real lat = 0, lon = 0;
174 while (std::getline(*input, s)) {
175 if (!cdelim.empty()) {
176 std::string::size_type m = s.find(cdelim);
177 if (m != std::string::npos) {
178 eol =
" " + s.substr(m) +
"\n";
182 bool endpoly = s.empty();
186 if (geoconvert_compat) {
187 p.
Reset(s,
true, longfirst);
190 str.clear(); str.str(s);
191 if (!(str >> slat >> slon))
195 DMS::DecodeLatLon(slat, slon, lat, lon, longfirst);
197 if (isnan(lat) || isnan(lon))
206 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
207 poly.Compute(reverse, sign, perimeter, area);
209 *output << num <<
" " << Utility::str(perimeter, prec);
211 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
215 linetype == RHUMB ? polyr.Clear() : poly.Clear();
218 linetype == RHUMB ? polyr.AddPoint(lat, lon) :
220 (linetype == AUTHALIC ?
221 ellip.
Convert(AuxLatitude::PHI, AuxLatitude::XI, lat, exact) : lat,
226 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
227 poly.Compute(reverse, sign, perimeter, area);
229 *output << num <<
" " << Utility::str(perimeter, prec);
231 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
235 linetype == RHUMB ? polyr.Clear() : poly.Clear();
239 catch (
const std::exception& e) {
240 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
244 std::cerr <<
"Caught unknown exception\n";
Header for the GeographicLib::AuxLatitude class.
Header for GeographicLib::DMS 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.
Conversions between auxiliary latitudes.
AuxAngle Convert(int auxin, int auxout, const AuxAngle &zeta, bool exact=false) const
Math::real AuthalicRadiusSquared(bool exact=false) 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
Exception handling for GeographicLib.
PolygonAreaT< Rhumb > PolygonAreaRhumb
PolygonAreaT< Geodesic > PolygonArea
Solve of the direct and inverse rhumb problems.
Namespace for GeographicLib.