6

I'm writing a QGIS plugin and I need to create a table with some attributes that, in a second moment, I'll load in a SpatiaLite database.

The table is created as a memory layer, but as I said, it is just an attribute table, so the features don't have any geometric requirements.

I'm not able to create a memory layer without geometry. Is there a way to do that?

From the cookbook it seems it is not possible to create a geometryless memory layer.

I thought it was possible to set the geometry after the layer creation using a combination of QgsGeometry and from Wkb with a specific option activated.

But I have not been able to do that.

Does someone have some suggestions?

Taras
  • 32,823
  • 4
  • 66
  • 137
matteo
  • 3,304
  • 1
  • 23
  • 47
  • I do not know how to create an empty layer that only holds attributes without geometries. But as a workaround you could maybe try to create en empty csv oder xls file and then import it into the project using python. Maybe that's not the most convenient way, but maybe it could be sufficient enough to fit your needs. At least until you find a better solution. – TobsenB Mar 07 '16 at 09:50
  • mmm this workaround cannot fit with my issue (my fault, did not explain the whole process of the plugin).. actually the memory layer is a result of a QTableWidget.. – matteo Mar 07 '16 at 10:11
  • I guess as soon as you have the layer created using that workaround you could then populate it with the desired output data. But I admit that that's not the most comfortable solution. – TobsenB Mar 07 '16 at 10:18

2 Answers2

10

Since QGIS 2.14 there is a simple way to do that. See the QGIS API documentation for more details.

Just write None as the path= parameter of the QgsVectorLayer:

from qgis.core import QgsVectorLayer, QgsProject

layer = QgsVectorLayer(path='None', baseName='table_name', providerLib='memory') QgsProject.instance().addMapLayer(layer)

In PyQGIS 2:

QgsMapLayerRegistry.instance().addMapLayer(layer)

Taras
  • 32,823
  • 4
  • 66
  • 137
matteo
  • 3,304
  • 1
  • 23
  • 47
1

I haven't really worked with plugins so not sure this solution would apply in your situation but I use the following code to create a geometry-less polygon memory layer:

from PyQt4.QtCore import *

# Create memory layer
layer = QgsVectorLayer("Polygon?crs=epsg:4326", "Table", "memory")
QgsMapLayerRegistry.instance().addMapLayer(layer)

# Begin editing memory layer and create 3 fields
layer.startEditing()
provider = layer.dataProvider()
provider.addAttributes([QgsField("Name", QVariant.String),QgsField("Area", QVariant.Int),QgsField("Size", QVariant.Double)])
layer.updateFields()

# Add a feature with attributes (and without geometry) to populate the 3 fields
attr = layer.dataProvider()
feat = QgsFeature()
feat.setAttributes(["UK", 151, 33.33])
attr.addFeatures([feat])

layer.commitChanges()
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • I tried your code, but, QgsMapLayerRegistry.instance().addMapLayer(layer) does not load anything on the map. – matteo Mar 07 '16 at 10:35
  • @matteo - Apologies, I've edited the post as there was a mistake in the code. Should work now =) – Joseph Mar 07 '16 at 10:40
  • Ok managed the error, but the problem is still there. Typing layer.wkbType() the result is 3 (Polygon as expected). Same thing with layer.geometryType() that results 2 (Polygon). My question is if it is possible to set layer with, for example, wkbType() == 100, that is data only layer.. Thanks Joseph! – matteo Mar 07 '16 at 10:41
  • @matteo - Sorry buddy but not sure how to achieve this. Probably the closest post I could find which is somewhat related is this: How to create/add features without geometry? but that's not through PyQGIS. Hopefully others will advise, interesting to see if this is doable! – Joseph Mar 07 '16 at 10:53
  • 1
    ok thanks! I will ask to some developer and update the question ASAP – matteo Mar 07 '16 at 11:12