2

I have a layer with lots of features. Every feature has the data attribute. I could select some features by Select by location based on other layer(s) or just by free hand (yellow points as example).

enter image description here

The task is to select all other features with the same data attribute.

enter image description here

I am working in QGIS 2.18.10

I am seeing the solution by creating a new layer, than looping for the features for selecting ones with the same value in the raw layer. However, I need to process large datasets, so this solution looks unefficient.

3
  • If I understand well, starting from a selection manually made by you, you want to select all the features (in the same layer) storing the same values in the "data" field and then creating a new layer with all these features. Am I right?
    – mgri
    Jul 18 '17 at 9:41
  • yes, you are correct. The only thing is creating new layer is definitely not obligatory Jul 18 '17 at 9:45
  • Please, see my solution and let me know if it works (I can edit my answer if you need to save the selected features in a new layer).
    – mgri
    Jul 18 '17 at 10:28
5

You may run this code from the Python Console:

layer=iface.activeLayer() # Load the layer as you want
field_name = 'data' # Set the field name of interest

values = []
for feat in layer.selectedFeatures():
    tmp_value = feat[field_name]
    if tmp_value not in values:
        values.append(str(tmp_value)) # You may remove 'str' from this line if your field of interest already stores strings

strings = []
for val in values:
    if val != values[-1]:
        string = field_name + ' = ' + val + ' or '
        strings.append(string)
    else:
        last_string = field_name + ' = ' + val
        strings.append(last_string)
query = ''.join(strings)

request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
request.setSubsetOfAttributes([]).setFilterExpression(query)

selection = layer.getFeatures(request)
layer.setSelectedFeatures([k.id() for k in selection])

Starting from the features initially selected, it will select all the other features having the same values stored in the "data" field.

Then, it should run very fast because the feature request doesn't require geometries (see here).

4
  • 1
    perfect! An elegant solution. I have just added apostrophe symbol while creating query, to create < "data" = 'somevalue' or ... >, cause my data field is string type Jul 18 '17 at 11:57
  • @MykolaKozyr, which line? Within the code, val is already a string.
    – mgri
    Jul 18 '17 at 12:22
  • string = field_name + ' = ' + val + ' or ' to string = field_name + " = '" + val + "' or ". The same in else block Jul 18 '17 at 12:38
  • 1
    @MykolaKozyr Thanks for pointing it! I'm glad you found a working solution for you.
    – mgri
    Jul 18 '17 at 12:41

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.