Loading...
Searching...
No Matches
GeoCoords.cpp
Go to the documentation of this file.
/**
* \file GeoCoords.cpp
* \brief Implementation for GeographicLib::GeoCoords class
*
* Copyright (c) Charles Karney (2008-2022) <karney@alum.mit.edu> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
**********************************************************************/
#include <GeographicLib/GeoCoords.hpp>
#include <GeographicLib/MGRS.hpp>
#include <GeographicLib/DMS.hpp>
#include <GeographicLib/Utility.hpp>
namespace GeographicLib {
using namespace std;
void GeoCoords::Reset(const std::string& s, bool centerp, bool longfirst) {
vector<string> sa;
const char* spaces = " \t\n\v\f\r,"; // Include comma as a space
for (string::size_type pos0 = 0, pos1; pos0 != string::npos;) {
pos1 = s.find_first_not_of(spaces, pos0);
if (pos1 == string::npos)
break;
pos0 = s.find_first_of(spaces, pos1);
sa.push_back(s.substr(pos1, pos0 == string::npos ? pos0 : pos0 - pos1));
}
if (sa.size() == 1) {
int prec;
MGRS::Reverse(sa[0], _zone, _northp, _easting, _northing, prec, centerp);
UTMUPS::Reverse(_zone, _northp, _easting, _northing,
_lat, _long, _gamma, _k);
} else if (sa.size() == 2) {
DMS::DecodeLatLon(sa[0], sa[1], _lat, _long, longfirst);
UTMUPS::Forward( _lat, _long,
_zone, _northp, _easting, _northing, _gamma, _k);
} else if (sa.size() == 3) {
unsigned zoneind, coordind;
if (sa[0].size() > 0 && isalpha(sa[0][sa[0].size() - 1])) {
zoneind = 0;
coordind = 1;
} else if (sa[2].size() > 0 && isalpha(sa[2][sa[2].size() - 1])) {
zoneind = 2;
coordind = 0;
} else
throw GeographicErr("Neither " + sa[0] + " nor " + sa[2]
+ " of the form UTM/UPS Zone + Hemisphere"
+ " (ex: 38n, 09s, n)");
UTMUPS::DecodeZone(sa[zoneind], _zone, _northp);
for (unsigned i = 0; i < 2; ++i)
(i ? _northing : _easting) = Utility::val<real>(sa[coordind + i]);
UTMUPS::Reverse(_zone, _northp, _easting, _northing,
_lat, _long, _gamma, _k);
FixHemisphere();
} else
throw GeographicErr("Coordinate requires 1, 2, or 3 elements");
CopyToAlt();
}
string GeoCoords::GeoRepresentation(int prec, bool longfirst) const {
using std::isnan; // Needed for Centos 7, ubuntu 14
prec = max(0, min(9 + Math::extra_digits(), prec) + 5);
return Utility::str(longfirst ? _long : _lat, prec) +
" " + Utility::str(longfirst ? _lat : _long, prec);
}
string GeoCoords::DMSRepresentation(int prec, bool longfirst,
char dmssep) const {
prec = max(0, min(10 + Math::extra_digits(), prec) + 5);
return DMS::Encode(longfirst ? _long : _lat, unsigned(prec),
longfirst ? DMS::LONGITUDE : DMS::LATITUDE, dmssep) +
" " + DMS::Encode(longfirst ? _lat : _long, unsigned(prec),
longfirst ? DMS::LATITUDE : DMS::LONGITUDE, dmssep);
}
string GeoCoords::MGRSRepresentation(int prec) const {
// Max precision is um
prec = max(-1, min(6, prec) + 5);
string mgrs;
MGRS::Forward(_zone, _northp, _easting, _northing, _lat, prec, mgrs);
return mgrs;
}
string GeoCoords::AltMGRSRepresentation(int prec) const {
// Max precision is um
prec = max(-1, min(6, prec) + 5);
string mgrs;
MGRS::Forward(_alt_zone, _northp, _alt_easting, _alt_northing, _lat, prec,
mgrs);
return mgrs;
}
void GeoCoords::UTMUPSString(int zone, bool northp,
real easting, real northing, int prec,
bool abbrev, string& utm) {
ostringstream os;
prec = max(-5, min(9 + Math::extra_digits(), prec));
// Need extra real because, since C++11, pow(float, int) returns double
real scale = prec < 0 ? real(pow(real(10), -prec)) : real(1);
os << UTMUPS::EncodeZone(zone, northp, abbrev) << fixed << setfill('0');
if (isfinite(easting)) {
os << " " << Utility::str(easting / scale, max(0, prec));
if (prec < 0 && fabs(easting / scale) > real(0.5))
os << setw(-prec) << 0;
} else
os << " nan";
if (isfinite(northing)) {
os << " " << Utility::str(northing / scale, max(0, prec));
if (prec < 0 && fabs(northing / scale) > real(0.5))
os << setw(-prec) << 0;
} else
os << " nan";
utm = os.str();
}
string GeoCoords::UTMUPSRepresentation(int prec, bool abbrev) const {
string utm;
UTMUPSString(_zone, _northp, _easting, _northing, prec, abbrev, utm);
return utm;
}
string GeoCoords::UTMUPSRepresentation(bool northp, int prec,
bool abbrev) const {
real e, n;
int z;
UTMUPS::Transfer(_zone, _northp, _easting, _northing,
_zone, northp, e, n, z);
string utm;
UTMUPSString(_zone, northp, e, n, prec, abbrev, utm);
return utm;
}
string GeoCoords::AltUTMUPSRepresentation(int prec, bool abbrev) const {
string utm;
UTMUPSString(_alt_zone, _northp, _alt_easting, _alt_northing, prec,
abbrev, utm);
return utm;
}
string GeoCoords::AltUTMUPSRepresentation(bool northp, int prec,
bool abbrev) const {
real e, n;
int z;
UTMUPS::Transfer(_alt_zone, _northp, _alt_easting, _alt_northing,
_alt_zone, northp, e, n, z);
string utm;
UTMUPSString(_alt_zone, northp, e, n, prec, abbrev, utm);
return utm;
}
void GeoCoords::FixHemisphere() {
using std::isnan; // Needed for Centos 7, ubuntu 14
if (_lat == 0 || (_northp && _lat >= 0) || (!_northp && _lat < 0) ||
isnan(_lat))
// Allow either hemisphere for equator
return;
if (_zone != UTMUPS::UPS) {
_northing += (_northp ? 1 : -1) * UTMUPS::UTMShift();
_northp = !_northp;
} else
throw GeographicErr("Hemisphere mixup");
}
} // namespace GeographicLib
DMS.hpp
Header for GeographicLib::DMS class.
GeoCoords.hpp
Header for GeographicLib::GeoCoords class.
real
GeographicLib::Math::real real
Definition GeodSolve.cpp:28
MGRS.hpp
Header for GeographicLib::MGRS class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition DMS.hpp:50
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:45
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.cpp:417
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition DMS.cpp:369
GeographicLib::GeoCoords::DMSRepresentation
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition GeoCoords.cpp:68
GeographicLib::GeoCoords::Reset
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition GeoCoords.cpp:19
GeographicLib::GeoCoords::AltMGRSRepresentation
std::string AltMGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:85
GeographicLib::GeoCoords::UTMUPSRepresentation
std::string UTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:117
GeographicLib::GeoCoords::GeoRepresentation
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
Definition GeoCoords.cpp:61
GeographicLib::GeoCoords::AltUTMUPSRepresentation
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:134
GeographicLib::GeoCoords::MGRSRepresentation
std::string MGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:77
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
GeographicLib::MGRS::Reverse
static void Reverse(const std::string &mgrs, int &zone, bool &northp, real &x, real &y, int &prec, bool centerp=true)
Definition MGRS.cpp:159
GeographicLib::MGRS::Forward
static void Forward(int zone, bool northp, real x, real y, int prec, std::string &mgrs)
Definition MGRS.cpp:124
GeographicLib::Math::extra_digits
static int extra_digits()
Definition Math.cpp:49
GeographicLib::UTMUPS::UPS
@ UPS
Definition UTMUPS.hpp:150
GeographicLib::UTMUPS::EncodeZone
static std::string EncodeZone(int zone, bool northp, bool abbrev=true)
Definition UTMUPS.cpp:253
GeographicLib::UTMUPS::Forward
static void Forward(real lat, real lon, int &zone, bool &northp, real &x, real &y, real &gamma, real &k, int setzone=STANDARD, bool mgrslimits=false)
Definition UTMUPS.cpp:65
GeographicLib::UTMUPS::Reverse
static void Reverse(int zone, bool northp, real x, real y, real &lat, real &lon, real &gamma, real &k, bool mgrslimits=false)
Definition UTMUPS.cpp:119
GeographicLib::UTMUPS::UTMShift
static Math::real UTMShift()
Definition UTMUPS.cpp:297
GeographicLib::UTMUPS::Transfer
static void Transfer(int zonein, bool northpin, real xin, real yin, int zoneout, bool northpout, real &xout, real &yout, int &zone)
Definition UTMUPS.cpp:173
GeographicLib::UTMUPS::DecodeZone
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition UTMUPS.cpp:207
GeographicLib::Utility::val
static T val(const std::string &s)
Definition Utility.hpp:225
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806