Files
qt6-migration-pipeline/Lts/Common/LtsCoordTrans/mgrs2lonlat.cpp
T

46 lines
1.2 KiB
C++
Executable File

#include <GeographicLib/UTMUPS.hpp>
#include <GeographicLib/MGRS.hpp>
#include <string>
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <mgrs>\n", argv[0]);
return -1;
}
std::string mgrs = argv[1];
mgrs.erase(remove_if(mgrs.begin(), mgrs.end(), isspace), mgrs.end());
int prec;
double lonDeg, latDeg;
try
{
int zone;
bool northp;
double x, y;
GeographicLib::MGRS::Reverse(mgrs, zone, northp, x, y, prec, true);
GeographicLib::UTMUPS::Reverse(zone, northp, x, y, latDeg, lonDeg);
GeographicLib::UTMUPS::Forward(latDeg, lonDeg, zone, northp, x, y);
GeographicLib::MGRS::Forward(zone, northp, x, y, prec, mgrs);
}
catch (...)
{
fprintf(stderr, "Error converting %s to Lon/Lat coords\n", mgrs.c_str());
return -2;
}
std::string part1, part2, part3, part4;
part1 = mgrs.substr(0, 3);
part2 = mgrs.substr(3, 2);
part3 = mgrs.substr(5, prec);
part4 = mgrs.substr(5 + prec, prec);
fprintf(stdout, "%s %s %s %s = %13.10f, %13.10f ( %13.10f, %13.10f )\n",
part1.c_str(), part2.c_str(), part3.c_str(), part4.c_str(), lonDeg, latDeg, lonDeg / 180. * M_PI, latDeg / 180. * M_PI);
return 0;
}