6

I have two layers in QGIS, each one with a field called "Name". I need to select the features in 'layer2' whose name exists in 'layer1' (totally or partially).

For example, in the images below, I want the following features in layer2 (on the right) to be selected: 197, 198, 200, 201, 203, 204, and 206.

enter image description here

How can I do that?

2
  • What exactly would be "partially"? A single character?
    – MrXsquared
    Commented Mar 25, 2022 at 20:02
  • The common field is Logradouro? For example, Square XYZ123 in the right table, and in the left table only XYZ123, should this field be joined?
    – hugonbg
    Commented Mar 25, 2022 at 20:21

3 Answers 3

9

Use "Select by expression" with this expresstion on layer 1:

array_contains( 
    aggregate( 
        'points1',  -- change this to the name of your layer1
        'array_agg',
        name1  -- change this to the name of the name field in layer1
    ),
    name2  -- change this to the name of the name field in layer2
)

Explanation: The function aggregate() creates an array of all values of the field "name1" in the layer named points1. array_contains() checks if the value (content, string) of each feature on layer 2 in the field "name2" is contained in this array and in this case returns true, otherwise false.

enter image description here

0
5

A solution using PyQGIS.

Let's assume there are two polygon layers called 'test' and 'test2' with, see image below.

input

With the code below I will select all features in the 'test' layer, that have the same "id" values as features in the 'test2' layer.

Proceed with Plugins > Python Console > Show Editor (see documentation) and paste the script below

def selecting_features(main_layer, target_layer, main_field, target_field):
    """
    Function for seleecting feature in the main_layer
    based on the mutual field with the target_layer.
    """
    main_layer_ = QgsProject.instance().mapLayersByName(main_layer)[0]
    target_layer_ = QgsProject.instance().mapLayersByName(target_layer)[0]
    
    list_of_target_values = QgsVectorLayerUtils.getValues(target_layer_, target_field)[0]
    tuple_of_target_values = tuple(set(list_of_target_values))

    if len(main_layer_.selectedFeatures()) > 0:
        main_layer_.removeSelection() 
    
    if main_field in target_layer_.fields().names():
        query = f'{main_field} in {tuple_of_target_values}'
        return main_layer_.selectByExpression(query)
    
    else:
        print(f'The field "{main_field}" does not exist in the layer "{target_layer_.name()}".')

selecting_features('test', 'test2', "id", "id")

Press Run script run script and get the output that will look like

output


References:

0
3

I would comment on @Babel, but I don't have enough power... (yet xD) With Babel's answer, you can select features where the attribute is exactly the same, although it isn't the case. You will still need to transform the strings in your fields. I added the functions to Babel's code, which will enable you to tansforms the strings in the Nome field to be able to match:

array_contains( 
aggregate( 
    'layer1',  -- change this to the name of your layer1
    'array_agg',
    replace(Nome, array('PRAÇA '), array(''))  -- change this to the name of the name field in layer1
),
upper(replace(Nome, array('Engenheiro', 'Governor'), array('ENG.', 'GOV.')))

-- change this to the name of the name field in layer2 )

The replace function can take arrays as parameters. The 1st value of the 1st array, will be replaced by the 1st value of the 2nd array, etc. Example: replace('PRAÇA FLORIVAL BRITA', array('PRAÇA '), array('')) returns 'FLORIVAL BRITA' The upper function transforms all lower case lettre to upper case letters : 'Word' => 'WORD' The arrays I provided might not completely get you covered. Some special caracters could need replacement, or other abreviations need replacement... I hope it helps!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.