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

Even Rouault even.rouault at spatialys.com
Wed Jun 24 06:20:54 PDT 2020


Julien,

> 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?

The issue is likely QgsVectorFileWriter.SaveVectorOptions() with no driverName set. There's 
no extension based guessing. Probably that the SaveVectorOptions inner class could have a 
"GPKG" default value for driverName, as it is the default driver name in other methods of 
qgsvectorfilewriter.h

There are some usage examples of QgsVectorFileWriter in
https://github.com/qgis/QGIS/blob/master/tests/src/python/test_qgsvectorfilewriter.py  
(not of create() though)

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200624/866456b2/attachment.html>


More information about the QGIS-Developer mailing list