1

I have two vector layers composed of polygons. The polygons of the first layer are contained in exactly one polygon of the second layer.

How do I create an attribute on the first layer with the ID of the polygon containing it?

The layers are shapefiles.

I have tried to use the field calculator to create the new attribute, but I could not figure out how refer to the geometries of the second layer.

1
  • Please edit the question to specify the format(s) of the data sources and what steps you have taken to attempt this.
    – Vince
    Feb 10, 2017 at 16:14

1 Answer 1

1

Assuming to start from this situation (where the blue layer is the "first layer" and the green one is the "second layer"):

enter image description here

you may use the Join attributes by location tool from the Processing Toolbox with these parameters:

enter image description here

You will obtain a new layer, identical to the "first layer", which also stores the attributes from the "second layer" when the geometric predicated is satisfied.

With reference to the above example, this will be the Attribute Table of the joined layer:

enter image description here

where the "ID_SECOND" field is the field which stored the ID from the second layer. Once you have done this, you may delete all the additional field that are not of interest.

5
  • When I try to do this, QGIS complain that the two layers do not have the same SRC (they do as far as I can tell) an that unexpected results may follow. Then it runs ans I get a layer with an empty attribute table.
    – fccoelho
    Feb 10, 2017 at 17:33
  • I managed to fix the SRC. but still gives me an empty atribute table
    – fccoelho
    Feb 10, 2017 at 17:37
  • You are trying to perform a spatial operation, so you need to have both layers with the same CRS (as in my example above). You see your layers as "aligned" only because you have the "on-the-fly reprojection" activated. I think you need to preliminary reproject your layers in the same CRS before applying my solution.
    – mgri
    Feb 10, 2017 at 17:39
  • Ok. finally ajusted the projection correctly and it worked!
    – fccoelho
    Feb 10, 2017 at 17:51
  • Perfect, I'm glad it worked also for you!
    – mgri
    Feb 10, 2017 at 18:10

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.