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

Alessandro Pasotti apasotti at gmail.com
Thu Jun 25 02:24:17 PDT 2020


This works fine for me:

md = QgsProviderRegistry.instance().providerMetadata('ogr')
conn = md.createConnection('/tmp/test_no_geometry.gpkg', {})
conn.createVectorTable('', 'my_table', QgsFields(),
QgsWkbTypes.NoGeometry, QgsCoordinateReferenceSystem(), True, {})
layer = QgsVectorLayer('/tmp/test_no_geometry.gpkg', 'pg', 'ogr')
layer.isValid() --> True
layer.geometryType() --> 4 == NullGeometry


On Wed, Jun 24, 2020 at 5:28 PM Julien Cabieces
<julien.cabieces at oslandia.com> wrote:
>
>
> Thanks Alessandro and Even for your replies,
>
> I try your suggestions but I still get the error. I opened an issue https://github.com/qgis/QGIS/issues/37386
>
> Kind regards,
> Julien
>
>
> > 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