Skip to content

Commit

Permalink
gdalinfo -json/gdal.Info(format='json'): avoid error/exception on eng…
Browse files Browse the repository at this point in the history
…ineering CRS (fixes OSGeo#9396)
  • Loading branch information
rouault committed Mar 6, 2024
1 parent 70e50ac commit 02b2eb8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
23 changes: 14 additions & 9 deletions apps/gdalinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,20 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions)
{
OGRSpatialReferenceH hLatLong = nullptr;

OGRErr eErr = OGRERR_NONE;
// Check that it looks like Earth before trying to reproject to wgs84...
if (bJson &&
fabs(OSRGetSemiMajor(hProj, &eErr) - 6378137.0) < 10000.0 &&
eErr == OGRERR_NONE)
{
bTransformToWGS84 = true;
hLatLong = OSRNewSpatialReference(nullptr);
OSRSetWellKnownGeogCS(hLatLong, "WGS84");
if (bJson)
{
// Check that it looks like Earth before trying to reproject to wgs84...
// OSRGetSemiMajor() may raise an error on CRS like Engineering CRS
CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler);
CPLErrorStateBackuper oCPLErrorHandlerPusher;
OGRErr eErr = OGRERR_NONE;
if (fabs(OSRGetSemiMajor(hProj, &eErr) - 6378137.0) < 10000.0 &&
eErr == OGRERR_NONE)
{
bTransformToWGS84 = true;
hLatLong = OSRNewSpatialReference(nullptr);
OSRSetWellKnownGeogCS(hLatLong, "WGS84");
}
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions autotest/utilities/test_gdalinfo_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,32 @@ def test_gdalinfo_lib_json_proj_shape():
ds = gdal.GetDriverByName("MEM").Create("", width, height)
ret = gdal.Info(ds, options="-json")
assert ret["stac"]["proj:shape"] == [height, width]


###############################################################################
# Test fix for https://github.com/OSGeo/gdal/issues/9396


def test_gdalinfo_lib_json_engineering_crs():

ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
srs = osr.SpatialReference()
srs.SetFromUserInput(
"""ENGCRS["Arbitrary (m)",
EDATUM["Unknown engineering datum"],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]"""
)
ds.SetSpatialRef(srs)
ds.SetGeoTransform([0, 1, 0, 0, 0, 1])
ret = gdal.Info(ds, format="json")
assert "coordinateSystem" in ret
assert "cornerCoordinates" in ret
assert "wgs84Extent" not in ret

0 comments on commit 02b2eb8

Please sign in to comment.