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 putting in on the key of the map. In order 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
    Nov 26 '20 at 15:09
  • 1
    I'm guessing you are looking for the Field calculator...
    – Albert
    Nov 26 '20 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
    Nov 26 '20 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. Nov 27 '20 at 8:13
  • 2
    Is the any logic behind the label? We need more details still.
    – MarcM
    Nov 30 '20 at 8:40
7
+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.

  1. "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
  1. 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.

  1. 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 @Alexandre Neto:

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

Check this article for more details: "Exploring variables in QGIS pt 3: layer level variables".

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

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.