3

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)

6

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
  • 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. – L.Eiden Feb 22 at 12:16
  • CASE WHEN geomintersects('layer2',"Geb_Nutz") THEN 1 WHEN geomintersects('layer3',"Geb_Nutz") THEN 2 END – L.Eiden Feb 22 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 – Joseph Feb 22 at 12:22

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.