1

I've been developing a plugin for Qgis, but something weird happend while renaming a layer, now whenever i add a layer using a button in my plugin, the layer is renamed with the last name I gave it. I'll explain it using an example.

The plugin creates a buffer around some points and creates a temporary layer to store these buffers. I programmed the buffer_layer to have the name 'buffer_{}'.format(current user) (where current_user is a variable which I tested to be right). When the layer is loaded however, it will have the name "Buffers". I remember changing the name once to Buffers, and it seems as if Qgis remembers this change and applies it to all layers created by the plugin. I can change the name of the layer using right mouse click > rename and rename it to 'testname' and delete it afterwards. If I then add a new buffer layer using the plugin, the layer will have the name 'testname'.

This keeps happening.

I'm on OS X El Capitan, using Qgis 2.12 Lyon.

Added code

def calculateBuffer(self):

    proj = QgsProject.instance()
    cur_user = self.SelectUserGroupCombo.currentText()
    # The current user is selected using a drop down menu.
    radius = proj.readNumEntry("SpatialDecisionDockWidget", "radius")[0]
    transittypes = proj.readEntry("SpatialDecisionDockWidget", "transittypes")[0]

    uf.selectFeaturesByExpression(self.getSelectedLayer(),"network in {}".format(transittypes))
    origins = self.getSelectedLayer().selectedFeatures()
    layer = self.getSelectedLayer()


    if origins > 0:
        cutoff_distance = radius
        buffers = {}
        for point in origins:
            geom = point.geometry()
            buffers[point.id()] = geom.buffer(cutoff_distance,12)
        # store the buffer results in temporary layer called "Buffers_[cur_user]"
        buffer_layer = uf.getLegendLayerByName(self.iface, 'Buffers_{}'.format(cur_user))
        # create one if it doesn't exist
        if not buffer_layer:
            attribs = ['id', 'distance', 'network']
            types = [QtCore.QVariant.String, QtCore.QVariant.Double, QtCore.QVariant.String]
            buffer_layer = uf.createTempLayer('Buffers_{}'.format(cur_user),'POLYGON',layer.crs().postgisSrid(), attribs, types)

The layer is renamed here!

            buffer_layer.setLayerName('Buffers_{}'.format(cur_user))
            uf.loadTempLayer(buffer_layer)
        # insert buffer polygons
        geoms = [] # geometries in a list
        values = [] #list of lists, consisting of 3 items. E.g. [[0L, 1200, 1],[...

        fld_values = uf.getFieldValues(layer, 'network', True,"network in {}".format(transittypes))[0]
        cnt = 0

        for buffer in buffers.iteritems():
            # each buffer has an id and a geometry
            geoms.append(buffer[1])
            # in the case of values, it expects a list of multiple values in each item - list of lists
            values.append([buffer[0],cutoff_distance, fld_values[cnt]])
            cnt += 1

        uf.insertTempFeatures(buffer_layer, geoms, values)
        self.refreshCanvas(buffer_layer)
        layer.removeSelection()

I just thought of a better way to describe this, it is as if qgis overrides the layer name with the name I've given to another when I renamed it. It is as if it remembers the renaming and overwrites the layer name with the 'remembered name'.

5
  • 1
    Welcome! Please take the tour here gis.stackexchange.com/tour. It usually helps us out to take a look at the problematic code to get a better idea of what is going on. From what I understand, you are creating a layer, renaming it (in QGIS or your plugin?) and the next time you run the plugin, you get a buffer layer with the same name as the layer that you just changed the name to?
    – MaryBeth
    Dec 15 '15 at 14:40
  • Yes, i've done quite the searching for my problem, but haven't found a solution yet. Yes, i'm creating a layer using the plugin, and assigning a name to it using the plugin (in the code using setLayerName("name")). When the layer is imported in QGIS it gets a different name ("buffers").
    – Matthie456
    Dec 15 '15 at 14:43
  • Please add a screen shot of your code to your question. It's difficult to troubleshoot code by guessing.
    – MaryBeth
    Dec 15 '15 at 14:44
  • What is uf defined as?
    – Nathan W
    Dec 15 '15 at 23:01
  • uf is a file called utility function, which has some helpful function in it. I could show the code, but that would make things chaotic I guess.. When calling the createTempLayer function with the given name, the variable for that function is indeed correct, so it is probably going wrong somewhere in QGIS...
    – Matthie456
    Dec 16 '15 at 8:17
1

I have found the answer, apparently the InaSafe plugin is messing with the layer names and overriding it. Checking off the plugin resolved the problem.

Your Answer

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

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