Skip to content

Commit

Permalink
Add COPY_SRC_MDD=AUTO/YES/NO and SRC_MDD=domain_name creation options
Browse files Browse the repository at this point in the history
Fixes OSGeo#7956

    - .. co:: COPY_SRC_MDD
         :choices: AUTO, YES, NO
         :default: AUTO
         :since: 3.8

      Defines if metadata domains of the source dataset should be copied to the
      destination dataset.
      In the default AUTO mode, only "safe" domains will be copied, which
      include the default metadata domain (some drivers may include other
      domains such as IMD, RPC, GEOLOCATION).
      When setting YES, all domains will be copied (but a few reserved ones like
      IMAGE_STRUCTURE or DERIVED_SUBDATASETS).
      Currently only recognized by the GTiff, COG, VRT, PNG and JPEG drivers.

      When setting NO, no source metadata will be copied.

    - .. co:: SRC_MDD
         :choices: <domain_name>
         :since: 3.8

      Defines which source metadata domain should be copied.
      This option restricts the list of source metadata domains to be copied
      (it implies COPY_SRC_MDD=YES if it is not set). This option may be specified
      as many times as they are source domains. The default metadata domain is the
      empty string "" ("_DEFAULT_") may also be used when empty string is not practical).
      Currently only recognized by the GTiff, COG, VRT, PNG and JPEG drivers.
  • Loading branch information
rouault committed Oct 2, 2023
1 parent 3a0ddea commit b19406d
Show file tree
Hide file tree
Showing 12 changed files with 2,517 additions and 1,437 deletions.
1,230 changes: 827 additions & 403 deletions autotest/gcore/cog.py

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions autotest/gcore/tiff_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -10255,5 +10255,64 @@ def test_tiff_write_createcopy_alpha_not_in_last_band(options):
gdal.GetDriverByName("GTiff").Delete(tmpfilename)


###############################################################################
def test_tiff_write_copy_mdd():

src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetMetadataItem("FOO", "BAR")
src_ds.SetMetadataItem("BAR", "BAZ", "OTHER_DOMAIN")
src_ds.SetMetadataItem("should_not", "be_copied", "IMAGE_STRUCTURE")

filename = "/vsimem/test_tiff_write_copy_mdd.tif"

gdal.GetDriverByName("GTiff").CreateCopy(filename, src_ds)
ds = gdal.Open(filename)
assert set(ds.GetMetadataDomainList()) == set(
["", "IMAGE_STRUCTURE", "DERIVED_SUBDATASETS"]
)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {}
assert ds.GetMetadata_Dict("IMAGE_STRUCTURE") == {"INTERLEAVE": "BAND"}
ds = None

gdal.GetDriverByName("GTiff").CreateCopy(
filename, src_ds, options=["COPY_SRC_MDD=NO"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {}
ds = None

gdal.GetDriverByName("GTiff").CreateCopy(
filename, src_ds, options=["COPY_SRC_MDD=YES"]
)
ds = gdal.Open(filename)
assert set(ds.GetMetadataDomainList()) == set(
["", "IMAGE_STRUCTURE", "DERIVED_SUBDATASETS", "OTHER_DOMAIN"]
)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
assert ds.GetMetadata_Dict("IMAGE_STRUCTURE") == {"INTERLEAVE": "BAND"}
ds = None

gdal.GetDriverByName("GTiff").CreateCopy(
filename, src_ds, options=["SRC_MDD=OTHER_DOMAIN"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
ds = None

gdal.GetDriverByName("GTiff").CreateCopy(
filename, src_ds, options=["SRC_MDD=", "SRC_MDD=OTHER_DOMAIN"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
ds = None

gdal.Unlink(filename)


def test_tiff_write_cleanup():
gdaltest.tiff_drv = None
62 changes: 62 additions & 0 deletions autotest/gcore/vrtmisc.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,65 @@ def test_vrtmisc_serialize_complexsource_with_NODATA():

print(content)
assert "<NODATA>1</NODATA>" in content


###############################################################################
def test_vrt_write_copy_mdd():

src_filename = "/vsimem/test_vrt_write_copy_mdd.tif"
src_ds = gdal.GetDriverByName("GTiff").Create(src_filename, 1, 1)
src_ds.SetMetadataItem("FOO", "BAR")
src_ds.SetMetadataItem("BAR", "BAZ", "OTHER_DOMAIN")
src_ds.SetMetadataItem("should_not", "be_copied", "IMAGE_STRUCTURE")

filename = "/vsimem/test_vrt_write_copy_mdd.vrt"

gdal.GetDriverByName("VRT").CreateCopy(filename, src_ds)
ds = gdal.Open(filename)
assert set(ds.GetMetadataDomainList()) == set(
["", "IMAGE_STRUCTURE", "DERIVED_SUBDATASETS"]
)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {}
assert ds.GetMetadata_Dict("IMAGE_STRUCTURE") == {"INTERLEAVE": "BAND"}
ds = None

gdal.GetDriverByName("VRT").CreateCopy(
filename, src_ds, options=["COPY_SRC_MDD=NO"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {}
ds = None

gdal.GetDriverByName("VRT").CreateCopy(
filename, src_ds, options=["COPY_SRC_MDD=YES"]
)
ds = gdal.Open(filename)
assert set(ds.GetMetadataDomainList()) == set(
["", "IMAGE_STRUCTURE", "DERIVED_SUBDATASETS", "OTHER_DOMAIN"]
)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
assert ds.GetMetadata_Dict("IMAGE_STRUCTURE") == {"INTERLEAVE": "BAND"}
ds = None

gdal.GetDriverByName("VRT").CreateCopy(
filename, src_ds, options=["SRC_MDD=OTHER_DOMAIN"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
ds = None

gdal.GetDriverByName("VRT").CreateCopy(
filename, src_ds, options=["SRC_MDD=", "SRC_MDD=OTHER_DOMAIN"]
)
ds = gdal.Open(filename)
assert ds.GetMetadata_Dict() == {"FOO": "BAR"}
assert ds.GetMetadata_Dict("OTHER_DOMAIN") == {"BAR": "BAZ"}
ds = None

src_ds = None
gdal.Unlink(filename)
gdal.Unlink(src_filename)
Loading

0 comments on commit b19406d

Please sign in to comment.