Skip to content

Instantly share code, notes, and snippets.

@fmg-lydonchandra
Created May 10, 2023 03:16
Show Gist options
  • Save fmg-lydonchandra/86b2e762ded0b7f29f33147f13901d72 to your computer and use it in GitHub Desktop.
Save fmg-lydonchandra/86b2e762ded0b7f29f33147f13901d72 to your computer and use it in GitHub Desktop.
// Online C++ compiler to run C++ program online
#include <iostream>
#include <array>
int main() {
// Latitude Band IDs start from 'C' (i.e. omit 'A' & 'B')
const uint8_t LAT_BAND_ID_TO_MGRS_LETTER_ID_OFFSET = 2;
const char* const MGRS_LETTER = "ABCDEFGHJKLMNPQRSTUVWXYZ";
const std::array<double, 20> BAND_NORTHING = {
-8883084.95594830438, // -80 deg (C)
-7991508.54271004162,
-7100467.04938164819,
-6210141.32687210384,
-5320655.78919156827,
-4432069.05689851660,
-3544369.90953862480,
-2657478.70944542065,
-1771254.01828129962,
-885503.759297154844,
0.000000000000,
885503.759297154145,
1771254.018281299155,
2657478.709445421118,
3544369.909538624808,
4432069.056898516603,
5320655.789191568270,
6210141.326872103848,
7100467.049381648190,
7991508.542710041627, // 72 deg (X)
};
double northing = 2657478;
auto it = std::lower_bound(BAND_NORTHING.begin(), BAND_NORTHING.end(), northing);
auto lat_band_id = (it - BAND_NORTHING.begin()) - 1;
// lat_band_id is 12, BAND_NORTHING[12]
auto lat_band_id_size = static_cast<size_t>(lat_band_id);
auto mgrs1 = MGRS_LETTER[static_cast<size_t>(lat_band_id) + LAT_BAND_ID_TO_MGRS_LETTER_ID_OFFSET];
std::cout << "Hello world! " << lat_band_id << ", " << lat_band_id_size << ", " << mgrs1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment