3

I have a table of attributes in which a field contains combinations of IDs from another shapefile (ex. '[6, 4]' where 6 and 4 are each individual IDs in my shapefile). Here's an example for visualization:

enter image description here

(the one in the left would be the aforementioned attribute table and the one in the right the shapefile).

I was wondering if there is some way I could select the features in my shapefile using the combinations in the separated table. Mostly I'd like to know if it's possible to Select by Expression using "what's selected in another table" as input.

Jochen Schwarze
15k8 gold badges56 silver badges124 bronze badges
asked Nov 29, 2019 at 3:34
2
  • Interesting... You mean if you select e.g. rows 1 and 3 in the left table you get rows 4,5,6 an 9 selected in the right one? I think this will not yet be possible out of the box, sounds more like there are some lines of python code required for that... Commented Nov 29, 2019 at 7:37
  • yes, that's exactly what I mean. I think the key was to create a virtual field is_selected() in the first (left) layer (as suggested by @eurojam) but i don't know how to procede from there. Commented Nov 30, 2019 at 17:50

1 Answer 1

5

yes it is possible, but it is a bit a wild ralley of expressions in my brain;-). Also possible, that there is a more ellegant and easier solution with python...Let's say your layer A contains your ids which should be selected by the pairs of values (field "link" in brackets, [1,3]) in your layer B. The first step is to make virtual field, e.g. "sel" in layer B with the expression shown in the image below. This will dynamically reflect the selection state of your layer B, which we will need later in layer A:

enter image description here

The next step is to build the expression in layer A to select the features which are related to the selected features in B. There we will use the aggregate function which is very powerful:

enter image description here

array_contains(
string_to_array(
aggregate( 'B',
 'concatenate', 
 replace(replace(replace("link", '[',''),']',''),' ',''), 
 filter:="sel"=1,
 concatenator:=', ') ) ,
to_string( "id" ))

The aggregate function collects all items from the link field, which are selected (filter:="sel"=1). then we have to replace the brackets [1, 2] and spaces with the 3 replace statements. it would be easier to get rid of them before. at the end we put the string back into an array and proof if the id from layer A will be within the array...

answered Nov 29, 2019 at 8:14
4
  • thank you very much! this helped me a lot but I still can't get what I want. I end up only with the first number from the final array being selected. I don't know what I'm doing wrong... I think the issue arises from using "array_contains" but i don't know what to do to fix it. Commented Nov 30, 2019 at 17:44
  • Also I'm only selecting one feature in my first layer (your B) so the concatenating part is not really neccesary. Commented Nov 30, 2019 at 17:52
  • 1
    I think it is because in your brackets it looks like [1, 2, 3] with spaces between the numbers, so we can change the statement do get the desired result. I moved the replace statements into the aggregate statement and added one more replace to get rid of the spaces. See my above post, where I edited the statement... Commented Dec 1, 2019 at 9:33
  • yes! that worked perfectly... thanks a lot! you solved my problem. Commented Dec 1, 2019 at 14:42

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.