4

I want to use the tool intersects in the field calculator. If the condition is true, I want to set a result field to a specific number. The field calculator doesn't accept my expression but I can not find the reason for the error. Can you please help me.

CASE
WHEN intersects('layer1', 'layer2') = true THEN 1
WHEN intersects('layer1', 'layer3') = true THEN 2
END

the error says:

syntax error, unexpected NUMBER_INT, expecting WHEN or ELSE or END syntax error, unexpected END, expecting $end

I work with QGIS 3.4 (Madeira)

underdark
84.9k22 gold badges237 silver badges419 bronze badges
asked Feb 21, 2019 at 15:52
0

1 Answer 1

9

The intersects function requires two geometries (not layers) as input requirements as described in the help section of the field calculator:

intersects(geometry a, geometry b)

Tests whether a geometry intersects another. Returns true if the geometries spatially intersect (share any portion of space) and false if they do not.

If you want to use layer names, install the refFunctions plugin which adds more functions to the field calculator such as:

geomintersects(targetLayer,targetField)

Retrieve target field value when source feature intersects target feature in target layer.


Assuming you are using the field calculator on layer1, you could use something like the following:

CASE
WHEN geomintersects('layer2', 'any_layer2_FieldName') THEN 1
WHEN geomintersects('layer3', 'any_layer3_FieldName') THEN 2
END
answered Feb 21, 2019 at 16:11
4
  • I want to change always the same field (from the same layer). My layer1 contains different polygons, wich represent the location of buildings. With the intersect operation I want to change always the field "Geb_Nutz" (layer1), wich shows the kind of use for the building. For all the buildings, that intersect layer1 and layer2, I want to set "Geb_Nutz" to 1. For all the buildings, that intersect layer1 and layer3, I want to set "Geb_Nutz" to 2. I tried the following operation, but it still doesn't work. Commented Feb 22, 2019 at 12:16
  • CASE WHEN geomintersects('layer2',"Geb_Nutz") THEN 1 WHEN geomintersects('layer3',"Geb_Nutz") THEN 2 END Commented Feb 22, 2019 at 12:17
  • If you want to change the field "Geb_Nutz" then you need to provide any field name from layer2 and layer3 for the expression. Do not use "Geb_Nutz" because that is the field you want to change. So select layer1, go to the field calculator, select to update your chosen field and use the expression: CASE WHEN geomintersects('layer2', 'layer2_field') THEN 1 WHEN geomintersects('layer3', 'layer3_field') THEN 2 END Commented Feb 22, 2019 at 12:22
  • Thanks for quoting refFunctions, I appreciated, helped me a lot using geomintersects function. Commented Mar 23, 2021 at 16:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.