Geographic Information Systems Asked on January 17, 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?
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 January 17, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP