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?
-
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– TobsenB2016年03月07日 09:50:31 +00:00Commented 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– matteo2016年03月07日 10:11:17 +00:00Commented 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– TobsenB2016年03月07日 10:18:48 +00:00Commented Mar 7, 2016 at 10:18
2 Answers 2
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)
-
+1, very simply indeed! Is there any documentation or post which mentions this?Joseph– Joseph2016年03月07日 15:05:22 +00:00Commented 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– matteo2016年03月07日 15:13:12 +00:00Commented Mar 7, 2016 at 15:13
-
1Works for the latest QGIS 3 release.Spacedman– Spacedman2018年02月24日 00:02:28 +00:00Commented Feb 24, 2018 at 0:02
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()
-
I tried your code, but,
QgsMapLayerRegistry.instance().addMapLayer(layer)
does not load anything on the map.matteo– matteo2016年03月07日 10:35:46 +00:00Commented 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– Joseph2016年03月07日 10:40:55 +00:00Commented 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 withlayer.geometryType()
that results 2 (Polygon). My question is if it is possible to setlayer
with, for example,wkbType()
== 100, that isdata only layer
.. Thanks Joseph!matteo– matteo2016年03月07日 10:41:55 +00:00Commented 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– Joseph2016年03月07日 10:53:02 +00:00Commented Mar 7, 2016 at 10:53
-
1ok thanks! I will ask to some developer and update the question ASAPmatteo– matteo2016年03月07日 11:12:16 +00:00Commented Mar 7, 2016 at 11:12
Explore related questions
See similar questions with these tags.