3

I am using QGIS, version 3.24.0. I have a shapefile called "Map_CorrectionDEF", uniquely identify by fields "GID" and "unitcode". It is a grid cell, in which the field "GID" is the cell and "unitcode" the country to which the cell belongs.

On the other hand, I have a CSV file, called crop_main (no geometry), uniquely identify by "unitcode" and "cropclass", where "unitcode" refers to the country and "cropclass" the type of crop.

I present an example of my CSV file:

enter image description here

I want to connect both fields. I would like to assign to each cell the type of crop which exist in the country which the cell belongs. More specific, for each cell, which belongs to a country ("unitcode" field), I would like to join every value of my csv file with has the same value of "unitcode". The goal is to obtain for all the cells of my shape file the information of all cropland which grows in the country. Hence, all the cells of the same country should have the same number of crops.

To do it (I am not sure if the correct way), I am doing the following relations:

enter image description here

However, when I do it, I am not able to visualize the relation I am creating. According to this, https://docs.qgis.org/3.10/en/docs/user_manual/working_with_vector/attribute_table.html?#creating-one-or-many-to-many-relations "depending on your settings, the form might open directly or you will have to choose to open it in the identification dialog under actions." In my case it's not open directly, but I am not able to go to the identification dialog.

I add a screenshot of what I see when I click one my cells in the map after doing the relation which @J.R proposed in his answer.

enter image description here

Second edit: I attach an screenshot of how I try to match the 1:many with aggregate function according to 1n-relations-in-label-expressions-general-syntax

enter image description here

1 Answer 1

5

You just need to set the two layer (parent : "Map_CorrectionDEF", and child : "crop_main") then set the field that link them, in your case the country field: "unitcode" for both layer.

This way when you click in a cell you will see all entry from the CSV table that share the same unitcode (be aware that relation don't work like join, you wont get the CSV column added to your grid table but you will see a form with, on top the grid attribute and below an extract of the attribute table of related CSV entry. see image below for example from QGIS doc on relation)

enter image description here

If your goal is to get a field in your grid table with a list of all crop for the country you may try the solution from here

12
  • Hi @J.R, thank you for your answer. When I do the relation just for "unitcode" and I select one of my cells, I just see a different numbers of cells, not the crops. I have edited my question with that.
    – OgeiD
    Commented Apr 15, 2022 at 9:14
  • 1
    @OgeiD I have modified the link at the end (it's for label but the same expression should work in field calculator). For seeing the same as in my picture you just need to click on the top left icon of the identify result panel to open the form (you may click on the wrench icon on the left to set it to automatically open)
    – J.R
    Commented Apr 15, 2022 at 9:26
  • 1
    Yes you are doing one to many as for each cell (1) you will get many crop (in reality you are doing many to many as each crop could belong to several cell but for your purpose it doesn't matter). For what you see in the form check the attribute form properties (in the grid layer properties, attribute form tab), it could be set either to autogenerate (then you should get the same as me) or to drag and drop designer then make sure to add the relation to the form (drag the relation from the available widgets column to the form layout column)
    – J.R
    Commented Apr 15, 2022 at 10:13
  • 1
    You forget to remplace the @parent value by 'Map_CorrectionDEF'
    – J.R
    Commented Apr 15, 2022 at 14:27
  • 1
    try to remplace expression:="cropclass" by expression:=to_string("cropclass")
    – J.R
    Commented Apr 15, 2022 at 14:54

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.