YoannQDQ / qgis-memory-layer-saver

Makes data in memory provider layers persistent between QGIS sessions. Data saved in .mldata file alongside project file.
Other
7 stars 4 forks source link

KeyError: '0' #2

Closed strk closed 10 years ago

strk commented 10 years ago

An error has occured while executing Python code:

Traceback (most recent call last): File "/home/strk/.qgis2/python/plugins/MemoryLayerSaver/MemoryLayerSaver.py", line 285, in saveData writer.writeLayers( layers ) File "/home/strk/.qgis2/python/plugins/MemoryLayerSaver/MemoryLayerSaver.py", line 47, in writeLayers self.writeLayer( layer ) File "/home/strk/.qgis2/python/plugins/MemoryLayerSaver/MemoryLayerSaver.py", line 71, in writeLayer ds.writeQVariant(feat[i]) KeyError: '0'

Python version: 2.7.6 (default, Mar 22 2014, 23:03:41) [GCC 4.8.2]

QGIS version: 2.5.0-Master Master, e91141c

Python path: ['/home/strk/.qgis2/python/plugins/processing', '/usr/src/qgis/build/master/output/python', u'/home/strk/.qgis2/python', u'/home/strk/.qgis2/python/plugins', '/usr/src/qgis/build/master/output/python/plugins', '/usr/local/lib/python2.7/dist-packages/nik2img-0.8.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/multicorn-1.0.3_dev-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/GDAL-1.11.0-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/src/qgis/build/master/output/python/plugins/fTools/tools', '/home/strk/Desktop', '/home/strk/.qgis2/python/plugins/MetaSearch/ext-libs']

http://hub.qgis.org/issues/11176

ccrook commented 10 years ago

Thanks for reporting this. I haven't been able to reproduce this - are you able to provide steps to reproduce? And platform information?

I've tried creating a new point memory layer, adding an integer and text attribute, creating a few points, and saving. That worked without generating any problems (QGIS 2.5.0, ubuntu 14.04)

strk commented 10 years ago

QGIS e91141c.

I used QuickWKT to create a memory layer, entered 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))' as the WKT, "QuickWKT 1" layer was added to the canvas. Hitting "Save" resulted in the above error.

Memory layer saver is version 3.3 and it's being loaded from my home directory. QGIS is running from the build directory.

QuickWKT is version 2.1 (latest) also running from my home dir. \cc @elpaso

ccrook commented 10 years ago

Thanks - I can reproduce this. I imagine an update to the plugin will be uploaded soon :-)

ccrook commented 10 years ago

Updated plugin to version 3.4.

The issue comes from the mismatch between the fields declared for the memory layer, and the fields that the individual features have. This is a potential issue with memory layers, which do not enforce any consistency in this (either when the features are added to the layer, or perhaps better for handling changes to the layer attributes, when they are recalled).

The fix that I've implemented is to set the field to NULL if it is not defined in an individual feature. This does mean that the reloaded layer is technically different to that originally saved - non-existent feature attributes are replaced with NULL.

I'm not sure in my own mind where this should properly be handled between the Memory vector data provider and the QuickWKT plugin. My inclination is to fix the provider to ensure that it always returns features with a full set of attributes.

ccrook commented 10 years ago

Further update to 3.5. Discovered a better way to avoid key error (indexing by field name rather than by attribute number - ie feat['name'] is valid whereas feat[0] is not). Also fixes potential exception saving memory layer while it is being edited, when it can be in an inconsistent state if the edits include changing the set of attributes.

Closing this issue.