Skip to content

Commit

Permalink
JP2ECW: do not export GMLJP2 if the SRS isn't compatible (fixes #9223)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 19, 2024
1 parent 41aa610 commit 64e43b4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
60 changes: 60 additions & 0 deletions autotest/gdrivers/ecw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,66 @@ def test_ecw_read_uint32_jpeg2000():
)


###############################################################################
# Test unsupported XML SRS


def test_jp2ecw_unsupported_srs_for_gmljp2(tmp_vsimem):

if gdaltest.jp2ecw_drv is None or not has_write_support():
pytest.skip("ECW write support not available")

filename = str(tmp_vsimem / "out.jp2")
# No EPSG code and Albers Equal Area not supported by OGRSpatialReference::exportToXML()
wkt = """PROJCRS["Africa_Albers_Equal_Area_Conic",
BASEGEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]],
CONVERSION["Albers Equal Area",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",25,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",20,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",-23,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["easting",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["northing",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]"""
gdal.ErrorReset()
assert gdal.Translate(filename, "data/byte.tif", outputSRS=wkt, format="JP2ECW")
assert gdal.GetLastErrorMsg() == ""
ds = gdal.Open(filename)
ref_srs = osr.SpatialReference()
ref_srs.ImportFromWkt(wkt)
assert ds.GetSpatialRef().IsSame(ref_srs)
# Check that we do *not* have a GMLJP2 box
assert "xml:gml.root-instance" not in ds.GetMetadataDomainList()


###############################################################################


Expand Down
25 changes: 24 additions & 1 deletion frmts/ecw/ecwcreatecopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,33 @@ CPLErr GDALECWCompressor::Initialize(
const char *pszGMLJP2V2Def =
CSLFetchNameValue(papszOptions, "GMLJP2V2_DEF");
if (pszGMLJP2V2Def != nullptr)
{
WriteJP2Box(oJP2MD.CreateGMLJP2V2(nXSize, nYSize,
pszGMLJP2V2Def, poSrcDS));
}
else
WriteJP2Box(oJP2MD.CreateGMLJP2(nXSize, nYSize));
{
if (poSRS == nullptr || poSRS->IsEmpty() ||
GDALJP2Metadata::IsSRSCompatible(poSRS))
{
WriteJP2Box(oJP2MD.CreateGMLJP2(nXSize, nYSize));
}
else if (CSLFetchNameValue(papszOptions, "GMLJP2"))
{
CPLError(CE_Warning, CPLE_AppDefined,
"GMLJP2 box was explicitly required but "
"cannot be written due "
"to lack of georeferencing and/or unsupported "
"georeferencing "
"for GMLJP2");
}
else
{
CPLDebug(
"JP2ECW",
"Cannot write GMLJP2 box due to unsupported SRS");
}
}
}
if (CPLFetchBool(papszOptions, "GeoJP2", true))
WriteJP2Box(oJP2MD.CreateJP2GeoTIFF());
Expand Down

0 comments on commit 64e43b4

Please sign in to comment.