qgis-3,qgis-modeler,qgis-variable – Extracting an Attribute from a Created Point and Setting it as a Project Variable in QGIS

qgis-3qgis-modelerqgis-variable

enter image description here
I am trying to create a QGIS modeler process by which the center point of the map on the desktop is used to query what County it falls in from a PostgreSQL data base and writes that county name as a string value to a project variable which can be used to fill in subsequent layout text boxes.

I have it all working except I can't seem to set the County_Var variable from the query.

See the attached image. Where I am wrong?

enter image description here

Best Answer

I created such a model you can download here: enter a polygon layer for countries, enter a point layer and it creates a project variable with the name of the country the point lies within.

Here how to do it yourself:

  1. The model consists of three parts (see screenshot): 1) a vector layer input, 2) Field calculator, 3) Set Project variable.

    enter image description here

  2. In Field calculator, use Pre-calculated Value with this expression: 'overlay_within(''world'', NAME)[0]', where world is the name of the layer and NAME the attribute containing the country name you want to get. Be sure to have single quotes ' and double single quotes '' exactly like that!

  3. In Set project variable, again use Pre-calculated Value, this time with this expression, where @Field_calculator_OUTPUT is the variable available in the expression string builder, based on the Field calculator algorithm and country the name of the field created there:

     attribute(
         get_feature_by_id(@Field_calculator_OUTPUT, 1),
         'country'
     )
    
  4. In the Set project variable dialog window, go to Dependencies and check the box next to Field calculator.

When you now run the model, a project variable will be created with the name of the country the point from the input layer lies within:

enter image description here

Related Question