[QGIS-Developer] Create plain table (without geometry) in gpkg?

Alessandro Pasotti apasotti at gmail.com
Wed Jun 24 06:08:55 PDT 2020


On Wed, Jun 24, 2020 at 2:57 PM Julien Cabieces
<julien.cabieces at oslandia.com> wrote:
>
>
> Hi alls,
>
> I was looking to create an empty GPKG layer and try the solution Even provide, but
> it creates an invalid file.
>
> fields=QgsFields()
> fields.append(QgsField("note", QVariant.Double))
> QgsVectorFileWriter.create("/tmp/test.gpkg", fields, QgsWkbTypes.MultiLineString, QgsProject.instance().crs(), QgsCoordinateTransformContext(), QgsVectorFileWriter.SaveVectorOptions())
>
> I try also:
> lyr = "mylayer"
> QgsVectorFileWriter.create("/tmp/again.gpkg", fields, QgsWkbTypes.MultiLineString, QgsProject.instance().crs(), QgsCoordinateTransformContext(), QgsVectorFileWriter.SaveVectorOptions(), QgsFeatureSink.SinkFlags(), None, lyr)
>
> It looks like there is no layer in the produced GPKG. Is it an issue, or am I misunderstanding the use of QgsVectorFileWriter?

Hi Julien,

I believe you need to add at least one feature to trigger the layer
creation (through QgsVectorFileWriter/GDAL).

You may try QgsAbstractDatabaseProviderConnection::createVectorTable
or QgsProviderRegistry::createEmptyLayer (they both use the same
provider's code)..

Hope it helps.

-- 
Alessandro Pasotti
QCooperative:  www.qcooperative.net
ItOpen:   www.itopen.it


More information about the QGIS-Developer mailing list