diff --git a/autotest/gdrivers/jp2openjpeg.py b/autotest/gdrivers/jp2openjpeg.py
index 8ecddf3bd252..51f93b4848aa 100755
--- a/autotest/gdrivers/jp2openjpeg.py
+++ b/autotest/gdrivers/jp2openjpeg.py
@@ -3864,3 +3864,62 @@ def test_jp2openjpeg_limit_resolution_count_from_image_size(tmp_vsimem):
# Check number of resolutions
ret = gdal.GetJPEG2000StructureAsString(filename, ["ALL=YES"])
assert '2' in ret
+
+
+###############################################################################
+# Test unsupported XML SRS
+
+
+def test_jp2openjpeg_unsupported_srs_for_gmljp2(tmp_vsimem):
+
+ 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="JP2OpenJPEG"
+ )
+ 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()
diff --git a/frmts/opjlike/jp2opjlikedataset.cpp b/frmts/opjlike/jp2opjlikedataset.cpp
index f044b0644f9e..ccef0e8a0ec6 100644
--- a/frmts/opjlike/jp2opjlikedataset.cpp
+++ b/frmts/opjlike/jp2opjlikedataset.cpp
@@ -2471,10 +2471,18 @@ GDALDataset *JP2OPJLikeDataset::CreateCopy(
{
bGeoreferencingCompatOfGeoJP2 = TRUE;
oJP2MD.SetGeoTransform(adfGeoTransform);
+ if (poSRS != nullptr && !poSRS->IsEmpty())
+ {
+ bGeoreferencingCompatOfGMLJP2 =
+ GDALJP2Metadata::IsSRSCompatible(poSRS);
+ if (!bGeoreferencingCompatOfGMLJP2)
+ {
+ CPLDebug(
+ CODEC::debugId(),
+ "Cannot write GMLJP2 box due to unsupported SRS");
+ }
+ }
}
- bGeoreferencingCompatOfGMLJP2 =
- poSRS != nullptr && !poSRS->IsEmpty() &&
- poSrcDS->GetGeoTransform(adfGeoTransform) == CE_None;
}
if (poSrcDS->GetMetadata("RPC") != nullptr)
{