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?
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:
The model consists of three parts (see screenshot): 1) a vector layer input, 2) Field calculator, 3) Set Project variable.
In Field calculator, use
Pre-calculated Value
with this expression:'overlay_within(''world'', NAME)[0]'
, whereworld
is the name of the layer andNAME
the attribute containing the country name you want to get. Be sure to have single quotes'
and double single quotes''
exactly like that!In
Set project variable
, again usePre-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 andcountry
the name of the field created there:In the
Set project variable
dialog window, go to Dependencies and check the box next toField 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: