TransWikia.com

How to export layer with metadata in QGIS?

Geographic Information Systems Asked on December 29, 2021

I have set some metadata for my layer that I would like to export. I export the layer using the following:

_writer = QgsVectorFileWriter.writeAsVectorFormat(layer,"path/layer.shp",'utf-8',driverName='ESRI Shapefile')

However the saved layer does not contain the metadata. How can I export the layer and keep the metadata?

One Answer

There are no built-in functions to achieve what you want. Get the reference to the QgsLayerMetadata object from this answer How to get QGIS layer information saved in JSON format? and as stated in comment, write your own metadata as JSON in a separated file.

Alternatively, you can write the layer definition with something like below. It will contains several information including metadata in an XML file


layer = iface.activeLayer()
# mylayer.qlr is hard coded here but it could be determined from "layer.dataProvider().dataSourceUri()"
# if it's a filename or the layer name "layer.name()" or layer id "layer.id()"
with open('mylayer.qlr', 'w') as f:
    f.write(QgsLayerDefinition.exportLayerDefinitionLayers([layer], QgsReadWriteContext()).toString())


Answered by ThomasG77 on December 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP