Hi dev and users
I don't understand how to append an existing gpkg layer with pyqgis.
I've tried many QgsVectorFileWriter options
self.options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
reset final layer
And
self.options.EditionCapability = QgsVectorFileWriter.CanAppendToExistingLayer
does not change anything
So what's the good way to do this ?
This is my code (I'm iterating over several other data sources)
self.options = QgsVectorFileWriter.SaveVectorOptions()
self.options.driverName = 'GPKG'
self.options.onlySelectedFeatures = True
self.options.layerName = 'parcelles'
for dir in dirs:
print(dir)
if os.path.isdir(dir):
db_GPKG = os.path.join(dir, 'MAEC2023.gpkg')
if os.path.exists(db_GPKG):
gpkg_layer = db_GPKG + "|layername=parcelles"
RPG_layer = QgsVectorLayer(gpkg_layer, "RPG", "ogr")
expr = (' "fk_mesure" is not null ')
#expr = ('"fk_mesure" = \'{}\'').format('MAEC Papillons')
RPG_layer.selectByExpression(expr)
selected_feature = RPG_layer.selectedFeatures()
print(len(selected_feature))
if os.path.exists(self.db_GPKG): # if the ouput file
already exist
self.options.actionOnExistingFile =
QgsVectorFileWriter.CreateOrOverwriteLayer
#self.options.actionOnExistingFile =
QgsVectorFileWriter.AppendToLayerNoNewFields
#self.options.actionOnExistingFile =
QgsVectorFileWriter.AppendToLayerAddFields
self.options.EditionCapability =
QgsVectorFileWriter.CanAppendToExistingLayer
else:
self.options.actionOnExistingFile =
QgsVectorFileWriter.CreateOrOverwriteFile
write_result, error_message =
QgsVectorFileWriter.writeAsVectorFormatV2(RPG_layer, self.db_GPKG ,
self.context, self.options)
Thanks
[cid:[email protected]]
Sylvain PIERRE
Chef de projet système d'information
Direction des Systèmes d'Information et du Développement Numérique
Service Projets et Ingénierie Numérique
Collectivité européenne d'Alsace
Tél : 03 88 76 68 88
[email protected]<mailto:[email protected]>
www.alsace.eu<http://www.alsace.eu>
[facebook]<http://www.facebook.com/toutelalsace> [twitter]
<http://www.twitter.com/toutelalsace> [insta]
<http://www.instagram.com/toutelalsace>
_______________________________________________
QGIS-User mailing list
[email protected]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user