Skip to content

Commit

Permalink
JP2OpenJPEG: do not export GMLJP2 if the SRS isn't compatible (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 26, 2024
1 parent 4e2f3a8 commit 61d7268
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
59 changes: 59 additions & 0 deletions autotest/gdrivers/jp2openjpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<Field name="SPcod_NumDecompositions" type="uint8">2</Field>' in ret


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


def test_jp2openjpeg_unsupported_srs_for_gmljp2(tmp_vsimem):

filename = str(tmp_vsimem / "out.jp2")
# There is no EPSG code and Albers Equal Area is 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()
16 changes: 12 additions & 4 deletions frmts/opjlike/jp2opjlikedataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ GDALDataset *JP2OPJLikeDataset<CODEC, BASE>::CreateCopy(
else
{
const OGRSpatialReference *poSRS = poSrcDS->GetSpatialRef();
if (poSRS != nullptr)
if (poSRS)
{
bGeoreferencingCompatOfGeoJP2 = TRUE;
oJP2MD.SetSpatialRef(poSRS);
Expand All @@ -2471,10 +2471,18 @@ GDALDataset *JP2OPJLikeDataset<CODEC, BASE>::CreateCopy(
{
bGeoreferencingCompatOfGeoJP2 = TRUE;
oJP2MD.SetGeoTransform(adfGeoTransform);
if (poSRS && !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)
{
Expand Down

0 comments on commit 61d7268

Please sign in to comment.