Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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()
share|improve this answer
    
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. –  MonsterMushroom Nov 22 '13 at 11:02
    
@MonsterMushroom: Ah yes, sorry, I forgot to call layer.updateFields() at the end of the script. Fixed now. –  Jake Nov 22 '13 at 11:28
    
Superschön, Thanks! –  MonsterMushroom Nov 22 '13 at 11:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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