2

I am writing a Python QGIS Plugin, using QGIS version 2.6 as the basis for development. When loading data into the canvas programmatically, I am trying to maintain the spatialite table name as the original name of each layer, so I can reference it to call functions on the data from GUIs in QGIS. However, I notice that when using QgsMapLayerRegistry to get the layers, the .originalName() function does not return the original name. Instead, it updates with the current layer name.

I have tried loading the vector layer using iface.addVectorLayer with the table name as the layer name, and changing the layer name after it has been loaded to see if that would maintain the original name, but I still have no luck. My workaround is load the vector layer as described above, but then call the .id() to get the original layer name, but the string returned from id() appends a long datetime to the end of the layer name, which makes my scripting work more complicated since the datetime has to be stripped out.

Is there a reason that the .originalName() function does not return the original name of the layer?

To be clear, this is how I load the spatialite layer into the canvas:

table_name = <spatialite table name>
display_name = <name to show in table of contents>
layer = iface.addVectorLayer(uri.uri(), table_name, 'spatialite')
layer.setLayerName(display_name)

And when trying to get the original name of the layer:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    print layer.originalName()

But the result from layer.originalName() just returns the display_name, not the table_name that the layer was initialized with.

Your Answer

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

Browse other questions tagged or ask your own question.