From svn_mapguide at osgeo.org Mon Jan 4 09:03:17 2021 From: svn_mapguide at osgeo.org (svn_mapguide at osgeo.org) Date: Mon, 4 Jan 2021 09:03:17 -0800 Subject: [mapguide-commits] r9826 - in trunk/MgDev: Common/Foundation/System Server/src/UnitTesting Message-ID: <20210104170317.650D316493F@trac.osgeo.org> Author: jng Date: 2021-01-04 09:03:16 -0800 (Mon, 04 Jan 2021) New Revision: 9826 Modified: trunk/MgDev/Common/Foundation/System/Util.cpp trunk/MgDev/Server/src/UnitTesting/TestMisc.cpp Log: Fix incorrect trailing decimal 0s truncation in MgUtil::DoubleToString, exposed through converting certain geometry coordinates to GeoJSON. Test cases added to catch this. Fixes #2832 Modified: trunk/MgDev/Common/Foundation/System/Util.cpp =================================================================== --- trunk/MgDev/Common/Foundation/System/Util.cpp 2020-12-09 11:35:29 UTC (rev 9825) +++ trunk/MgDev/Common/Foundation/System/Util.cpp 2021-01-04 17:03:16 UTC (rev 9826) @@ -1111,6 +1111,11 @@ // Slice off trailing 0s size_t end = str.find_last_not_of('0') + 1; str.erase(end); + // If this means we sliced off *all* decimal places, slice off the decimal point as well + if (str.back() == '.') + { + str.erase(str.length() - 1); + } } } @@ -1128,6 +1133,11 @@ // Slice off trailing 0s size_t end = str.find_last_not_of(L'0') + 1; str.erase(end); + // If this means we sliced off *all* decimal places, slice off the decimal point as well + if (str.back() == '.') + { + str.erase(str.length() - 1); + } } } Modified: trunk/MgDev/Server/src/UnitTesting/TestMisc.cpp =================================================================== --- trunk/MgDev/Server/src/UnitTesting/TestMisc.cpp 2020-12-09 11:35:29 UTC (rev 9825) +++ trunk/MgDev/Server/src/UnitTesting/TestMisc.cpp 2021-01-04 17:03:16 UTC (rev 9826) @@ -841,6 +841,19 @@ REQUIRE(L"123.34574834" == ws); REQUIRE("123.34574834" == s); + + // Brought about by: https://trac.osgeo.org/mapguide/ticket/2832 + // impl: Should std::setprecision be rounding in such a case? + double d3 = 486772.999999999; + + ws.clear(); + s.clear(); + + MgUtil::DoubleToString(d3, ws, 7); + MgUtil::DoubleToString(d3, s, 7); + + REQUIRE(L"486773" == ws); + REQUIRE("486773" == s); } catch (MgException* e) { @@ -1197,4 +1210,34 @@ { throw; } +} + +TEST_CASE("GeoJsonRounding", "[Misc]") +{ + try + { + Ptr gjw = new MgGeoJsonWriter(); + Ptr wktRw = new MgWktReaderWriter(); + //STRING wkt = L"POLYGON ((486750.766 5784456.011, 486728.359 5784446.655, 486706.876 5784437.296, 486690.777 5784429.9, 486676.834 5784422.189, 486659.232 5784411.444, 486646.087 5784401.948, 486648.067 5784395.8, 486651.508 5784385.121, 486652.819 5784381.05, 486655.289 5784373.384, 486657.158 5784367.581, 486667.279 5784336.166, 486668.836 5784331.334, 486672.166 5784320.995, 486679.091 5784299.499, 486680.004 5784296.665, 486657.192 5784287.844, 486652.536 5784286.043, 486650.669 5784285.322, 486652.693 5784279.674, 486646.319 5784277.197, 486607.544 5784262.129, 486611.782 5784254.856, 486619.71 5784241.874, 486641.348 5784265.104, 486664.672 5784243.409, 486693.607 5784248.872, 486704.871 5784250.998, 486706.382 5784246.318, 486711.838 5784229.225, 486715.044 5784219.176, 486735.317 5784222.052, 486746.793 5784224.479, 486746.643 5784225.14, 486753.34 5784226.848, 486760.007 5784228.698, 486766.554 5784230.807, 486772.999999999 5784233.246, 486779.257 5784236.075, 4867 85.374 5784239.294, 486797.129 5784246.463, 486802.768 5784250.362, 486813.835 5784258.681, 486814.204 5784258.151, 486825.248 5784266.724, 486827.881 5784268.769, 486828.191 5784268.399, 486846.766 5784284.326, 486847.798 5784285.214, 486835.598 5784319.835, 486820.899 5784364.066, 486835.747 5784374.205, 486823.91 5784435.848, 486846.995 5784450.096, 486846.814 5784457.889, 486846.658 5784464.636, 486846.617 5784468.126, 486846.57 5784470.327, 486846.732 5784481.668, 486846.22 5784483.886, 486843.72 5784484.496, 486841.391 5784484.935, 486820.463 5784478.249, 486799.031 5784472.704, 486775.631 5784464.912, 486750.766 5784456.011))"; + + STRING wkt = L"POINT (5784230.807 486772.999999999)"; + + Ptr geom = wktRw->Read(wkt); + STRING jsonStr; + gjw->SetPrecision(7); + gjw->SetPrecisionEnabled(true); + gjw->ToGeoJson(geom, jsonStr); + + REQUIRE(L"\"geometry\": { \"type\": \"Point\", \"coordinates\": [5784230.807, 486773]}" == jsonStr); + } + catch (MgException* e) + { + STRING message = e->GetDetails(TestServiceFactory::TEST_LOCALE); + SAFE_RELEASE(e); + FAIL(MG_WCHAR_TO_CHAR(message.c_str())); + } + catch (...) + { + throw; + } } \ No newline at end of file