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

Function MGRStoLatLon

libs/platform/utm_mgrs_utils.cpp:397–434  ·  view source on GitHub ↗

Convert MGRS parameters to UTM parameters and then use UTM to lat,lon conversion.

Source from the content-addressed store, hash-verified

395
396// Convert MGRS parameters to UTM parameters and then use UTM to lat,lon conversion.
397std::optional<ms::LatLon> MGRStoLatLon(int easting, int northing, int zoneCode, char zoneLetter, char squareCode[2])
398{
399 // Convert easting and northing according to zone_code and square_code
400 if (zoneCode < 1 || zoneCode > 60)
401 return {};
402
403 if (zoneLetter <= 'B' || zoneLetter >= 'Y' || zoneLetter == 'I' || zoneLetter == 'O')
404 return {};
405
406 int const set = zoneCode % kSetOriginColumnLetters.size();
407
408 auto const char1 = squareCode[0];
409 auto const char2 = squareCode[1];
410
411 if (char1 < 'A' || char2 < 'A' || char1 > 'Z' || char2 > 'Z' || char1 == 'I' || char2 == 'I' || char1 == 'O' ||
412 char2 == 'O')
413 return {};
414
415 int const east100k = SquareCharToEasting(char1, set);
416 if (east100k == kInvalidEastingNorthing)
417 return {};
418
419 int north100k = SquareCharToNorthing(char2, set);
420 if (north100k == kInvalidEastingNorthing)
421 return {};
422
423 int const minNorthing = ZoneToMinNorthing(zoneLetter);
424 if (minNorthing == kInvalidEastingNorthing)
425 return {};
426
427 while (north100k < minNorthing)
428 north100k += 2000000;
429
430 easting += east100k;
431 northing += north100k;
432
433 return UTMtoLatLon(easting, northing, zoneCode, zoneLetter);
434}
435
436// Convert lat,lon for WGS84 ellipsoid to MGRS string.
437std::string FormatMGRS(double lat, double lon, int precision)

Callers 1

MatchMGRSCoordsFunction · 0.85

Calls 5

SquareCharToEastingFunction · 0.85
SquareCharToNorthingFunction · 0.85
ZoneToMinNorthingFunction · 0.85
UTMtoLatLonFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected