Skip to main content
added 53 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

There is no prebuilt class to do the magic the field calculatorField Calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepare(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 
expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepare(layer.pendingFields())
layer.startEditing()

for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepare(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges() 
expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepare(layer.pendingFields())

layer.startEditing()

for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges()

There is no prebuilt class to do the magic the field calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepare(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepare(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges() 

There is no prebuilt class to do the magic the Field Calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepare(layer.pendingFields())
layer.startEditing()

for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepare(layer.pendingFields())

layer.startEditing()

for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges()
deleted 12 characters in body
Source Link
artwork21
  • 35.2k
  • 8
  • 67
  • 134

There is no prebuilt class to do the magic the field calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepareFieldsprepare(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepareFieldsprepare(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges() 

There is no prebuilt class to do the magic the field calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepareFields(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepareFields(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges() 

There is no prebuilt class to do the magic the field calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepare(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepare(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges() 
Source Link
Nathan W
  • 35k
  • 5
  • 99
  • 148

There is no prebuilt class to do the magic the field calculator does (I am planning on adding it at some stage) however it's quite simple:

expression = QgsExpression("1 + 1")
# This allows field lookup
index = layer.fieldNameIndex("UpdateField")
expression.prepareFields(layer.pendingFields())
layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    layer.changeAttributeValue(feature.id(), index, value)

layer.commitChanges() 

A couple of notes: We are using changeAttributeValue because it's faster but does require use to use the feature id and field index, another way which might be as fast but is clearer to read is:

expression = QgsExpression("1 + 1")
# This allows field lookup
expression.prepareFields(layer.pendingFields())

layer.startEditing()
for feature in layer.getFeatures():
    value = expression.evaluate(feature)
    feature["UpdateField"] = value
    layer.updateFeature(feature)

layer.commitChanges()