2

In QGIS 3.16 the "merge feature attributes" function (on the advanced digitizing toolbar) allows merging the attributes of selected features by either 1) selecting which feature to keep the attributes from, or 2) concatenating the values from all selected features. However, there is no 'concatenate unique' option here; equal values will be duplicated.

Is there a way to concatenate the attribute values without duplication?

I am not looking to do this across the board for all features in a layer; just for features that I click and select.

2
  • Maybe Aggregate could help, see: gis.stackexchange.com/a/395730/88814
    – Babel
    Commented May 25, 2021 at 14:10
  • I considered it initially but didn't think it was the appropriate tool for this particular task, since I don't want to apply the same rule set to the whole layer, and I want to group many small batches of polygons somewhat subjectively, with different criteria each time. (I'd think that using Aggregate for that would probably be as much work as just concatenating the attribute values manually, since I would need to do it many times for my various selections.) But maybe I'm missing something...!
    – pete
    Commented May 25, 2021 at 14:40

2 Answers 2

6

Nice use case for Layer Actions.

enter image description here

Just configure a new layer action like this:

enter image description here

Code for the layer action:

from qgis.core import QgsProject
from qgis.utils import iface

field_name = "etiqueta"  # CHANGE THIS TO YOUR OWN FIELD NAME

layer = QgsProject.instance().mapLayer("[% @layer_id %]")
idx = layer.fields().indexOf(field_name)

if layer.selectedFeatureCount():
    params = QgsAggregateCalculator.AggregateParameters()
    params.delimiter = ','

    value = layer.aggregate(
        QgsAggregateCalculator.StringConcatenateUnique,
        field_name, params, fids=layer.selectedFeatureIds())[0]
        
    for fid in layer.selectedFeatureIds():
        layer.changeAttributeValue(fid, idx, value)
            
    iface.messageBar().pushSuccess("Concatenate unique", "Done!")
else:
    iface.messageBar().pushInfo("Concatenate unique", "First select some features...")

Notes:

  • You just need to change the field_name variable to match your field name and you're done.
  • The layer action will be available in editing mode. It won't be saved until you click on Save Layer Edits.

Below the code for concatenating several fields at once:

from qgis.core import QgsProject
from qgis.utils import iface

field_names = ["my_field_1", "my_field_2"]  # CHANGE THIS TO YOUR OWN FIELD NAMES

layer = QgsProject.instance().mapLayer("[% @layer_id %]")
field_dict = {layer.fields().indexOf(fn): fn for fn in field_names}

if layer.selectedFeatureCount():
    params = QgsAggregateCalculator.AggregateParameters()
    params.delimiter = ','

    attr_map = dict()
    for idx, field_name in field_dict.items():
        value = layer.aggregate(
            QgsAggregateCalculator.StringConcatenateUnique,
            field_name, params, fids=layer.selectedFeatureIds())[0]
        attr_map[idx] = value
        
    for fid in layer.selectedFeatureIds():
        layer.changeAttributeValues(fid, attr_map)
            
    iface.messageBar().pushSuccess("Concatenate unique", "Done!")
else:
    iface.messageBar().pushInfo("Concatenate unique", "First select some features...")
0
2

You can run the Aggregate tool and check the box next to Selected features only. Set the Aggregate function to concatenate_unique.

enter image description here

2
  • So the reason I'd rather not use the Aggregate tool is because the output is an additional layer; I will be doing this operation many times and I'd rather the data consolidation happen within my working layer. Additionally there is the fact that I don't think that I can save my parameters for Aggregate Function, Delimiter, etc. (this is also true for "Merge feature attributes") and having to put them in each time for something like 50 fields gets tiring fast. (I assume there may be a way to generalize the parameters using SQL, but I'm not there yet!)
    – pete
    Commented May 26, 2021 at 7:03
  • OK, that sounds reasonable!
    – Babel
    Commented May 26, 2021 at 7:04

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.