[Qgis-user] Pyqgis how to append existing gpkg layer
andrea antonello
andrea.antonello at gmail.com
Wed Apr 26 07:10:52 PDT 2023
Hi, from what I saw in the docs, you should be able to append to layers
with the filewriter option AppendToLayerNoNewFields?
The CreateOrOverwriteLayer overwrites the layer completely.
Cheers,
Andrea
On Wed, Apr 26, 2023 at 2:58 PM Anne B. Nilsen via QGIS-User <
qgis-user at lists.osgeo.org> wrote:
> I’m not a pyQGIS-expert, but with help from gis.stackechange.com I have
> managed to create an empty geopackage file and then adding several layers
> to it with this code:
>
> …
>
> gpkg_reg = "karplanteregistrering.gpkg" # the NEW gpkg
> to be created
>
> if os.path.exists(gpkg_reg): # If gpkg-file
> already exists, delete it
>
> os.remove(gpkg_reg)
>
> …
>
> #
> https://gis.stackexchange.com/questions/417916/creating-empty-layers-in-a-geopackage-using-pyqgis?noredirect=1&lq=1
>
> 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
>
> …
>
> # Layer 1 oversikt – creating an empty layer with attributes read from a
> csv-file
>
> f = open("./egenskaper/egenskaper_oversikt.csv",'r') # open file
>
> f.readline() # read the first line of the file (the "metadata"/heading)
>
> layer_name = "oversikt" # set layer name
>
> print('creates layer', layer_name) # print layer name
>
> geom = QgsWkbTypes.PointZ # geometrytype
>
> crs = 'epsg:25832' # coordinate system
>
> fields = QgsFields()
>
> for line in f: # Read attrubutes from csv-file
>
> r = {}
>
> r = line.split(';') # 0 navn, 1 QVariant-type, 2 lengde og 3
> presisjon
>
> fname = r[0]
>
> l = int(r[2])
>
> p = int(r[3])
>
> try:
>
> fields.append(QgsField(fname,set_variant(r[1]),'',l,p)) # add
> field to layer
>
> except:
>
> print("Noe gikk galt - får ikke laget egenskaper fra " +str(f))
>
> f.close() # close file
>
> create_blank_gpkg_layer(gpkg_reg, layer_name, geom, crs, fields) # Do NOT
> include True when the FIRST layer is created
>
>
>
> # Layer 2 arter (to be added to the same new gpkg-file just created above)
>
> f = open("./egenskaper/egenskaper_arter.csv",'r') # open file
>
> f.readline()
>
> layer_name = "arter"
>
> print('creates layer', layer_name) # print layer name
>
> geom = QgsWkbTypes.PointZ # geometrytype
>
> crs = 'epsg:25832' # coordinate system
>
> fields = QgsFields()
>
> for line in f: # Read attrubutes from csv-file
>
> r = {}
>
> r = line.split(';') # 0 navn, 1 QVariant-type, 2 lengde og 3
> presisjon
>
> fname = r[0]
>
> l = int(r[2])
>
> p = int(r[3])
>
> try:
>
> fields.append(QgsField(fname,set_variant(r[1]),'',l,p)) # add
> field to layer
>
> except:
>
> print("Noe gikk galt - får ikke laget egenskaper fra " +str(f))
>
> f.close() # close file
>
> create_blank_gpkg_layer(gpkg_reg, layer_name, geom, crs, fields, True) #
> Include True to add a new layer to the SAME gpkg-file
>
> …
>
>
>
> Note the difference in the use of True and not when calling
> create_blank_gpk_layer().
>
> Perhaps something similar may be the solution to your problem.
>
>
>
> Kind regards
>
> Anne
>
>
>
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230426/85ec7664/attachment-0001.htm>
More information about the QGIS-User
mailing list