2

I want to feed metadata (title, attribution, etc) to a lot of raster layers in a QGIS project. From what I have read, I managed fine to get hold of metadata from the QGIS python console, using simple code like:

my_layer= QgsProject.instance().mapLayersByName('layer_of_interest')[0]
my_layer_title = my_layer.metadata().title()
my_layer_rights = my_layer.metadata().rights()

I can change the name of the layer (with immediate effect) like this:

my_layer.setName("New layer name")

So, I figured could do something similar to update metadata, so I tried:

my_layer.metadat().setTitle("New title")

which threw no errors nor warnings, but it didn't change the metadata at all, at least nothing I could see from the QGIS GUI. Does anybody know how to do this, or could point me in the right direction?

1 Answer 1

1

Find the solution myself. Some more trials showed me that the solution to this is a two-step process. 1) feed de new metadata to "layers metadata" object and then do a set metadata command to the layer object, like so:

# Retrieve layer and metadata objects
my_layer= QgsProject.instance().mapLayersByName('layer_of_interest')[0]
my_layer_meta = my_layer.metadata()

# 1. Do the metadata updating as needed
my_layer_meta.setTitle("New title")
my_layer_meta.setRights(
    ["First attribution right", "Second attribution right"]
)
my_layer_meta.setParentIdentifier("a parent identifier")
my_layer_meta.setIdentifier("an identifier")
my_layer_meta.setType("a type")
my_layer_meta.setLanguage("a language")
my_layer_meta.setAbstract("an abstract")

# 2. "commit" updated metadata
my_layer.setMetadata(my_layer_meta)

Then I get the new metadata updated at QGIS GUI as can be seen in layer properties. Done!

Documentation :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.