Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdalinfo/ogrinfo text output: avoid weird truncation of coordinate epoch when its value is {year}.0 #10145

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/gdalinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 5 additions & 3 deletions apps/ogrinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
9 changes: 5 additions & 4 deletions autotest/utilities/test_gdalinfo_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


###############################################################################
Expand Down
23 changes: 22 additions & 1 deletion autotest/utilities/test_ogrinfo_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import gdaltest
import pytest

from osgeo import gdal, ogr
from osgeo import gdal, ogr, osr

###############################################################################
# Simple test
Expand Down Expand Up @@ -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)
Loading