I have two layers in QGIS. The first one contains buildings and the second one contains blocks. Now I want to add the ID of the block to all buildings inside this block.

Are there any functions in QGIS available for this?

  • Have you tried join attributes by location? – Erik Jul 15 '19 at 11:52
  • Now I tried but it give me not completely the output that I want. I do not want to get a new layer with extra column. I want that the id can be added to a predefined column. I'm not sure if this output is possible or if I'm using it wrong. – beajifu Jul 15 '19 at 12:25
  • Yeah, but the "new" layer is simply you smaller layer containing additional attributes. Is this a severe problem? – Erik Jul 15 '19 at 13:16
  • No it's not a big problem. It would have been easier if I could fill the existing layer that is also a database table (I use GeoPackage for the data). – beajifu Jul 15 '19 at 13:38

Option 1: Use join attributes by location.

Option 2: Use the field calculator with the following code (adaption needed)

aggregate(layer:='large layer',aggregate:='max',expression:="Name",filter:=intersects)

Please note: Make sure, the field type you are creating fits the source field. Furthermore, you need to use the layer name provided by the field calculator, not the displayed name. You need to change the expression-value to the column you want to copy. Also you might need to change the filter value. This is some rather advanced stuff.

  • Thank you for the answer. I tried the code with the field calculator and it seems to do want I want. But actually the fields are empty. The way I use it is: Select the buildings_layer > Open Field Calculator > Add your code (aggregate(layer:='block_test',aggregate:='max',expression:="block_id",filter:=intersects) > Check 'Update exisiting field' and select the field of the buildings_layer where I want the block_id . – beajifu Jul 16 '19 at 6:39
  • 1
    Okay I found the solution. I changed the code to: 'aggregate(layer:='block_test_109b7ef4_2a62_4efc_9cd4_1c54ca8c3e4f',aggregate:='concatenate',expression:="id",filter:= intersects( $geometry, geometry(@parent) ))' This works and is exactely what I want. Thank you very much for the fast and good help. :) – beajifu Jul 16 '19 at 7:13
  • Glad to help, and sorry it didn't work out initially. – Erik Jul 16 '19 at 8:05

Install the RefFunctions plugin. This gives you additional functions to use in the Field Calculator. These new functions are simpler to use than the built-in aggregate functions.

Use the Field Calculator to add a new field to the building layer, with this expression:

geomintersects('block layer', 'ID')

Substitute the actual names of your block layer and the ID field in the block layer.

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.