I am writing a plugin for QGIS (v2.12).

There have been quite a few similar questions on this forum, but they don't seem to work for me. My plugin does the following:

  • get the vector layer
  • check that it is a line shapefile.
  • get the raster layer
  • look at the vector layer, if there isn't a attribute named 'slope' add it.
  • step through each line in the shape layer and compute the slope.
  • change the attribute value for that line to the computed slope.

My problem is the last step, I can compute the slope, set the attribute value for that field, but it never appears in the attribute table after the plugin completes.

Here is the relevant code, the logMessages are all valid, the # comments are things I have tried. I like setAttribute the best.

for line in selectedVectorLayer.getFeatures():             
    slope = self.getLineSlope(line, rasterData)
    columnid = selectedVectorLayer.fieldNameIndex('slope')                   
    QgsMessageLog.logMessage("id: {} segment: {} Slope: {}".format(line.attribute('id'), line.attribute('segment'), slope) , 'LineSlopes', QgsMessageLog.INFO)
    #line.changeAttributeValues(line.id(), columnid, slope)
    #attrib = {0: line.id(), 1: line.attribute('segment'), 2: slope }
    #selectedVectorLayer.dataProvider().changeAttributeValues({line.id(): attrib})
    line.setAttribute('slope', slope)
    QgsMessageLog.logMessage("id: {} segment: {} Slope: {}".format(line.attribute('id'), line.attribute('segment'), line.attribute('slope')) , 'LineSlopes', QgsMessageLog.INFO)                 

I'm thinking that there has to be something simple that I have overlooked.

  • I tried both and my attribute table shows null for slope instead of the computed value. Incidentally, selectedVectorLayer.setCacheImage generates a warning, indicating it is deprecated. – Robotuner Dec 13 '15 at 0:07
  • Apparently the line.setAttribute('slope', slope) is the culprit. The call is returning false, meaning it fails. Does anyone know the correct syntax for it? QgsFeature documentation indicates the syntax is setAttribute(const QString& name, const QVariant& value). – Robotuner Dec 13 '15 at 0:28
  • It says in the API docs that Returns false if attribute name could not be converted to index. Field map must be associated using setFields before this method can be used. ... did you call setFields() to create the field map? – J. Taylor Dec 13 '15 at 2:05
  • How do I call setFields()? line.setFields(?, false) – Robotuner Dec 15 '15 at 3:59

You're right. The ways you tried out don't seem to work properly. But there's a way you didn't mentioned in your original question. Use changeAttributeValue from the layer object, this way:

columnid = selectedVectorLayer.fieldNameIndex('slope') # 'slope' field should already exist

for line in selectedVectorLayer.getFeatures():             
    slope = self.getLineSlope(line, rasterData)
    selectedVectorLayer.changeAttributeValue(line.id(), columnid, slope)


It was the only way I got the attribute updated.

  • Thanks, that is one of the other solutions that I tried and commented out above. At this point, I'm thinking that there must be something else wrong. Perhaps either the Layer or QGIS is in a bad state. I notice that when I exit QGIS after running the plugin I get a crash dumped message: minidump written to C:L . . . How do I look at the .dmp file? It is binary. – Robotuner Dec 14 '15 at 4:24
  • I don't see my solution commented out above. I use changeAttributeValue from the layer object, whereas you used changeAttributeValues (with an 's') from the line (feature) object. Test my solution, it was the only way I got it to work, I hope it also works for you. – Germán Carrillo Dec 14 '15 at 4:41
  • I modified my code to mirror yours and got the same thing. It doesn't throw errors the logMessages indicate that it was successful, but nothing shows up in the attribute table. I'm currently focused on trying to figure out the cause of the crash dump when I close qgis, I'm thinking whatever is causing that is leaving the project in an unstable state. – Robotuner Dec 17 '15 at 3:05
  • Thanks everyone, I still haven't figured out what the problem is. For the time being, I'm going to shelf this until I figure out another issue (the crash dump I have mentioned). I will introduce that as a separate question. – Robotuner Dec 18 '15 at 22:19

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.