MCPcopy Create free account
hub / github.com/comaps/comaps / LatLonToUtm

Function LatLonToUtm

libs/platform/utm_mgrs_utils.cpp:101–144  ·  view source on GitHub ↗

Main algorithm. Formulas source: https://github.com/Turbo87/utm

Source from the content-addressed store, hash-verified

99
100// Main algorithm. Formulas source: https://github.com/Turbo87/utm
101UTMPoint LatLonToUtm(double lat, double lon)
102{
103 using std::sin, std::cos, std::sqrt;
104
105 double const latRad = math::DegToRad(lat);
106 double const latSin = sin(latRad);
107 double const latCos = cos(latRad);
108
109 double const latTan = latSin / latCos;
110 double const latTan2 = latTan * latTan;
111 double const latTan4 = latTan2 * latTan2;
112
113 int const zoneNumber = LatLonToZoneNumber(lat, lon);
114 auto const zoneLetter = LatitudeToZoneLetter(lat);
115 ASSERT(zoneLetter, (lat));
116
117 double const lonRad = math::DegToRad(lon);
118 double const centralLon = ZoneNumberToCentralLon(zoneNumber);
119 double const centralLonRad = math::DegToRad(centralLon);
120
121 double const n = R / sqrt(1.0 - E * latSin * latSin);
122 double const c = E_P2 * latCos * latCos;
123
124 double const a = latCos * NormalizeAngle(lonRad - centralLonRad);
125 double const a2 = a * a;
126 double const a3 = a2 * a;
127 double const a4 = a3 * a;
128 double const a5 = a4 * a;
129 double const a6 = a5 * a;
130
131 double const m = R * (M1 * latRad - M2 * sin(2 * latRad) + M3 * sin(4 * latRad) - M4 * sin(6 * latRad));
132
133 double const easting =
134 K0 * n * (a + a3 / 6 * (1 - latTan2 + c) + a5 / 120 * (5 - 18 * latTan2 + latTan4 + 72 * c - 58 * E_P2)) +
135 500000.0;
136
137 double northing = K0 * (m + n * latTan *
138 (a2 / 2 + a4 / 24 * (5 - latTan2 + 9 * c + 4 * c * c) +
139 a6 / 720 * (61 - 58 * latTan2 + latTan4 + 600 * c - 330 * E_P2)));
140 if (lat < 0.0)
141 northing += 10000000.0;
142
143 return {easting, northing, zoneNumber, zoneLetter};
144}
145
146// Generate UTM string from UTM point parameters.
147std::string UTMtoStr(UTMPoint const & point)

Callers 2

FormatMGRSFunction · 0.85
FormatUTMFunction · 0.85

Calls 6

DegToRadFunction · 0.85
LatLonToZoneNumberFunction · 0.85
LatitudeToZoneLetterFunction · 0.85
ASSERTFunction · 0.85
ZoneNumberToCentralLonFunction · 0.85
NormalizeAngleFunction · 0.70

Tested by

no test coverage detected