7

How can I use the QgsVectorFileWriter to include metadata description for Geopackage layers in a python script?

I can set the layer name using:

options.layerName = "roads_layer"

what I want is something like:

options.description = 'This are the roads covering such and such, Dec 2019".

In QGIS you can right click, export layer to Geopackage and you get the option to set a Description.

enter image description here

Then in QGIS, you get the description showing in the Abstract (baring in mind my examples don't actually match):

enter image description here

I can see that the GDAL Geopackage definition means you can do this, I just haven't been able to find or understand how to do it.

enter image description here https://gdal.org/drivers/vector/gpkg.html

Can anyone help please?

Code:

layObj.setAbstract("Test metadata Dec 2019") # this line doesn't translate to the geopackage table
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
options.layerName = layname
writer = QgsVectorFileWriter.writeAsVectorFormat(layObj, geopP, options)
12
  • I don't know how to implement it in pyqgis, but you need to write the value of the 'description' field, in the 'gpkg_contents' table for the row corresponding to your table, at the GeoPackage cointained database. Dec 7, 2019 at 6:59
  • At least, that is what the DESCRIPTION creation option does. But what you see in the Abstract is a Metadata Property. Dec 8, 2019 at 1:07
  • Thanks but I was hoping to avoid requiring two methods for exporting the data. Pyqgis and perhaps sqlite3. If someone could confirm it's not possible in pyqgis that would be good to know.
    – GIS_py
    Dec 10, 2019 at 10:09
  • Do you want to write the abstract metadata of the layer in the project or the description field of the gpkg_contents table of the layer file? Dec 10, 2019 at 10:13
  • Most important is the description field of the gpkg contents table as it should be permanently present irrespective of software platform. In any case, qgis will read that Description and auto-populate the metadata abstract as I had in my post. I've just tested in ArcMap and it doesn't show in the Layer Description but adding layer info with arcpy is easy.
    – GIS_py
    Dec 10, 2019 at 12:00

1 Answer 1

7
+100

You can use layerOptions to add creation options. layerOptions is a list of strings formated as 'KEY=VALUE'.

Hence, embedding a layer description in the geopackage just comes to adding

options.layerOptions = ["DESCRIPTION=This are the roads covering such and such, Dec 2019"]

Here is a generic function which saves a layer into a geopackage and uses the layer's abstract as description in the geopackage.

def save_with_description(layer, outputfile):
    options = QgsVectorFileWriter.SaveVectorOptions()

    # If geopackage exists, append layer to it, else create it
    if os.path.exists(outputfile):
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
    else:
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile

    # Use input layer abstract and name in the geopackage
    options.layerOptions = [f"DESCRIPTION={layer.abstract()}"]
    options.layerName = layer.name()
    return QgsVectorFileWriter.writeAsVectorFormat(layer, outputfile, options)
1
  • 1
    It works -Fabulous! Many thanks Yoann, I had a feeling it was easy- always struggle to find the documentation for that though
    – GIS_py
    Dec 12, 2019 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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