Loading...
Searching...
No Matches
TransverseMercatorProj.cpp
Go to the documentation of this file.
/**
* \file TransverseMercatorProj.cpp
* \brief Command line utility for transverse Mercator projections
*
* Copyright (c) Charles Karney (2008-2023) <karney@alum.mit.edu> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
*
* See the <a href="TransverseMercatorProj.1.html">man page</a> for usage
* information.
**********************************************************************/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <GeographicLib/TransverseMercator.hpp>
#include <GeographicLib/DMS.hpp>
#include <GeographicLib/Utility.hpp>
#if defined(_MSC_VER)
// Squelch warnings about potentially uninitialized local variables
# pragma warning (disable: 4701)
#endif
#include "TransverseMercatorProj.usage"
int main(int argc, const char* const argv[]) {
try {
using namespace GeographicLib;
typedef Math::real real;
Utility::set_digits();
bool exact = true, extended = false, reverse = false, longfirst = false;
real
a = Constants::WGS84_a(),
f = Constants::WGS84_f(),
k0 = Constants::UTM_k0(),
lon0 = 0;
int prec = 6;
std::string istring, ifile, ofile, cdelim;
char lsep = ';';
for (int m = 1; m < argc; ++m) {
std::string arg(argv[m]);
if (arg == "-r")
reverse = true;
else if (arg == "-t") {
exact = true;
extended = true;
} else if (arg == "-s") {
exact = false;
extended = false;
} else if (arg == "-l") {
if (++m >= argc) return usage(1, true);
try {
DMS::flag ind;
lon0 = DMS::Decode(std::string(argv[m]), ind);
if (ind == DMS::LATITUDE)
throw GeographicErr("Bad hemisphere");
lon0 = Math::AngNormalize(lon0);
}
catch (const std::exception& e) {
std::cerr << "Error decoding argument of " << arg << ": "
<< e.what() << "\n";
return 1;
}
} else if (arg == "-k") {
if (++m >= argc) return usage(1, true);
try {
k0 = Utility::val<real>(std::string(argv[m]));
}
catch (const std::exception& e) {
std::cerr << "Error decoding argument of " << arg << ": "
<< e.what() << "\n";
return 1;
}
} else if (arg == "-e") {
if (m + 2 >= argc) return usage(1, true);
try {
a = Utility::val<real>(std::string(argv[m + 1]));
f = Utility::fract<real>(std::string(argv[m + 2]));
}
catch (const std::exception& e) {
std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
return 1;
}
m += 2;
} else if (arg == "-w")
longfirst = !longfirst;
else if (arg == "-p") {
if (++m == argc) return usage(1, true);
try {
prec = Utility::val<int>(std::string(argv[m]));
}
catch (const std::exception&) {
std::cerr << "Precision " << argv[m] << " is not a number\n";
return 1;
}
} else if (arg == "--input-string") {
if (++m == argc) return usage(1, true);
istring = argv[m];
} else if (arg == "--input-file") {
if (++m == argc) return usage(1, true);
ifile = argv[m];
} else if (arg == "--output-file") {
if (++m == argc) return usage(1, true);
ofile = argv[m];
} else if (arg == "--line-separator") {
if (++m == argc) return usage(1, true);
if (std::string(argv[m]).size() != 1) {
std::cerr << "Line separator must be a single character\n";
return 1;
}
lsep = argv[m][0];
} else if (arg == "--comment-delimiter") {
if (++m == argc) return usage(1, true);
cdelim = argv[m];
} else if (arg == "--version") {
std::cout << argv[0] << ": GeographicLib version "
<< GEOGRAPHICLIB_VERSION_STRING << "\n";
return 0;
} else
return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
}
if (!ifile.empty() && !istring.empty()) {
std::cerr << "Cannot specify --input-string and --input-file together\n";
return 1;
}
if (ifile == "-") ifile.clear();
std::ifstream infile;
std::istringstream instring;
if (!ifile.empty()) {
infile.open(ifile.c_str());
if (!infile.is_open()) {
std::cerr << "Cannot open " << ifile << " for reading\n";
return 1;
}
} else if (!istring.empty()) {
std::string::size_type m = 0;
while (true) {
m = istring.find(lsep, m);
if (m == std::string::npos)
break;
istring[m] = '\n';
}
instring.str(istring);
}
std::istream* input = !ifile.empty() ? &infile :
(!istring.empty() ? &instring : &std::cin);
std::ofstream outfile;
if (ofile == "-") ofile.clear();
if (!ofile.empty()) {
outfile.open(ofile.c_str());
if (!outfile.is_open()) {
std::cerr << "Cannot open " << ofile << " for writing\n";
return 1;
}
}
std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
const TransverseMercator TM(a, f, k0, exact, extended);
// Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
// 10^-11 sec (= 0.3 nm).
prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
std::string s, eol, stra, strb, strc;
std::istringstream str;
int retval = 0;
std::cout << std::fixed;
while (std::getline(*input, s)) {
try {
eol = "\n";
if (!cdelim.empty()) {
std::string::size_type m = s.find(cdelim);
if (m != std::string::npos) {
eol = " " + s.substr(m) + "\n";
s = s.substr(0, m);
}
}
str.clear(); str.str(s);
real lat, lon, x, y;
if (!(str >> stra >> strb))
throw GeographicErr("Incomplete input: " + s);
if (reverse) {
x = Utility::val<real>(stra);
y = Utility::val<real>(strb);
} else
DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
if (str >> strc)
throw GeographicErr("Extraneous input: " + strc);
real gamma, k;
if (reverse) {
TM.Reverse(lon0, x, y, lat, lon, gamma, k);
*output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
<< Utility::str(longfirst ? lat : lon, prec + 5) << " "
<< Utility::str(gamma, prec + 6) << " "
<< Utility::str(k, prec + 6) << eol;
} else {
TM.Forward(lon0, lat, lon, x, y, gamma, k);
*output << Utility::str(x, prec) << " "
<< Utility::str(y, prec) << " "
<< Utility::str(gamma, prec + 6) << " "
<< Utility::str(k, prec + 6) << eol;
}
}
catch (const std::exception& e) {
*output << "ERROR: " << e.what() << "\n";
retval = 1;
}
}
return retval;
}
catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
catch (...) {
std::cerr << "Caught unknown exception\n";
return 1;
}
}
DMS.hpp
Header for GeographicLib::DMS class.
usage
int usage(int retval, bool)
Definition Geod3ODE.cpp:41
real
GeographicLib::Math::real real
Definition Geod3Solve.cpp:25
main
int main(int argc, const char *const argv[])
Definition TransverseMercatorProj.cpp:28
TransverseMercator.hpp
Header for GeographicLib::TransverseMercator class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::Constants::UTM_k0
static T UTM_k0()
Definition Constants.hpp:176
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition Constants.hpp:118
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition Constants.hpp:112
GeographicLib::DMS::flag
flag
Definition DMS.hpp:41
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:51
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition DMS.cpp:374
GeographicLib::DMS::Decode
static Math::real Decode(const std::string &dms, flag &ind)
Definition DMS.cpp:40
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:344
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:69
GeographicLib::Math::real
double real
Definition Math.hpp:115
GeographicLib::Math::extra_digits
static int extra_digits()
Definition Math.cpp:49
GeographicLib::TransverseMercator
Transverse Mercator projection.
Definition TransverseMercator.hpp:94
GeographicLib::TransverseMercator::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition TransverseMercator.cpp:523
GeographicLib::TransverseMercator::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition TransverseMercator.cpp:355
GeographicLib::Utility::fract
static T fract(const std::string &s)
Definition Utility.hpp:298
GeographicLib::Utility::val
static T val(const std::string &s)
Definition Utility.hpp:225
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition Utility.cpp:184
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12