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

2020-06-26 Thread Julien Cabieces

It works fine for me too, thanks Alessandro!

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

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-25 Thread Alessandro Pasotti
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
 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
> >  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
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-24 Thread Julien Cabieces

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

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-24 Thread Even Rouault
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
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-24 Thread Alessandro Pasotti
On Wed, Jun 24, 2020 at 2:57 PM Julien Cabieces
 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
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-24 Thread Julien Cabieces

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?

Kind regards,
Julien


> On mardi 23 juin 2020 15:34:58 CEST Raymond Nijssen wrote:
>> My python plugin should create a table without geometry in a gpkg to
>> store some additional (meta) info about the layers. I could use python
>> bindings for sqlite of course, but the plugin users might not have that
>> installed.
>> 
>> What are the options from within qgis? Is python-sqlite available, or
>> can I run sql queries in antother way? Or can I create a table similar
>> to creating a new maplayer?
>> 
>> My (ugly) solution would be creating a point layer and use that
>> attribute table and storing POINT(0 0) as geometries.
>> Or even worse, creating a .csv sidecar file for my gpkg.
>> 
>> Hope anyone here knows a better solution!
>
> Raymond,
>
> You should be able to just use the normal QGIS vector API for that. For 
> example 
> QgsVectorFileWriter.create() with geometryType = NoGeometry
>
> Even

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-23 Thread Raymond Nijssen

Thanks Even, that is it. So easy!

And then you can create features without geometry and add them.

Here's my little test script for those who are looking for the same:

vl = QgsVectorLayer('NoGeometry', 'test', 'memory')
pr = vl.dataProvider()
pr.addAttributes([QgsField("txt", QVariant.String)])
pr.addAttributes([QgsField("num", QVariant.Int)])
vl.updateFields()
QgsProject.instance().addMapLayer(vl)

feat = QgsFeature()
feat.setAttributes(['hi', 3])
pr.addFeatures([feat])


Regards,
Raymond




On 23-06-2020 15:51, Even Rouault wrote:

On mardi 23 juin 2020 15:34:58 CEST Raymond Nijssen wrote:

 > My python plugin should create a table without geometry in a gpkg to

 > store some additional (meta) info about the layers. I could use python

 > bindings for sqlite of course, but the plugin users might not have that

 > installed.

 >

 > What are the options from within qgis? Is python-sqlite available, or

 > can I run sql queries in antother way? Or can I create a table similar

 > to creating a new maplayer?

 >

 > My (ugly) solution would be creating a point layer and use that

 > attribute table and storing POINT(0 0) as geometries.

 > Or even worse, creating a .csv sidecar file for my gpkg.

 >

 > Hope anyone here knows a better solution!

Raymond,

You should be able to just use the normal QGIS vector API for that. For 
example QgsVectorFileWriter.create() with geometryType = NoGeometry


Even

--

Spatialys - Geospatial professional services

http://www.spatialys.com


___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2020-06-23 Thread Even Rouault
On mardi 23 juin 2020 15:34:58 CEST Raymond Nijssen wrote:
> My python plugin should create a table without geometry in a gpkg to
> store some additional (meta) info about the layers. I could use python
> bindings for sqlite of course, but the plugin users might not have that
> installed.
> 
> What are the options from within qgis? Is python-sqlite available, or
> can I run sql queries in antother way? Or can I create a table similar
> to creating a new maplayer?
> 
> My (ugly) solution would be creating a point layer and use that
> attribute table and storing POINT(0 0) as geometries.
> Or even worse, creating a .csv sidecar file for my gpkg.
> 
> Hope anyone here knows a better solution!

Raymond,

You should be able to just use the normal QGIS vector API for that. For example 
QgsVectorFileWriter.create() with geometryType = NoGeometry

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer