I have two layers imported to QGIS 3.28.1 Firenze; 1* points and 1* polyline.
In Model Builder I have run an algorithm on the polyline which outputs points with an attribute named "KP"
Using the syntax
"KP" in (1,2,3,4)
I can select points from this layer where "KP" appears in a manually entered list. I am trying to select points from this layer where "KP" appears in the input points layer, attribute name "KPto".
I try
"KP" in (@KPzones_xyz, "KPto")
The script executes without error however nothing is returned. What should the correct syntax be?
-
My magic 8-ball says you need to provide more details on your data, especially field names, contents and common keys.Erik– Erik2023年01月10日 15:37:59 +00:00Commented Jan 10, 2023 at 15:37
-
Thanks. Field names of interest are "KPto" on layer KPZones_xyz and "KP" on layer "Field_calculator_OUTPUT". Both of which are type double. I don't understand the reference to magic 8-ball. I haven't come across this in QGIS before, is it a debugging tool?CJA888– CJA8882023年01月10日 15:54:09 +00:00Commented Jan 10, 2023 at 15:54
-
en.wikipedia.org/wiki/Magic_8_Ball - and I still have no idea, how your fields should interact. Is there a common key/ID? What exactly are you trying to achieve?Erik– Erik2023年01月10日 15:58:36 +00:00Commented Jan 10, 2023 at 15:58
-
I see, thanks for the clarification on magic 8 ball, I have heard of it but didn't know what it does. I also have no idea how my fields should interact. No there is no common key. That is the problem, I am trying to select fields "KP" from my points layer where "KP" matches "KPto" from a different layer. You can think of "KPto" being a representation of (1,2,3,4) which was the manual text entry which works to select the required field.CJA888– CJA8882023年01月10日 16:12:59 +00:00Commented Jan 10, 2023 at 16:12
1 Answer 1
If you run the select by expression
on one layer to select the attributes pointid
to be within all id
fields from another layer layer1
, one expression that will work is:
array_contains(
aggregate(layer:='layer1', aggregate:='array_agg', expression:="id"),
"pointid")
The aggregate()
creates an array with all existing value of the layer1
attribute
array_contains()
checks if the current layer attribute is part of the list
Applied to layer2
, where attributes pointid
are in the id
of layer1
:
If I get your attribute names well, it should read like this in your case:
array_contains(
aggregate(layer:='KPzones_xyz', aggregate:='array_agg', expression:="KPto"),
"KP")
-
1Accepted as solution thank you- I removed explicit naming so code is as below for ease of reading, and kept the explicit case in comments array_contains( aggregate('KP_xyz', 'array_agg', "KPto"), "KP")CJA888– CJA8882023年01月11日 08:28:03 +00:00Commented Jan 11, 2023 at 8:28