2

I have a table in Memory from a cx_Oracle query (a list of rows, and a list of field names and their types) in my python plugin and I want to load it as a geometry-less layer (Attribute table) in qgis. Which data Provider will Support that?

QgsOracleProvider has a constructor that Returns false, disconnects and sets valid to False if the table queried has no geometry column.

QgsMemoryProvider only allows Point, Linestring, Multipoint, MultilineString, and Multipolygon geometries. Any other geometry is interpreted as WKBUnknown, which causes the QgsMemoryProvider.isValid() to return False. If I cheat and create a QgsVectorLayer("Point", "myLayerName", "memory"), and add features with no geometries, then I get a seg fault when I add the layer to the map layer registry. Also important to note, is that when a layer is imported using QgsMemoryProvider, it is not possible to Join against it (A Join from the layer does work.) Since mostly people want to load non-geometric tables into Qgis in order to join them to an existing vector, this reduces the Memory provider's suitability for non geometric tables. (Unless I am wrong. I would be delighted to be wrong.)

QgsDelimitedTextProvider requires an Input file, and I would much rather not write out a csv to the file and then Import it again when I already have the data. But maybe I have to?

Thanks

1 Answer 1

3

You can use a point memory layer for this, but you need to make sure that each feature contains an empty geometry by calling:

feature.setGeometry(QgsGeometry())

Here's an example script that creates a memory layer with attributes taken from a nested list:

import PyQt4.QtCore as qt

data = [[1,'a'], [6,'b'], [12,'cde']]

layer = QgsVectorLayer("Point", "result", "memory")
layer.dataProvider().addAttributes( [ QgsField("myint", qt.QVariant.Int), QgsField("mytext", qt.QVariant.String) ] )

features = []
for entry in data:
    feature = QgsFeature()
    feature.setGeometry(QgsGeometry())
    feature.setAttributes(entry)
    features.append(feature)


layer.dataProvider().addFeatures(features)
QgsMapLayerRegistry.instance().addMapLayer(layer)
layer.updateFields()
2
  • Hi, Unfortunately this script has almost the same result as using QgsMemoryProvider without Setting the Feature geometry. A seg fault, but maybe a Little bit later. I ran exactly your script from the python console within qgis, on a blank Project. Namely, it loads, but then if you open the Attribute table, the fields haven't been properly added, and there is only one field in the Attribute table. If I click on it's field header, I get my seg fault. I tried Fixing the data line to have Indexes 0,1,2 and the addAttributes line to add a third field, but I have the same result. Explode. Commented Nov 22, 2013 at 11:02
  • @MonsterMushroom: Ah yes, sorry, I forgot to call layer.updateFields() at the end of the script. Fixed now.
    – Jake
    Commented Nov 22, 2013 at 11:28

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.