3

Is there a tool in QGIS which populates fields within the attribute table?

I know I can edit it manually but what I want is to include that tool in the workflow automation process.

I want to label the output rather than put it on the key of the map. To label I need to have a field where I can add the label. Since this is part of a larger process in the Graphical Modeler I need to automate it rather than adding the field and name manually.

What I'm interested in is if there is a tool or Expression in the Field Calculator that can return the name of the input layer and add it to the attribute table of the new/ output layer of the Field Calculator tool

6
  • 3
    Populate them with what? At which point of your workflow? What is your workflow? What have you tried? How do you populate the fields currently?
    – Erik
    Commented Nov 26, 2020 at 15:09
  • 1
    I'm guessing you are looking for the Field calculator...
    – Albert
    Commented Nov 26, 2020 at 15:33
  • 2
    Do you want to add a "calculate field" step to a processing model ? You need to give a lot more detail on your workflow to get answer
    – J.R
    Commented Nov 26, 2020 at 23:24
  • My model adds a field but at the moment it's just an empty field. I want to add a certain string so I can label the feature. Commented Nov 27, 2020 at 8:13
  • 2
    Is the any logic behind the label? We need more details still.
    – MarcM
    Commented Nov 30, 2020 at 8:40

1 Answer 1

9
+25

There are several options I may refer you to:

  1. "Field calculator" : qgis:fieldcalculator

    Opens the field calculator. You can use all the supported expressions and functions.

    A new layer is created with the result of the expression.

    The field calculator is very useful when used in The graphical modeler.

  2. "Refactor fields" : qgis:refactorfields

    Allows editing the structure of the attribute table of a vector layer.

    Fields can be modified in their type and name, using a fields mapping.

    The original layer is not modified. A new layer is generated, which contains a modified attribute table, according to the provided fields mapping.

    Refactor layer fields allows to:

    • Change field names and types
    • Add and remove fields
    • Reorder fields
    • Calculate new fields based on expressions
    • Load field list from another layer
  3. Advanced Python field calculator : qgis:advancedpythonfieldcalculator

    Adds a new attribute to a vector layer, with values resulting from applying an expression to each feature.

    The expression is defined as a Python function.

  4. Execute SQL : qgis:executesql

    Runs a simple or complex query with SQL syntax on the source layer.

    Beside a simple query, you can add expressions or variables within the SQL query parameter itself. This is particulary useful if this algorithm is executed within a Processing model and you want to use a model input as a parameter of the query. An example of a query will then be SELECT * FROM [% @table %] where @table is the variable that identifies the model input.

    The result of the query will be added as a new layer.


Answering the question "Expression in the Field Calculator that returns the name of the input layer?"

As was mentioned by @AlexandreNeto:

you can get the layer name using the variable /@layer_name, there is also the /@layer_id.

Check these article for more details:

4
  • Thanks for this, it's very helpful! Is there an expression in the Field Calculator that returns the name of the input file? Commented Nov 30, 2020 at 9:45
  • sorry for asking, do you mean file or layer?
    – Taras
    Commented Nov 30, 2020 at 9:46
  • sorry the input layer name. Thank you Commented Nov 30, 2020 at 10:39
  • @CuriousGirl you can get the layer name using the variable /@layer_name, there is also the /@layer_id Commented Dec 7, 2020 at 1:21

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.