Changed UTM & MGRS API to use string instead of optional<string>

Signed-off-by: S. Kozyr <s.trump@gmail.com>
This commit is contained in:
Sergiy Kozyr 2023-05-31 10:22:02 +03:00
parent 950e1015a3
commit 782099961d
Signed by: strump
GPG key ID: C622E5563CAC205D
4 changed files with 66 additions and 67 deletions

View file

@ -963,17 +963,17 @@ Java_app_organicmaps_Framework_nativeFormatLatLon(JNIEnv * env, jclass, jdouble
return jni::ToJavaString(env, measurement_utils::FormatOsmLink(lat, lon, 14));
case android::CoordinatesFormat::UTM: // Universal Transverse Mercator
{
optional<string> utmFormat = utm_mgrs_utils::FormatUTM(lat, lon);
if (utmFormat)
return jni::ToJavaString(env, utmFormat.value());
string utmFormat = utm_mgrs_utils::FormatUTM(lat, lon);
if (utmFormat.empty())
return jni::ToJavaString(env, utmFormat);
else
return nullptr;
}
case android::CoordinatesFormat::MGRS: // Military Grid Reference System
{
optional<string> mgrsFormat = utm_mgrs_utils::FormatMGRS(lat, lon, 5);
if (mgrsFormat)
return jni::ToJavaString(env, mgrsFormat.value());
string mgrsFormat = utm_mgrs_utils::FormatMGRS(lat, lon, 5);
if (mgrsFormat.empty())
return jni::ToJavaString(env, mgrsFormat);
else
return nullptr;
}

View file

@ -6,65 +6,64 @@
using namespace utm_mgrs_utils;
#define OPT(a) std::make_optional(a)
UNIT_TEST(FormatUTM)
{
TEST_EQUAL(FormatUTM(42.0, -93.0), OPT("15N 500000 4649776"), ());
TEST_EQUAL(FormatUTM(31.77597, 35.23406), OPT("36N 711554 3517777"), ());
TEST_EQUAL(FormatUTM(-34.81449, -58.54016), OPT("21S 359135 6146448"), ());
TEST_EQUAL(FormatUTM(36.2361322, -115.0820944), OPT("11N 672349 4011845"), ());
TEST_EQUAL(FormatUTM(42.0, -93.0), "15N 500000 4649776", ());
TEST_EQUAL(FormatUTM(31.77597, 35.23406), "36N 711554 3517777", ());
TEST_EQUAL(FormatUTM(-34.81449, -58.54016), "21S 359135 6146448", ());
TEST_EQUAL(FormatUTM(36.2361322, -115.0820944), "11N 672349 4011845", ());
TEST_EQUAL(FormatUTM(50.77535, 6.08389), OPT("32N 294409 5628898"), ());
TEST_EQUAL(FormatUTM(40.71435, -74.00597), OPT("18N 583960 4507523"), ());
TEST_EQUAL(FormatUTM(-41.28646, 174.77624), OPT("60S 313784 5427057"), ());
TEST_EQUAL(FormatUTM(-33.92487, 18.42406), OPT("34S 261878 6243186"), ());
TEST_EQUAL(FormatUTM(-32.89018, -68.84405), OPT("19S 514586 6360877"), ());
TEST_EQUAL(FormatUTM(64.83778, -147.71639), OPT("6N 466013 7190568"), ());
TEST_EQUAL(FormatUTM(56.79680, -5.00601), OPT("30N 377486 6296562"), ());
TEST_EQUAL(FormatUTM(84, -5.00601), OPT("30N 476594 9328501"), ());
TEST_EQUAL(FormatUTM(50.77535, 6.08389), "32N 294409 5628898", ());
TEST_EQUAL(FormatUTM(40.71435, -74.00597), "18N 583960 4507523", ());
TEST_EQUAL(FormatUTM(-41.28646, 174.77624), "60S 313784 5427057", ());
TEST_EQUAL(FormatUTM(-33.92487, 18.42406), "34S 261878 6243186", ());
TEST_EQUAL(FormatUTM(-32.89018, -68.84405), "19S 514586 6360877", ());
TEST_EQUAL(FormatUTM(64.83778, -147.71639), "6N 466013 7190568", ());
TEST_EQUAL(FormatUTM(56.79680, -5.00601), "30N 377486 6296562", ());
TEST_EQUAL(FormatUTM(84, -5.00601), "30N 476594 9328501", ());
TEST_EQUAL(FormatUTM(12.016469, 188.0), std::nullopt, ());
TEST_EQUAL(FormatUTM(84.644103, 3.000009), "", ()); // Latitude limit exceeded.
TEST_EQUAL(FormatUTM(12.016469, 188.0), "", ());
}
UNIT_TEST(FormatMGRS)
{
TEST_EQUAL(FormatMGRS(42.0, -93.0, 5), OPT("15T WG 00000 49776"), ());
TEST_EQUAL(FormatMGRS(31.77597, 35.23406, 5), OPT("36R YA 11554 17776"), ());
TEST_EQUAL(FormatMGRS(-34.81449, -58.54016, 5), OPT("21H UB 59135 46447"), ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 5), OPT("11S PA 72349 11844"), ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 4), OPT("11S PA 7234 1184"), ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 3), OPT("11S PA 723 118"), ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 2), OPT("11S PA 72 11"), ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 1), OPT("11S PA 7 1"), ());
TEST_EQUAL(FormatMGRS(42.0, -93.0, 5), "15T WG 00000 49776", ());
TEST_EQUAL(FormatMGRS(31.77597, 35.23406, 5), "36R YA 11554 17776", ());
TEST_EQUAL(FormatMGRS(-34.81449, -58.54016, 5), "21H UB 59135 46447", ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 5), "11S PA 72349 11844", ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 4), "11S PA 7234 1184", ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 3), "11S PA 723 118", ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 2), "11S PA 72 11", ());
TEST_EQUAL(FormatMGRS(36.2361322, -115.0820944, 1), "11S PA 7 1", ());
TEST_EQUAL(FormatMGRS(84.644103, 3.000009, 5), std::nullopt, ()); // Some converters generate string "Z AB 31142 05767"
TEST_EQUAL(FormatMGRS(-81.016469, 8.745519, 5), std::nullopt, ()); // Some converters generate string "B BX 51947 87732"
TEST_EQUAL(FormatMGRS(12.016469, 188.0, 2), std::nullopt, ());
TEST_EQUAL(FormatMGRS(84.644103, 3.000009, 5), "", ()); // Some converters generate string "Z AB 31142 05767"
TEST_EQUAL(FormatMGRS(-81.016469, 8.745519, 5), "", ()); // Some converters generate string "B BX 51947 87732"
TEST_EQUAL(FormatMGRS(12.016469, 188.0, 2), "", ());
// Test data from https://s3.amazonaws.com/mgrs.io/mgrsToGeo_WE.txt
TEST_EQUAL(FormatMGRS(0.000005, 0.304981, 5), OPT("31N BA 00000 00000"), ());
TEST_EQUAL(FormatMGRS(4.518520, 48.296629, 5), OPT("39N TF 00000 00000"), ());
TEST_EQUAL(FormatMGRS(9.045438, 50.090125, 5), OPT("39P VL 00000 00000"), ());
TEST_EQUAL(FormatMGRS(63.115494, 91.017708, 5), OPT("46V DR 00000 00000"), ());
TEST_EQUAL(FormatMGRS(40.626644, 137.364687, 5), OPT("53T QF 00000 00000"), ());
TEST_EQUAL(FormatMGRS(0.000005, -4.797048, 5), OPT("30N UF 00000 00000"), ());
TEST_EQUAL(FormatMGRS(0.000005, -0.592324, 5), OPT("30N YF 67993 00000"), ());
TEST_EQUAL(FormatMGRS(0.000005, 6.592333, 5), OPT("32N KF 32007 00000"), ());
TEST_EQUAL(FormatMGRS(0.000005, 54.592333, 5), OPT("40N BF 32007 00000"), ());
TEST_EQUAL(FormatMGRS(76.463950, 58.675211, 5), OPT("40X EK 43763 87577"), ());
TEST_EQUAL(FormatMGRS(-49.578078, 85.150396, 5), OPT("45F UF 66291 06635"), ());
TEST_EQUAL(FormatMGRS(-45.089793, 132.812340, 5), OPT("53G LL 27848 04746"), ());
TEST_EQUAL(FormatMGRS(4.514602, 138.603457, 5), OPT("54N TK 34069 99447"), ());
TEST_EQUAL(FormatMGRS(-67.547521, -142.303616, 5), OPT("07D DF 44443 06997"), ());
TEST_EQUAL(FormatMGRS(-62.908852, -154.887008, 5), OPT("05E ML 04130 23160"), ());
TEST_EQUAL(FormatMGRS(4.514602, -144.603447, 5), OPT("06N YK 65931 99447"), ());
TEST_EQUAL(FormatMGRS(4.514602, -137.396543, 5), OPT("08N KK 34069 99447"), ());
TEST_EQUAL(FormatMGRS(9.028532, -144.637149, 5), OPT("06P YQ 59760 98847"), ());
TEST_EQUAL(FormatMGRS(54.109208, -60.059588, 5), OPT("20U PE 92211 99669"), ());
TEST_EQUAL(FormatMGRS(54.109208, -53.940397, 5), OPT("22U CE 07789 99669"), ());
TEST_EQUAL(FormatMGRS(-45.089793, -53.187660, 5), OPT("22G CR 27848 04746"), ());
TEST_EQUAL(FormatMGRS(-31.596034, -18.161583, 5), OPT("27J YF 69322 00842"), ());
TEST_EQUAL(FormatMGRS(-22.559756, -11.111475, 5), OPT("29K KR 82882 03678"), ());
TEST_EQUAL(FormatMGRS(0.000005, 0.592333, 5), OPT("31N BA 32007 00000"), ());
TEST_EQUAL(FormatMGRS(0.000005, 0.304981, 5), "31N BA 00000 00000", ());
TEST_EQUAL(FormatMGRS(4.518520, 48.296629, 5), "39N TF 00000 00000", ());
TEST_EQUAL(FormatMGRS(9.045438, 50.090125, 5), "39P VL 00000 00000", ());
TEST_EQUAL(FormatMGRS(63.115494, 91.017708, 5), "46V DR 00000 00000", ());
TEST_EQUAL(FormatMGRS(40.626644, 137.364687, 5), "53T QF 00000 00000", ());
TEST_EQUAL(FormatMGRS(0.000005, -4.797048, 5), "30N UF 00000 00000", ());
TEST_EQUAL(FormatMGRS(0.000005, -0.592324, 5), "30N YF 67993 00000", ());
TEST_EQUAL(FormatMGRS(0.000005, 6.592333, 5), "32N KF 32007 00000", ());
TEST_EQUAL(FormatMGRS(0.000005, 54.592333, 5), "40N BF 32007 00000", ());
TEST_EQUAL(FormatMGRS(76.463950, 58.675211, 5), "40X EK 43763 87577", ());
TEST_EQUAL(FormatMGRS(-49.578078, 85.150396, 5), "45F UF 66291 06635", ());
TEST_EQUAL(FormatMGRS(-45.089793, 132.812340, 5), "53G LL 27848 04746", ());
TEST_EQUAL(FormatMGRS(4.514602, 138.603457, 5), "54N TK 34069 99447", ());
TEST_EQUAL(FormatMGRS(-67.547521, -142.303616, 5), "07D DF 44443 06997", ());
TEST_EQUAL(FormatMGRS(-62.908852, -154.887008, 5), "05E ML 04130 23160", ());
TEST_EQUAL(FormatMGRS(4.514602, -144.603447, 5), "06N YK 65931 99447", ());
TEST_EQUAL(FormatMGRS(4.514602, -137.396543, 5), "08N KK 34069 99447", ());
TEST_EQUAL(FormatMGRS(9.028532, -144.637149, 5), "06P YQ 59760 98847", ());
TEST_EQUAL(FormatMGRS(54.109208, -60.059588, 5), "20U PE 92211 99669", ());
TEST_EQUAL(FormatMGRS(54.109208, -53.940397, 5), "22U CE 07789 99669", ());
TEST_EQUAL(FormatMGRS(-45.089793, -53.187660, 5), "22G CR 27848 04746", ());
TEST_EQUAL(FormatMGRS(-31.596034, -18.161583, 5), "27J YF 69322 00842", ());
TEST_EQUAL(FormatMGRS(-22.559756, -11.111475, 5), "29K KR 82882 03678", ());
TEST_EQUAL(FormatMGRS(0.000005, 0.592333, 5), "31N BA 32007 00000", ());
}

