3

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

Someone has 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
    Mar 7 '16 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
    Mar 7 '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 7 '16 at 10:18
5

From QGIS 2.14 there is a very simple and straightforward way to to that. Just write None as the typical geometry:

layer = QgsVectorLayer('None', 'table_name', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(layer) 

(In later versions of QGIS replace last line with:

QgsProject.instance().addMapLayer(layer)

)

hope this can help other users

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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