4

This is based on a previous question/answer that I had. Replacing values in multiple columns using QGIS Field Calculator?

I need it to update 3 fields now and I get an name 'e' is not defined error. What have I done wrong?

layer = qgis.utils.iface.activeLayer()

# Set field names you want to update
field_1 = "New_Name"
field_2 = "New_File_P"
field_3 = "Process"
idx_1 = layer.fieldNameIndex( field_1 )
idx_2 = layer.fieldNameIndex( field_2 )
idx_3 = layer.fieldNameIndex( field_3 )


# Set expression to find all features which fall into expression
exp = QgsExpression( """ "Existing_F" LIKE '%2015 Aerial Ortho\\_2015\\ECW_Tiles%'  """ )

# Select all features which fall into expression
ids = [i.id() for i in layer.getFeatures(QgsFeatureRequest(exp))]
layer.setSelectedFeatures(ids)

# Set expressions to fill in values for selected features
formula_1 = """ "File_Name" """
formula_2 = """ '\\sipv-gis01\\GIS_Library\\Raster\\Aerial\\2015_25cm\\Images\\ECW' """
formula_3 = """  'y'  """
e_1 = QgsExpression(formula_1)
e_2 = QgsExpression(formula_2)
e_3 = QgsExpression(formula_3)
e.prepare(layer.pendingFields())

with edit(layer):
    # For each selected feature
    for f in layer.selectedFeatures():
        f[idx_1] = e_1.evaluate(f)
        f[idx_2] = e_2.evaluate(f)
        f[idx_3] = e_3.evaluate(f)

        layer.updateFeature(f)

Error

e.prepare(layer.pendingFields())
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'e' is not defined

==== I needed to do this again and here's the QGIS 3x code =====

#search and replace text in all fields of all layers in map

#set text to search for and replace with.
#CAUTION Partial match is allowed
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3

searchText = "Bougainvilia"
replaceText = "Bougainvillea"

#run on active layer
#layer = iface.activeLayer()

#run on all layers
layers = QgsProject.instance().mapLayers()
i=1
for layer_id, layer in layers.items():
    print("Layer: %s" % (layer.name()))
    # get data provider
    dpr = layer.dataProvider()
    for field in layer.fields():
        fieldName=field.name()

        for feature in layer.getFeatures():
            inText = str(feature[fieldName])
            # get field index
            fieldIndex = layer.fields().indexFromName(fieldName)
            #print ("Checking %s" % (inText))

            if searchText in inText:
                # change inText
                print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
                outText = inText.replace(searchText, replaceText)
                i+=1
                # save changes
                dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
15
  • 1
    I don't know python, but what stands 'e' for? don't see any declaration in your code snippet or is it al global variable? Commented May 13, 2016 at 7:03
  • Is it a typo for the exp QgsExpression you declared in line ~20?
    – Spacedman
    Commented May 13, 2016 at 7:09
  • 1
    Sorry, that was a mistake on my part. You shouldn't need that line e.prepare(layer.pendingFields()).
    – Joseph
    Commented May 13, 2016 at 10:03
  • 1
    Thanks George but @JochenSchwarze posted an answer which already describes commenting out (or removing) that line so please accept his :)
    – Joseph
    Commented May 17, 2016 at 9:23
  • 1
    @GeorgeC - Struggling to find the problem as the code still works and updates the .dbf file. This is after I clear all attributes in the 3 fields you mention in your code and setting the expression to exp = QgsExpression( """ "File_Type" = 'shp' """ ). Don't think I can help much further, sorry -_-
    – Joseph
    Commented May 24, 2016 at 11:07

1 Answer 1

2

According to http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html the member function QgsExpression.prepare() is not mandatory but makes the execution faster. I would recommend to comment out the line e.prepare(layer.pendingFields()) and see what happens.

And as far as I see, an exp.prepare(...) is missing directly behind the creation of the expression (i.e. exp=QgsExpression("""...) if things run slow, because the expression is evaluated in the next line as an argument to QgsFeatureRequest(exp).

3
  • I approved this to quick. It appears to work (i.e no errors) but the data fields are not updated. The query works correctly when just run in "select by expression" but not in the code. What else could be wrong?
    – GeorgeC
    Commented May 19, 2016 at 22:23
  • Hello everyone, does this work with QGIS 3.x and python 3? I failed to repeat this successfully.
    – Dan
    Commented Sep 18, 2018 at 7:53
  • See the updated Q for Python 3x code.
    – GeorgeC
    Commented Feb 1, 2022 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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