<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
span.EpostStil17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="NO-BOK" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-GB">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:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">gpkg_reg = "karplanteregistrering.gpkg" # the NEW gpkg to be created<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">if os.path.exists(gpkg_reg): # If gpkg-file already exists, delete it<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> os.remove(gpkg_reg)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""># <a href="https://gis.stackexchange.com/questions/417916/creating-empty-layers-in-a-geopackage-using-pyqgis?noredirect=1&lq=1">
https://gis.stackexchange.com/questions/417916/creating-empty-layers-in-a-geopackage-using-pyqgis?noredirect=1&lq=1</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">def create_blank_gpkg_layer(gpkg_path: str, layer_name: str, geometry: int,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> crs: str, fields: QgsFields, append: bool = False<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> ) -> bool:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> # To add a layer to an existing GPKG file, pass 'append' as True<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> options = QgsVectorFileWriter.SaveVectorOptions()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> options.driverName = "GPKG"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> options.layerName = layer_name<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> if append:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> writer = QgsVectorFileWriter.create(<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> gpkg_path,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> fields,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> geometry,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> QgsCoordinateReferenceSystem(crs),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> QgsCoordinateTransformContext(),<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> options)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> del writer<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> return True<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""># Layer 1 oversikt – creating an empty layer with attributes read from a csv-file<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">f = open("./egenskaper/egenskaper_oversikt.csv",'r') # open file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">f.readline() # read the first line of the file (the "metadata"/heading)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">layer_name = "oversikt" # set layer name<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">print('creates layer', layer_name) # print layer name<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">geom = QgsWkbTypes.PointZ # geometrytype<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">crs = 'epsg:25832' # coordinate system<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">fields = QgsFields()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">for line in f: # Read attrubutes from csv-file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> </span>
<span style="font-family:"Courier New"">r = {}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> r = line.split(';') # 0 navn, 1 QVariant-type, 2 lengde og 3 presisjon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </span><span lang="EN-GB" style="font-family:"Courier New"">fname = r[0]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> l = int(r[2])<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> p = int(r[3])<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> try:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> fields.append(QgsField(fname,set_variant(r[1]),'',l,p)) # add field to layer
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> </span>
<span style="font-family:"Courier New"">except:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> print("Noe gikk galt - får ikke laget egenskaper fra " +str(f))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">f.close() # close file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">create_blank_gpkg_layer(gpkg_reg, layer_name, geom, crs, fields) # Do NOT include True when the FIRST layer is created<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""># Layer 2 arter (to be added to the same new gpkg-file just created above)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">f = open("./egenskaper/egenskaper_arter.csv",'r') # open file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">f.readline()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">layer_name = "arter"
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">print('creates layer', layer_name) # print layer name<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">geom = QgsWkbTypes.PointZ # geometrytype<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">crs = 'epsg:25832' # coordinate system<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">fields = QgsFields()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">for line in f: # Read attrubutes from csv-file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> </span>
<span style="font-family:"Courier New"">r = {}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> r = line.split(';') # 0 navn, 1 QVariant-type, 2 lengde og 3 presisjon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </span><span lang="EN-GB" style="font-family:"Courier New"">fname = r[0]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> l = int(r[2])<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> p = int(r[3])<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> try:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> fields.append(QgsField(fname,set_variant(r[1]),'',l,p)) # add field to layer
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New""> </span>
<span style="font-family:"Courier New"">except:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> print("Noe gikk galt - får ikke laget egenskaper fra " +str(f))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">f.close() # close file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-family:"Courier New"">create_blank_gpkg_layer(gpkg_reg, layer_name, geom, crs, fields, True) # Include True to add a new layer to the SAME gpkg-file<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Note the difference in the use of True and not when calling create_blank_gpk_layer().<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Perhaps something similar may be the solution to your problem.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Kind regards<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Anne<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
</div>
</body>
</html>