28 #include "moos_geodesy.h" 30 CMOOSGeodesy::CMOOSGeodesy()
31 : m_sUTMZone(0), m_dOriginEasting(0), m_dOriginNorthing(0), m_dOriginLongitude(0),
32 m_dOriginLatitude(0), pj_utm_(0), pj_latlong_(0)
36 CMOOSGeodesy::~CMOOSGeodesy()
42 bool CMOOSGeodesy::Initialise(
double lat,
double lon)
45 SetOriginLatitude(lat);
46 SetOriginLongitude(lon);
48 int zone = (
static_cast<int>(std::floor((lon + 180) / 6)) + 1) % 60;
50 std::stringstream proj_utm;
51 proj_utm <<
"+proj=utm +ellps=WGS84 +zone=" << zone;
53 if (!(pj_utm_ = pj_init_plus(proj_utm.str().c_str())))
55 std::cerr <<
"Failed to initiate utm proj" << std::endl;
58 if (!(pj_latlong_ = pj_init_plus(
"+proj=latlong +ellps=WGS84")))
60 std::cerr <<
"Failed to initiate latlong proj" << std::endl;
65 double tempNorth = lat * DEG_TO_RAD, tempEast = lon * DEG_TO_RAD;
68 if (err = pj_transform(pj_latlong_, pj_utm_, 1, 1, &tempEast, &tempNorth, NULL))
70 std::cerr <<
"Failed to transform datum, reason: " << pj_strerrno(err) << std::endl;
76 SetOriginNorthing(tempNorth);
77 SetOriginEasting(tempEast);
83 double CMOOSGeodesy::GetOriginLongitude() {
return m_dOriginLongitude; }
85 double CMOOSGeodesy::GetOriginLatitude() {
return m_dOriginLatitude; }
87 void CMOOSGeodesy::SetOriginLongitude(
double lon) { m_dOriginLongitude = lon; }
89 void CMOOSGeodesy::SetOriginLatitude(
double lat) { m_dOriginLatitude = lat; }
91 void CMOOSGeodesy::SetOriginNorthing(
double North) { m_dOriginNorthing = North; }
93 void CMOOSGeodesy::SetOriginEasting(
double East) { m_dOriginEasting = East; }
95 void CMOOSGeodesy::SetUTMZone(
int zone) { m_sUTMZone = zone; }
97 int CMOOSGeodesy::GetUTMZone() {
return m_sUTMZone; }
99 bool CMOOSGeodesy::LatLong2LocalUTM(
double lat,
double lon,
double& MetersNorth,
double& MetersEast)
102 double tmpEast = lon * DEG_TO_RAD;
103 double tmpNorth = lat * DEG_TO_RAD;
104 MetersNorth = std::numeric_limits<double>::quiet_NaN();
105 MetersEast = std::numeric_limits<double>::quiet_NaN();
107 if (!pj_latlong_ || !pj_utm_)
109 std::cerr <<
"Must call Initialise before calling LatLong2LocalUTM" << std::endl;
114 if (err = pj_transform(pj_latlong_, pj_utm_, 1, 1, &tmpEast, &tmpNorth, NULL))
116 std::cerr <<
"Failed to transform (lat,lon) = (" << lat <<
"," << lon
117 <<
"), reason: " << pj_strerrno(err) << std::endl;
121 MetersNorth = tmpNorth - GetOriginNorthing();
122 MetersEast = tmpEast - GetOriginEasting();
126 double CMOOSGeodesy::GetOriginEasting() {
return m_dOriginEasting; }
128 double CMOOSGeodesy::GetOriginNorthing() {
return m_dOriginNorthing; }
130 bool CMOOSGeodesy::UTM2LatLong(
double dfX,
double dfY,
double& dfLat,
double& dfLong)
132 double x = dfX + GetOriginEasting();
133 double y = dfY + GetOriginNorthing();
135 dfLat = std::numeric_limits<double>::quiet_NaN();
136 dfLong = std::numeric_limits<double>::quiet_NaN();
138 if (!pj_latlong_ || !pj_utm_)
140 std::cerr <<
"Must call Initialise before calling UTM2LatLong" << std::endl;
145 if (err = pj_transform(pj_utm_, pj_latlong_, 1, 1, &x, &y, NULL))
147 std::cerr <<
"Failed to transform (x,y) = (" << dfX <<
"," << dfY
148 <<
"), reason: " << pj_strerrno(err) << std::endl;
152 dfLat = y * RAD_TO_DEG;
153 dfLong = x * RAD_TO_DEG;