View file

@ -13,14 +13,14 @@ using namespace math; // To use constexpr math::pi
using namespace base; // To use base::DegToRad and base::RadToDeg
using namespace ms; // To use ms::LatLon
typedef struct UTMPoint_Value
struct UTMPoint
{
double easting;
double northing;
int zone_number;
char zone_letter;
} UTMPoint;
};
constexpr double K0 = 0.9996; // The scale factor at the central meridian.
constexpr double E = 0.00669438; // The square of the eccentricity for the WGS84 ellipsoid.
@ -484,7 +484,7 @@ std::optional<ms::LatLon> MGRStoLatLon(int easting, int northing, int zone_code,
}
// Convert lat,lon for WGS84 ellipsoid to MGRS string.
optional<string> FormatMGRS(double lat, double lon, int precision)
string FormatMGRS(double lat, double lon, int precision)
{
if (precision > 5)
precision = 5;
@ -492,9 +492,9 @@ optional<string> FormatMGRS(double lat, double lon, int precision)
precision = 1;
if (lat <= -80 || lat > 84)
return nullopt; // Latitude limit exceeded.
return {}; // Latitude limit exceeded.
if (lon <= -180 || lon > 180)
return nullopt; // Longitude limit exceeded.
return {}; // Longitude limit exceeded.
UTMPoint mgrsp = LatLonToUtm(lat, lon);
@ -506,16 +506,16 @@ optional<string> FormatMGRS(double lat, double lon, int precision)
return UTMtoMgrsStr(mgrsp, precision);
}
return nullopt;
return {};
}
// Convert lat,lon for WGS84 ellipsoid to UTM string.
optional<string> FormatUTM(double lat, double lon)
string FormatUTM(double lat, double lon)
{
if (lat <= -80 || lat > 84)
return nullopt; // Latitude limit exceeded.
return {}; // Latitude limit exceeded.
if (lon <= -180 || lon > 180)
return nullopt; // Longitude limit exceeded.
return {}; // Longitude limit exceeded.
UTMPoint utm = LatLonToUtm(lat, lon);
return UTMtoStr(utm);

View file

@ -7,7 +7,7 @@
namespace utm_mgrs_utils
{
// Convert from Lat and Lon coordinates in WGS 84 projection to UTM string
std::optional<std::string> FormatUTM(double lat, double lon);
std::string FormatUTM(double lat, double lon);
/* Convert from Lat and Lon coordinates in WGS 84 projection to MGRS square string
* Parameter prec should be a number from 1 to 5.
@ -17,7 +17,7 @@ std::optional<std::string> FormatUTM(double lat, double lon);
* prec = 4 gives MGRS coordinates "11S PA 7234 1184"
* prec = 5 gives MGRS coordinates "11S PA 72349 11844" (highest precision)
*/
std::optional<std::string> FormatMGRS(double lat, double lon, int prec);
std::string FormatMGRS(double lat, double lon, int prec);
// Covevrt UTM coordinates to Lat Lon. If UTM parameters are invalid function returns false
std::optional<ms::LatLon> UTMtoLatLon(int easting, int northing, int zone_code, char zone_letter);