Skip to content

Commit

Permalink
gdalinfo/ogrinfo text output: avoid weird truncation of coordinate ep…
Browse files Browse the repository at this point in the history
…och when its value is {year}.0
  • Loading branch information
rouault authored and github-actions[bot] committed Jun 10, 2024
1 parent c7f44e8 commit 6f5a13e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
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 @@ -199,17 +199,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)

0 comments on commit 6f5a13e

Please sign in to comment.