0

Just give some context: I have two streetnetworks of which I want to convey attributes from one network to the other. However, the networks are geometrically diverging, so not overlapping, so that I will need to (partially) manually copy the attributes from one layer to the other. As I have to make this very, very often, it would be awesome, to have some tool, which supports this manually copy-pasting process.So, for example, by first defining from which layer I want to copy values and from which field and also to which layer and its field I want to paste values. I imagine that you could then just click on one feature in the copy-layer, and then click the relevant feature of the paste-layer, which then probably opens a combo box, where I can see both features and the to be copied value and then just clicking 'ok, which would then convey the value from the copy-layer-feature to the paste-layer-feature. Something like this. Hope this is understandable.

Is there a plugin in QGIS which does this, or helps in any way to copy-paste attribute values between features of two layer?

Of course, I did some research on this, and tried to find a plugin, but couldn't find any.

If there are other tipps, on how to semi-automate this kind of process, I'm happy to hear about!

2
  • 1
    I didn't use it but the "Attribute painter" plugin seem to be interesting for your needs
    – J.R
    Apr 24, 2023 at 9:35
  • Thank you for your quick reply! This is close to what I need, however, the tool seems to be made for copying attributes from features to features within the same layer, if I understand it correctly. What I need is to copy values from features in one layer to features in another layer.
    – i.i.k.
    Apr 24, 2023 at 9:47

2 Answers 2

2

While a custom map tool as asked about in your subsequent question is certainly one approach, here is a slightly more 'hard-coded' but simpler approach based on simply defining source and target layers and field names, then selecting a single feature in each layer and running the code to copy the value from the specified field in the source layer selected feature and paste it to the specified field in the target layer selected feature.

Note that this is writing directly to the data provider, so edits will be immediate and permanent. If you wanted to you could use an edit buffer instead.

project = QgsProject.instance()

source_layer_name = 'Layer_A'# Name of layer to copy attribute from
target_layer_name = 'Layer_B'# Name of layer to copy attribute to

source_field_name = 'Name'# Name of field in source layer to copy attribute from
target_field_name = 'Species_Name'# Name of field in target layer to copy attribute to

# Get source layer object
source_layer = project.mapLayersByName(source_layer_name)[0]

# Get target layer object
target_layer = project.mapLayersByName(target_layer_name)[0]

# Get index of target field
target_field_idx = target_layer.fields().lookupField(target_field_name)

# Get selected feature in source layer
source_feature = source_layer.selectedFeatures()[0]

# Get ID of selected feature in target layer
target_feature_id = target_layer.selectedFeatureIds()[0]

# Paste value from source field in selected feature of source layer to target field of selected feature in target layer
target_layer.dataProvider().changeAttributeValues({target_feature_id: {target_field_idx: source_feature[source_field_name]}})

# Repaint the target layer on the map canvas (this will just update any labels etc.)
target_layer.triggerRepaint()
1
  • Yes! I think this is it! In combination with the Multilayer Select Plugin (plugins.qgis.org/plugins/multilayerselect) I can use this for my purpose! It still needs a little adjustments for my purpose, but it allows to actually work with two activated layer! I had already some code written, which started to work like this in a similar way, but it would become way more complex. Thank you!!!
    – i.i.k.
    Apr 27, 2023 at 12:43
0

This is really awesome, thanks again to @Ben W! In combination with the Multilayer Select Plugin (https://plugins.qgis.org/plugins/multilayerselect/) it is now possible to copy-paste values from one layer to the other. I changed the code a little to be also able to copy values from one source feature to multiple target feature. The code is only a slight change:

#Create a new QgsProject. Reads and writes project states
project = QgsProject.instance()

source_layer_name = 'source'# Name of layer to copy attribute from
target_layer_name = 'target'# Name of layer to copy attribute to

source_field_name = 'source_id'# Name of field in source layer to copy attribute from
target_field_name = 'test_id'# Name of field in target layer to copy attribute to

# Get source layer object
source_layer = project.mapLayersByName(source_layer_name)[0]

# Get target layer object
target_layer = project.mapLayersByName(target_layer_name)[0]

# Get index of target field
target_field_idx = target_layer.fields().lookupField(target_field_name)

# Get selected feature in source layer
source_feature = source_layer.selectedFeatures()[0]
print(source_feature['source_id'])

target_feature = target_layer.selectedFeatures()
for feature in target_feature: 

    # Get ID of selected feature in target layer
    target_feature_id = target_layer.selectedFeatureIds()
    
    for feature_id in target_feature_id:
        target_layer.dataProvider().changeAttributeValues({feature_id: {target_field_idx: source_feature[source_field_name]}})


# Repaint the target layer on the map canvas (this will just update any labels etc.)
target_layer.triggerRepaint()



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.