diff --git a/apps/gdalinfo_lib.cpp b/apps/gdalinfo_lib.cpp index 8aa00d02b6a7..e357e76e211e 100644 --- a/apps/gdalinfo_lib.cpp +++ b/apps/gdalinfo_lib.cpp @@ -639,10 +639,12 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions) { std::string osCoordinateEpoch = CPLSPrintf("%f", dfCoordinateEpoch); - if (osCoordinateEpoch.find('.') != std::string::npos) + const size_t nDotPos = osCoordinateEpoch.find('.'); + if (nDotPos != std::string::npos) { - while (osCoordinateEpoch.back() == '0') - osCoordinateEpoch.resize(osCoordinateEpoch.size() - 1); + while (osCoordinateEpoch.size() > nDotPos + 2 && + osCoordinateEpoch.back() == '0') + osCoordinateEpoch.pop_back(); } Concat(osStr, psOptions->bStdoutOutput, "Coordinate epoch: %s\n", osCoordinateEpoch.c_str()); diff --git a/apps/ogrinfo_lib.cpp b/apps/ogrinfo_lib.cpp index 9612d6570d7b..42538bc65f6b 100644 --- a/apps/ogrinfo_lib.cpp +++ b/apps/ogrinfo_lib.cpp @@ -1191,10 +1191,12 @@ static void ReportOnLayer(CPLString &osRet, CPLJSONObject &oLayer, { std::string osCoordinateEpoch = CPLSPrintf("%f", dfCoordinateEpoch); - if (osCoordinateEpoch.find('.') != std::string::npos) + const size_t nDotPos = osCoordinateEpoch.find('.'); + if (nDotPos != std::string::npos) { - while (osCoordinateEpoch.back() == '0') - osCoordinateEpoch.resize(osCoordinateEpoch.size() - 1); + while (osCoordinateEpoch.size() > nDotPos + 2 && + osCoordinateEpoch.back() == '0') + osCoordinateEpoch.pop_back(); } Concat(osRet, psOptions->bStdoutOutput, "Coordinate epoch: %s\n", osCoordinateEpoch.c_str()); diff --git a/autotest/utilities/test_gdalinfo_lib.py b/autotest/utilities/test_gdalinfo_lib.py index 8e49b231fd43..ade1b8ab0b5c 100755 --- a/autotest/utilities/test_gdalinfo_lib.py +++ b/autotest/utilities/test_gdalinfo_lib.py @@ -201,17 +201,18 @@ def test_gdalinfo_lib_nodatavalues(): ############################################################################### -def test_gdalinfo_lib_coordinate_epoch(): +@pytest.mark.parametrize("epoch", ["2021.0", "2021.3"]) +def test_gdalinfo_lib_coordinate_epoch(epoch): ds = gdal.Translate( - "", "../gcore/data/byte.tif", options='-of MEM -a_coord_epoch 2021.3"' + "", "../gcore/data/byte.tif", options=f'-of MEM -a_coord_epoch {epoch}"' ) ret = gdal.Info(ds) - assert "Coordinate epoch: 2021.3" in ret + assert f"Coordinate epoch: {epoch}" in ret ret = gdal.Info(ds, format="json") assert "coordinateEpoch" in ret - assert ret["coordinateEpoch"] == 2021.3 + assert ret["coordinateEpoch"] == float(epoch) ############################################################################### diff --git a/autotest/utilities/test_ogrinfo_lib.py b/autotest/utilities/test_ogrinfo_lib.py index 055e69ed2c40..876f75e935ff 100755 --- a/autotest/utilities/test_ogrinfo_lib.py +++ b/autotest/utilities/test_ogrinfo_lib.py @@ -33,7 +33,7 @@ import gdaltest import pytest -from osgeo import gdal, ogr +from osgeo import gdal, ogr, osr ############################################################################### # Simple test @@ -637,3 +637,24 @@ def test_ogrinfo_lib_layers(): with pytest.raises(Exception, match="Couldn't fetch requested layer"): gdal.VectorInfo(ds, format="json", layers=["invalid"]) + + +############################################################################### + + +@pytest.mark.parametrize("epoch", ["2021.0", "2021.3"]) +def test_ogrinfo_lib_coordinate_epoch(epoch): + + ds = gdal.GetDriverByName("Memory").Create("dummy", 0, 0, 0, gdal.GDT_Unknown) + srs = osr.SpatialReference() + srs.ImportFromEPSG(4326) + srs.SetCoordinateEpoch(float(epoch)) + ds.CreateLayer("foo", srs=srs) + + ret = gdal.VectorInfo(ds) + assert f"Coordinate epoch: {epoch}" in ret + + j = gdal.VectorInfo(ds, format="json") + crs = j["layers"][0]["geometryFields"][0]["coordinateSystem"] + assert "coordinateEpoch" in crs + assert crs["coordinateEpoch"] == float(epoch)