QGIS – How to Append Field from One Layer to Another Layer in QGIS

attribute-joinsattribute-tablelayersqgisunique id

In QGIS, I have one layer (called 'All data shapefile') which is a shapefile with my entire set of data, and another layer (called 'Y_data') that is also a shape file with a subset of 'All data shapefile'. The subset was determined by making a buffer around certain key data points and extracting all data points within 100m radius of the key points to make this second layer.

In the 'All data shapefile' layer I have a column named 'ID' which gives a unique number ID for each datapoint, however I added this column after creating 'Y_data', so the latter layer does not have the ID column.

How do I append the 'ID' column into the 'Y_data' layer, ensuring that each data point will have the same ID in both layers (i.e. each data point is given its correct ID as determined by 'All data shapefile')?

When I try to use 'Join', the pictures indicate what I end up with in my 'Y_data' attribute table:

Join tool
'Y_data' attribute table

Best Answer

Version A: Join attributes by location

Use Menu Processing / Toolbox / Join attributes by location. Settings to make (red highlighted on the screenshot):

  • Set Y_data as Base layer and All data shapefile as Join layer
  • Check the box next to intersects
  • For Join type, select Take attributes of the feature with largest overlap only (one-to-one).

Optional settings (yellow on the screenshot):

  • Fields to add (leave empty to use all fields): select the ID-field.
  • Optionally, you can set a Joined field prefix to better distinguish the initial attributes from the ones added by the join operation.

enter image description here

Version B: field calculator

You could also create a new field with field calculator on the layer Y_data and this expression:

attribute( 
     get_feature_by_id( 
        'All_data_shapefile',
        array_first (
            overlay_equals( 
                'All_data_shapefile', 
                $id
            )
        )
    ),
    'id'
)
Related Question