24int main(
int argc,
const char*
const argv[]) {
28 Utility::set_digits();
29 enum { GEODESIC, AUTHALIC, RHUMB };
31 a = Constants::WGS84_a(),
32 f = Constants::WGS84_f();
33 bool reverse =
false, sign =
true, polyline =
false, longfirst =
false,
34 exact =
false, geoconvert_compat =
false;
35 int linetype = GEODESIC;
37 std::string istring, ifile, ofile, cdelim;
40 for (
int m = 1; m < argc; ++m) {
41 std::string arg(argv[m]);
48 else if (arg ==
"-e") {
49 if (m + 2 >= argc)
return usage(1,
true);
51 a = Utility::val<real>(std::string(argv[m + 1]));
52 f = Utility::fract<real>(std::string(argv[m + 2]));
54 catch (
const std::exception& e) {
55 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
59 }
else if (arg ==
"-w")
60 longfirst = !longfirst;
61 else if (arg ==
"-p") {
62 if (++m == argc)
return usage(1,
true);
64 prec = Utility::val<int>(std::string(argv[m]));
66 catch (
const std::exception&) {
67 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
70 }
else if (arg ==
"-G")
78 else if (arg ==
"--geoconvert-input")
79 geoconvert_compat =
true;
80 else if (arg ==
"--input-string") {
81 if (++m == argc)
return usage(1,
true);
83 }
else if (arg ==
"--input-file") {
84 if (++m == argc)
return usage(1,
true);
86 }
else if (arg ==
"--output-file") {
87 if (++m == argc)
return usage(1,
true);
89 }
else if (arg ==
"--line-separator") {
90 if (++m == argc)
return usage(1,
true);
91 if (std::string(argv[m]).size() != 1) {
92 std::cerr <<
"Line separator must be a single character\n";
96 }
else if (arg ==
"--comment-delimiter") {
97 if (++m == argc)
return usage(1,
true);
99 }
else if (arg ==
"--version") {
100 std::cout << argv[0] <<
": GeographicLib version "
101 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
104 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
107 if (!ifile.empty() && !istring.empty()) {
108 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
111 if (ifile ==
"-") ifile.clear();
112 std::ifstream infile;
113 std::istringstream instring;
114 if (!ifile.empty()) {
115 infile.open(ifile.c_str());
116 if (!infile.is_open()) {
117 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
120 }
else if (!istring.empty()) {
121 std::string::size_type m = 0;
123 m = istring.find(lsep, m);
124 if (m == std::string::npos)
128 instring.str(istring);
130 std::istream* input = !ifile.empty() ? &infile :
131 (!istring.empty() ? &instring : &std::cin);
133 std::ofstream outfile;
134 if (ofile ==
"-") ofile.clear();
135 if (!ofile.empty()) {
136 outfile.open(ofile.c_str());
137 if (!outfile.is_open()) {
138 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
142 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
146 if (linetype == AUTHALIC) {
152 const Geodesic geod(a, linetype != RHUMB ? f : 0,
153 exact && linetype != RHUMB);
154 const Rhumb rhumb(a, linetype == RHUMB ? f : 0,
155 exact && linetype == RHUMB);
156 PolygonArea poly(geod, 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 std::istringstream str;
167 std::string slat, slon, junk;
168 real lat = 0, lon = 0;
169 while (std::getline(*input, s)) {
170 if (!cdelim.empty()) {
171 std::string::size_type m = s.find(cdelim);
172 if (m != std::string::npos) {
173 eol =
" " + s.substr(m) +
"\n";
177 bool endpoly = s.empty();
181 if (geoconvert_compat) {
182 p.
Reset(s,
true, longfirst);
185 str.clear(); str.str(s);
186 if (!(str >> slat >> slon))
190 DMS::DecodeLatLon(slat, slon, lat, lon, longfirst);
192 if (isnan(lat) || isnan(lon))
201 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
202 poly.Compute(reverse, sign, perimeter, area);
204 *output << num <<
" " << Utility::str(perimeter, prec);
206 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
210 linetype == RHUMB ? polyr.Clear() : poly.Clear();
213 linetype == RHUMB ? polyr.AddPoint(lat, lon) :
215 (linetype == AUTHALIC ?
216 ellip.
Convert(AuxLatitude::PHI, AuxLatitude::XI, lat, exact) : lat,
221 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
222 poly.Compute(reverse, sign, perimeter, area);
224 *output << num <<
" " << Utility::str(perimeter, prec);
226 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
230 linetype == RHUMB ? polyr.Clear() : poly.Clear();
234 catch (
const std::exception& e) {
235 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
239 std::cerr <<
"Caught unknown exception\n";