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?

3
  • 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
    Commented Mar 7, 2016 at 9: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
    Commented Mar 7, 2016 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
    Commented Mar 7, 2016 at 10:18

2 Answers 2

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) 
3
  • +1, very simply indeed! Is there any documentation or post which mentions this?
    – Joseph
    Commented Mar 7, 2016 at 15:05
  • I just asked in the dev mailing list.. so I think no documentation is available (at the moment)
    – matteo
    Commented Mar 7, 2016 at 15:13
  • 1
    Works for the latest QGIS 3 release.
    – Spacedman
    Commented Feb 24, 2018 at 0:02
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()
5
  • I tried your code, but, QgsMapLayerRegistry.instance().addMapLayer(layer) does not load anything on the map.
    – matteo
    Commented Mar 7, 2016 at 10:35
  • @matteo - Apologies, I've edited the post as there was a mistake in the code. Should work now =)
    – Joseph
    Commented Mar 7, 2016 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
    Commented Mar 7, 2016 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
    Commented Mar 7, 2016 at 10:53
  • 1
    ok thanks! I will ask to some developer and update the question ASAP
    – matteo
    Commented Mar 7, 2016 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.