14

Im trying to calculate certain values of a newly calculated point. So I use the field calculator to write an equation to determine the values.

Is there a way to save that equation, so that if and when I start a new job, I don't have to retype it in?

Something similar to how you save styles and symbology for a array of points.

1
  • That is a good feature request and would be quite easy to add.
    – Nathan W
    Commented Sep 26, 2018 at 21:51

4 Answers 4

8

QGIS saves the last equation used in the field calculator under the tab "Recent" where you can reuse it by double-clicking it

img of field calc

1
  • 1
    That is right in case the OP didn't use other equations later. But it will disappear by time when the OP uses new equations. The Recent container is just temporarily.
    – ahmadhanb
    Commented Sep 25, 2018 at 7:52
8

You can write your equation in a new function, and then you would only have to call the function to re-apply the computation.

Open the field calculator, click on the Function Editor tab, new file then edit the function. Select load. Note that ok will not be available at this point.

Go back to the Expression tab, select the custom function group and double click on your function. Select the field to be updated, and hit ok.


If you want to only use field values and constants, you can edit the default function signature, which starts with value1. In my example I have removed it, but you could use this field(s) to pass new arguments.

You can then access the feature field values using feature["fieldName"].... be carefull to call this function only on layers containing the said fields!

@qgsfunction(args='auto', group='Custom')
def funcTestCalc( feature, parent):
    return feature["fieldA"]+feature["fieldB"];

enter image description here enter image description here

4

If you're adding a new point to an existing layer, and you want to run the same calculation for that point as for all the other points, here are two useful features:

Virtual Field

A virtual field automatically calculates an expression for every new attribute. It's also updated regularly. So, eg, for a virtual field called "latitude" you could use the expression $y.

Pros and Cons of Virtual Fields:

  • Whenever you add a new point, it will automatically have the output of the expression added to this field.
  • Automatically updated whenever the expression out put changes. So if you move the point in the example, the "latitude" value will change automatically.
  • You can't edit a virtual field value.
  • Virtual fields are saved as part of the QGIS project file, not as part of the shapefile (or whatever format your layer is in). So if you open the layer in a different project, it won't have its virtual fields.

Regular Field with a Default Field Value

You can set up a field to have a default value based on an expression. When you create a new point, its attribute will be auto-filled with the default value. You can change the attribute value. It's not automatically updated.*

Pros and Cons of Default Field Values:

  • Whenever you add a new point, it will automatically have the output of the expression added to this field.
  • Not automatically updated. In the example this means that if you moved an existing point, its "latitude" value wouldn't be updated.
  • You can change the value of this field.
  • Saved as part of the shapefile. So if you open the layer in a different project, it will have this field in its attribute table.

*In QGIS 3.2, default field values can (optionally) be set to "apply default value on update." With this option, if you move a point where the default value is $y the current value will be replaced by the new y-coordinate.

2
  • I think the default field value can be set to auto-update in QGIS 3.2.
    – she_weeds
    Commented Sep 26, 2018 at 5:22
  • @she_weeds Thank you for pointing that out. I'll update my answer to include that information.
    – csk
    Commented Sep 26, 2018 at 16:58
3

Additionally to the solution by @LaughU, you may incorporate any field calculator operation into a graphical model and then save and run the model. This but requires your column names always to be the same.

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.