[QGIS-Developer] Pyqgis how to append existing gpkg layer
PIERRE Sylvain
sylvain.pierre at alsace.eu
Thu Apr 20 00:20:15 PDT 2023
Hi Germán,
options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields
Was the key. Thanks !
[cid:image001.jpg at 01D97369.4FF99FA0]
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
sylvain.pierre at alsace.eu<mailto:sylvain.pierre at alsace.eu>
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>
De : Germán Carrillo <carrillo.german at gmail.com>
Envoyé : mercredi 19 avril 2023 17:16
À : PIERRE Sylvain <sylvain.pierre at alsace.eu>
Cc : qgis-user at lists.osgeo.org; qgis-developer at lists.osgeo.org
Objet : Re: [QGIS-Developer] Pyqgis how to append existing gpkg layer
Soyez vigilant : ce courriel provient d'une organisation externe à la CeA.
• Assurez-vous d’abord qu’il ne s’agit pas d’un email malveillant avant de cliquer sur tout lien ou pièce jointe.
Apprendre à reconnaître un email de phishing.<https://intranet.alsace.eu/actualites/apprenez-a-reconnaitre-un-email-phishing/>
Bonjour Sylvain,
J'espère que vous allez bien.
If you want to append features from a source layer to another layer that already exists in a GPKG file, you can do this:
-----------------------------------------------------------------------------------------------------
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = "my_existing_layer_name" # Write here the existing layer name in the GPKG
options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields
options.onlySelectedFeatures = True
path = "/tmp/existing_gpkg_file_path.gpkg"
layer = QgsVectorLayer("/tmp/my_source_layer.shp", "my source layer name", "ogr")
result = QgsVectorFileWriter.writeAsVectorFormatV3(
layer,
path, # Existing GPKG file path
layer.transformContext(),
options)
-----------------------------------------------------------------------------------------------------
If on the contrary, you want to append a whole layer (with its own field structure, crs, etc.) to an existing GPKG database, you can follow this solution:
https://gis.stackexchange.com/a/417950
As you can see, the key is to define the options.actionOnExistingFile property, whose possible values are well documented here:
https://api.qgis.org/api/3.28/classQgsVectorFileWriter.html#afda86eff21ac1da7dc42cbdde424acb1
Note: SInce we're using writeAsVectorFormatV3(), you need at least QGIS v3.20 for this solution to work.
Regards,
Germán
El mié, 19 abr 2023 a las 3:55, PIERRE Sylvain via QGIS-Developer (<qgis-developer at lists.osgeo.org<mailto:qgis-developer at lists.osgeo.org>>) escribió:
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:image005.jpg at 01D97369.4FF99FA0]
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
sylvain.pierre at alsace.eu<mailto:sylvain.pierre at alsace.eu>
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-Developer mailing list
QGIS-Developer at lists.osgeo.org<mailto:QGIS-Developer at lists.osgeo.org>
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
--
-----------
|\__
(:>__)(
|/
Soluciones Geoinformáticas Libres
http://geotux.tuxfamily.org/
https://twitter.com/GeoTux2<http://twitter.com/GeoTux2>
[Image supprimée par l'expéditeur.]<http://gis.stackexchange.com/users/4972/germ%c3%a1n-carrillo>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ~WRD000.jpg
Type: image/jpeg
Size: 823 bytes
Desc: ~WRD000.jpg
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0003.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 8103 bytes
Desc: image001.jpg
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0004.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 827 bytes
Desc: image002.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 853 bytes
Desc: image003.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 1038 bytes
Desc: image004.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.jpg
Type: image/jpeg
Size: 8033 bytes
Desc: image005.jpg
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0005.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.png
Type: image/png
Size: 897 bytes
Desc: image006.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0009.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image007.png
Type: image/png
Size: 850 bytes
Desc: image007.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0010.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image008.png
Type: image/png
Size: 1028 bytes
Desc: image008.png
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230420/8a339852/attachment-0011.png>
More information about the QGIS-Developer
mailing list