[Qgis-user] Pyqgis how to append existing gpkg layer

Anne B. Nilsen anne.b.nilsen at nibio.no
Wed Apr 26 06:15:42 PDT 2023


Sorry, an error in the code in the previous answer.
Correct code is (writer should be without indentation):
...
def create_blank_gpkg_layer(gpkg_path: str, layer_name: str, geometry: int,
                            crs: str, fields: QgsFields, append: bool = False
                            ) -> bool:
    # To add a layer to an existing GPKG file, pass 'append' as True
    options = QgsVectorFileWriter.SaveVectorOptions()
    options.driverName = "GPKG"
    options.layerName = layer_name
    if append:
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
    writer = QgsVectorFileWriter.create(
        gpkg_path,
        fields,
        geometry,
        QgsCoordinateReferenceSystem(crs),
        QgsCoordinateTransformContext(),
        options)
    del writer
    return True
...

Kind regards
Anne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230426/aa773798/attachment.htm>


More information about the QGIS-User mailing list