2

Is there a way in QGIS to connect two fields belonging to two different layers of the same type?

For example in my case I have a linear "father" layer containing all the information of the pipelines of my network, from this layer I extracted another "son" layer containing the same elements but with different fields, except for some that are identical (for example the lengths of the pipes).

I would like to make sure that when I update the lengths contained in the "father" layer, the lengths in the "son" layer are updated automatically. Can you help me?

I attach a screen of the two layers, in which I would like to make sure that if I update the "L_LUN (m)" field of the "father" layer, also the "Length" field of the "son" layer is updated automatically.

enter image description here

3
  • 1
    Did you consider virtual layers?
    – Babel
    May 27, 2020 at 17:56
  • Thanks for your help, I was in fact trying to figure out how to connect the fields together using a virtual field, but I don't know what functions I must use. However, using a virtual field does not modify the original shapefile, while I would like the shapefile to be modified. May 27, 2020 at 18:07
  • 1
    Normally what you are doing would be easily achieved with a trigger if you were using a database (eg sqlite, postgres). However I don't think QGIS has an out of the box way to automatically update a child layer every time the parent layer changes. The only option might be to use python code in Actions
    – she_weeds
    May 28, 2020 at 1:26

1 Answer 1

1

You can use a virtual field in your son-layer which takes the length from the father layer. So when the length in the father will be changed, the virtual field in the son-layer will reflect the changes:

 aggregate( 
 layer:='father',
 aggregate:='max',
 expression:="length",
 filter:="id_son"=attribute(@parent,'id_father'))

this expression will take the value from field "length" of the father layer with the same id: "id_son"=attribute(@parent,'id_father')) You have to adjust the fieldnames to your layer, like id_son = Name and id_father = ID_COND.

enter image description here

4
  • Thank you very much for your answer, I tried the function but unfortunately the resulting field in the child layer (which I called "Paullo_link") is empty, maybe I'm wrong: I created a new virtual field in the child layer that I called LUNGHEZZA and I inserted this expression: aggregate( layer:='CONDOTTE EDITATE', aggregate:='max', expression:="L_LUN (m)", filter:="Name"=attribute(@parent,'ID_COND')) did I enter wrong? Thanks for your help! May 28, 2020 at 9:52
  • Should I create a field that has the same name as that of the parent layer, that is "L_LUN (m)" in my case? I wish that if I update the field of the parent layer "L_LUN (m)", it should automatically update the "Length" field in the child layer, being: Parent layer: 'CONDOTTE EDITATE' with filter id: "ID_COND" Child layer: 'Paullo_link' with filter id: "Name" May 28, 2020 at 10:04
  • Ciao Enrico, in your case it should be something like aggregate( layer:='CONDOTTE EDITATE', aggregate:='max', expression:="L_LUN (m)", filter:="Name"=attribute(@parent,'ID_COND'))
    – eurojam
    May 28, 2020 at 11:51
  • it's exactly what i tried but unfortunately it doesn't work .. Grazie per l'aiuto! May 28, 2020 at 13:08

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.