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, EXACT };
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) {
152 linetype = exact ? EXACT : GEODESIC;
156 }
else if (linetype == GEODESIC && exact)
161 const Rhumb rhumb(a, f, exact && linetype == RHUMB);
169 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
170 std::string s, eol(
"\n");
171 real perimeter, area;
173 std::istringstream str;
174 std::string slat, slon, junk;
175 real lat = 0, lon = 0;
176 while (std::getline(*input, s)) {
177 if (!cdelim.empty()) {
178 std::string::size_type m = s.find(cdelim);
179 if (m != std::string::npos) {
180 eol =
" " + s.substr(m) +
"\n";
184 bool endpoly = s.empty();
188 if (geoconvert_compat) {
189 p.
Reset(s,
true, longfirst);
192 str.clear(); str.str(s);
193 if (!(str >> slat >> slon))
197 DMS::DecodeLatLon(slat, slon, lat, lon, longfirst);
199 if (isnan(lat) || isnan(lon))
208 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
209 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
210 poly.Compute(reverse, sign, perimeter, area);
212 *output << num <<
" " << Utility::str(perimeter, prec);
214 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
218 linetype == EXACT ? polye.Clear() :
219 linetype == RHUMB ? polyr.Clear() : poly.Clear();
222 linetype == EXACT ? polye.AddPoint(lat, lon) :
223 linetype == RHUMB ? polyr.AddPoint(lat, lon) :
225 (linetype == AUTHALIC ?
226 ellip.
Convert(AuxLatitude::PHI, AuxLatitude::XI, lat, exact) : lat,
231 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
232 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
233 poly.Compute(reverse, sign, perimeter, area);
235 *output << num <<
" " << Utility::str(perimeter, prec);
237 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
241 linetype == EXACT ? polye.Clear() :
242 linetype == RHUMB ? polyr.Clear() : poly.Clear();
246 catch (
const std::exception& e) {
247 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
251 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
Exact geodesic calculations.
Exception handling for GeographicLib.
PolygonAreaT< Rhumb > PolygonAreaRhumb
PolygonAreaT< Geodesic > PolygonArea
PolygonAreaT< GeodesicExact > PolygonAreaExact
Solve of the direct and inverse rhumb problems.
Namespace for GeographicLib.