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 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;
150 if (linetype == AUTHALIC) {
152 a = sqrt(ellip.
Area() / (4 * Math::pi()));
157 const Rhumb rhumb(a, f);
165 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
166 std::string s, eol(
"\n");
167 real perimeter, area;
169 std::istringstream str;
170 std::string slat, slon, junk;
171 real lat = 0, lon = 0;
172 while (std::getline(*input, s)) {
173 if (!cdelim.empty()) {
174 std::string::size_type m = s.find(cdelim);
175 if (m != std::string::npos) {
176 eol =
" " + s.substr(m) +
"\n";
180 bool endpoly = s.empty();
184 if (geoconvert_compat) {
185 p.
Reset(s,
true, longfirst);
188 str.clear(); str.str(s);
189 if (!(str >> slat >> slon))
193 DMS::DecodeLatLon(slat, slon, lat, lon, longfirst);
195 if (isnan(lat) || isnan(lon))
204 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
205 linetype == RHUMB ? polyr.Compute(reverse, sign, perimeter, area) :
206 poly.Compute(reverse, sign, perimeter, area);
208 *output << num <<
" " << Utility::str(perimeter, prec);
210 *output <<
" " << Utility::str(area, std::max(0, prec - 5));
214 linetype == EXACT ? polye.Clear() :
215 linetype == RHUMB ? polyr.Clear() : poly.Clear();
218 linetype == EXACT ? polye.AddPoint(lat, lon) :
219 linetype == RHUMB ? polyr.AddPoint(lat, lon) :
225 linetype == EXACT ? polye.Compute(reverse, sign, perimeter, area) :
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 == EXACT ? polye.Clear() :
236 linetype == RHUMB ? polyr.Clear() : poly.Clear();
240 catch (
const std::exception& e) {
241 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
245 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.
PolygonAreaT< Rhumb > PolygonAreaRhumb
PolygonAreaT< Geodesic > PolygonArea
PolygonAreaT< GeodesicExact > PolygonAreaExact
Solve of the direct and inverse rhumb problems.
Namespace for GeographicLib.