24# pragma warning (disable: 4127 4701)
27#include "GeodSolve.usage"
35 latstr = dms ? DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) :
36 DMS::Encode(lat, prec + 5, DMS::NUMBER),
37 lonstr = dms ? DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
38 DMS::Encode(lon, prec + 5, DMS::NUMBER);
40 (longfirst ? lonstr : latstr) +
" " + (longfirst ? latstr : lonstr);
45 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
46 DMS::Encode(azi, prec + 5, DMS::NUMBER);
50 bool full,
bool arcmode,
int prec,
bool dms) {
54 s += Utility::str(s12, prec);
58 s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
64 return fraction ? Utility::fract<real>(s) :
65 (arcmode ? DMS::DecodeAngle(s) : Utility::val<real>(s));
68int main(
int argc,
const char*
const argv[]) {
71 enum { NONE = 0, LINE, DIRECT, INVERSE };
72 Utility::set_digits();
73 bool inverse =
false, arcmode =
false,
74 dms =
false, full =
false, exact =
false, unroll =
false,
75 longfirst =
false, azi2back =
false, fraction =
false,
78 a = Constants::WGS84_a(),
79 f = Constants::WGS84_f();
80 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
82 int linecalc = NONE, prec = 3;
83 std::string istring, ifile, ofile, cdelim;
84 char lsep =
';', dmssep = char(0);
86 for (
int m = 1; m < argc; ++m) {
87 std::string arg(argv[m]);
91 }
else if (arg ==
"-a")
95 else if (arg ==
"-L") {
98 if (m + 3 >= argc)
return usage(1,
true);
100 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
101 lat1, lon1, longfirst);
102 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
104 catch (
const std::exception& e) {
105 std::cerr <<
"Error decoding arguments of -L: " << e.what() <<
"\n";
109 }
else if (arg ==
"-D") {
112 if (m + 4 >= argc)
return usage(1,
true);
114 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
115 lat1, lon1, longfirst);
116 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
118 arcmodeline = arcmode;
120 catch (
const std::exception& e) {
121 std::cerr <<
"Error decoding arguments of -D: " << e.what() <<
"\n";
125 }
else if (arg ==
"-I") {
128 if (m + 4 >= argc)
return usage(1,
true);
130 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
131 lat1, lon1, longfirst);
132 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
133 lat2, lon2, longfirst);
135 catch (
const std::exception& e) {
136 std::cerr <<
"Error decoding arguments of -I: " << e.what() <<
"\n";
140 }
else if (arg ==
"-e") {
141 if (m + 2 >= argc)
return usage(1,
true);
143 a = Utility::val<real>(std::string(argv[m + 1]));
144 f = Utility::fract<real>(std::string(argv[m + 2]));
146 catch (
const std::exception& e) {
147 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
151 }
else if (arg ==
"-u")
153 else if (arg ==
"-d") {
156 }
else if (arg ==
"-:") {
159 }
else if (arg ==
"-w")
160 longfirst = !longfirst;
161 else if (arg ==
"-b")
163 else if (arg ==
"-f")
165 else if (arg ==
"-p") {
166 if (++m == argc)
return usage(1,
true);
168 prec = Utility::val<int>(std::string(argv[m]));
170 catch (
const std::exception&) {
171 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
174 }
else if (arg ==
"-E")
176 else if (arg ==
"--input-string") {
177 if (++m == argc)
return usage(1,
true);
179 }
else if (arg ==
"--input-file") {
180 if (++m == argc)
return usage(1,
true);
182 }
else if (arg ==
"--output-file") {
183 if (++m == argc)
return usage(1,
true);
185 }
else if (arg ==
"--line-separator") {
186 if (++m == argc)
return usage(1,
true);
187 if (std::string(argv[m]).size() != 1) {
188 std::cerr <<
"Line separator must be a single character\n";
192 }
else if (arg ==
"--comment-delimiter") {
193 if (++m == argc)
return usage(1,
true);
195 }
else if (arg ==
"--version") {
196 std::cout << argv[0] <<
": GeographicLib version "
197 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
200 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
203 if (!ifile.empty() && !istring.empty()) {
204 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
207 if (ifile ==
"-") ifile.clear();
208 std::ifstream infile;
209 std::istringstream instring;
210 if (!ifile.empty()) {
211 infile.open(ifile.c_str());
212 if (!infile.is_open()) {
213 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
216 }
else if (!istring.empty()) {
217 std::string::size_type m = 0;
219 m = istring.find(lsep, m);
220 if (m == std::string::npos)
224 instring.str(istring);
226 std::istream* input = !ifile.empty() ? &infile :
227 (!istring.empty() ? &instring : &std::cin);
229 std::ofstream outfile;
230 if (ofile ==
"-") ofile.clear();
231 if (!ofile.empty()) {
232 outfile.open(ofile.c_str());
233 if (!outfile.is_open()) {
234 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
238 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
240 unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
242 outmask |= inverse ? Geodesic::DISTANCE :
243 (arcmode ? Geodesic::NONE : Geodesic::DISTANCE_IN);
245 outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
247 outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
248 Geodesic::GEODESICSCALE | Geodesic::AREA) :
254 if (linecalc == LINE) fraction =
false;
255 ls = linecalc == DIRECT ?
256 geods.GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
257 linecalc == INVERSE ?
258 geods.InverseLine(lat1, lon1, lat2, lon2, outmask) :
260 geods.Line(lat1, lon1, azi1, outmask);
262 if (linecalc == INVERSE) azi1 = ls.
Azimuth();
267 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
268 std::string s, eol, slat1, slon1, slat2, slon2, sazi1, ss12, strc;
269 std::istringstream str;
271 while (std::getline(*input, s)) {
274 if (!cdelim.empty()) {
275 std::string::size_type m = s.find(cdelim);
276 if (m != std::string::npos) {
277 eol =
" " + s.substr(m) +
"\n";
281 str.clear(); str.str(s);
283 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
287 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
288 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
289 a12 = geods.GenInverse(lat1, lon1, lat2, lon2, outmask,
290 s12, azi1, azi2, m12, M12, M21, S12);
294 lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
297 lon1 = Math::AngNormalize(lon1);
298 lon2 = Math::AngNormalize(lon2);
300 *output <<
LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
305 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
311 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
316 *output <<
" " << Utility::str(m12, prec)
317 <<
" " << Utility::str(M12, prec+7)
318 <<
" " << Utility::str(M21, prec+7)
319 <<
" " << Utility::str(S12, std::max(prec-7, 0));
328 s12 =
ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
330 lat2, lon2, azi2, s12, m12, M12, M21, S12);
332 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
336 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
337 azi1 = DMS::DecodeAzimuth(sazi1);
339 a12 = geods.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
340 lat2, lon2, azi2, s12, m12, M12, M21, S12);
344 <<
LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
345 prec, dms, dmssep, longfirst)
351 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
353 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
358 <<
" " << Utility::str(m12, prec)
359 <<
" " << Utility::str(M12, prec+7)
360 <<
" " << Utility::str(M21, prec+7)
361 <<
" " << Utility::str(S12, std::max(prec-7, 0));
365 catch (
const std::exception& e) {
367 *output <<
"ERROR: " << e.what() <<
"\n";
373 catch (
const std::exception& e) {
374 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
378 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
int main(int argc, const char *const argv[])
real ReadDistance(const std::string &s, bool arcmode, bool fraction=false)
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Header for GeographicLib::GeodesicLine class.
Header for GeographicLib::Geodesic class.
Header for GeographicLib::Utility class.
Math::real Azimuth() const
Math::real GenDistance(bool arcmode) const
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Exception handling for GeographicLib.
Namespace for GeographicLib.