0

I was wondering if it is possible to code the symbology of a polygon with the size of the colors according to the numbers they represent (like a graph in excel) and display the labels on each slice, without using the Classify Option, everything is done with Symbology - Geometry Generator.

For example:

Purple 30% of the polygon, Red 20%, Blue 15%, White 12.5 %, Green 10%, Yellow 7.5%, Orange 5%.

Edited Image in Paint:

enter image description here

enter image description here

I have tried the Answer below and I am stuck. I think the Atrribute Table is at fault (the way I add information in it perhaps). I created a new Column as a test with just 4 values to be able to see if the polygon could be divided in 4 equal parts. Each value was 25. I did not add another column with just 1,2,3,4 because they are automatically on the left side of the attribute table. Here is a screenshot with an example made in Excel (because the Attribute Table in QGIS is too big):

enter image description here

Or should the Attribute Table look like this (example made also in Excel):

enter image description here

Could you post a screenshot from Excel how the Attribute Table should look like so the code could work?

I do not know what I am doing wrong.

I have managed to color the polygon according to the code. It works now:

enter image description here

Now I am having problems with the last 4 paragraphs about labeling.

For the 1st Paragraph about centroid() is this how it should be written?

enter image description here

For the 2nd Paragraph I managed to find the Font Marker, but I cannot see a button which allows me to create a custom formula:

enter image description here

For the 3rd Paragraph I do not know how to code what you mention there about Austria and Germany and the rest.

For the 4th Paragraph how do you duplicate the layers in Symbology like you mentioned?

If you can update your Answer with some screenshots step by step how to do the last 4 paragraphs. For advanced users such as you this is very simple, but to me as a beginner I find it very complicated because I am being shown only partially what needs to be done and the rest I have to figure out myself.

So I have tried something regarding Labelling , but I do not know where the mistake is . Besides the 4 Geometry Generators with Colors I added other 4 Geometry Generators with Fonts . Here is a screenshot : enter image description hereFor the 1st Geometry Generator the Font code in my case looks like this : enter image description here I managed to find where to write the expression : enter image description here But when I write the expression round("1",2) the output preview is NULL : enter image description here

Where is the mistake ? The 4 Geometry Generator added with Fonts / The Geometry Generator Font Code written incorrectly / The expression round("1",2) which gives me output preview NULL ? Besides this , for the other colors should the expression be round("2",3) , round("3",4) and round ("4") ?

Lastly , where and how should the code be written for this part you mentioned : "If you only need the Austria and Germany (for instance) to use this symbology: then either add some case when "name_field" in ('Austria','Germany') then ... in all the layers to only have the generated Style applied to when the geometry "name_field" is Austria or Germany, and have a simple fill in other cases.

Or duplicate your layers, with one filtered to only show Austria & Germany, on top of the simple underlying one" ?

5
  • 1
    Welcome to GIS.SE. I would probably split the 3rd question into its own post just to increase the likelihood of it being responded to and searchable by future users. However, you can manually modify the legend symbology by going to the legend properties in the map layout and double clicking on the specific legend item and setting the custom symbology as required.
    – she_weeds
    Jan 9, 2023 at 23:10
  • 1
    Welcome to GIS SE. Thank you for taking the Tour. Unfortunately, you seem to have missed the One question per Question policy. Any time you provide a bullet list of topics, you can expect your Question to attract closure votes for lacking focus.
    – Vince
    Jan 10, 2023 at 13:50
  • Thank you for your kind responses ! I will follow your feedback and make another post for the 3rd question . I tried double clicking on the specific legend item and I could only change the text . Could you please post an answer through images step by step how it is done in the post I created as requested : gis.stackexchange.com/questions/449289/… . Thank you very much !
    – Zoro
    Jan 10, 2023 at 20:02
  • All 3 questions pretty much depended on one another from my own point of view and that is why I chose to post them toghether . I did not know it is a one question policy . It would make no sense to create 3 different Questions , when all 3 questions pretty much are linked to one another . If I were to create 3 different Questions in just 1 day maybe people will criticise me as a spammer ? Plus people would probablly not be able to understand what I am trying to achieve in 3 seperate Questions and that is why they should be toghether . So why not have them toghether ? Just my own thoughts .
    – Zoro
    Jan 10, 2023 at 20:12
  • Adding more questions to the body is not going to address a "Needs more focus" closure.
    – Vince
    Jan 15, 2023 at 13:31

1 Answer 1

3

You can achieve what you mention in items 1&2 with the Geometry Generator.

Assuming there are 5 attributes, representing percentages (total of all5 values needs to be 1). In my example, these fields are named 1,2,3,4,5.

Create 5 instances of Geometry Generator, to fill the polygons with colors Use the same logic & 5 instances, to generate a Point at the centroid of these polygons, and show the percentage value using the 'Font Marker'

enter image description here

Create one Geometry Generator entry, type Polygon, using a combination of:

  • getting the $geometry bounding box min & max coordinates
  • make_rectangle_3points from these coordinates and using the 1 value
  • intersect this interim 'bounding_box' with the $geometry to limit the extent inside teh polygon (in my screenshot below, I added another entry to show the interim 'bounding_box')

full code goes like this:

intersection(make_rectangle_3points( 
  make_point(xmin(bounds($geometry)),ymin(bounds($geometry))),
  make_point(xmin(bounds($geometry)),ymax(bounds($geometry))),
  make_point(xmin(bounds($geometry))+ ("1")*(xmax(bounds($geometry))-xmin(bounds($geometry))),ymax(bounds($geometry)))),$geometry)

enter image description here

Then create 4 other entries with a similar formula, you just need to adapt where you frame the left most point (Xmin offset by all the previous percentages), and most right point (Xmin offset by all the previous percentages + the current percentage). For instance this is the code for the 4th entry:

intersection(make_rectangle_3points( 
   make_point(xmin(bounds($geometry))+("1"+"2"+"3")*(xmax(bounds($geometry))-xmin(bounds($geometry))),ymin(bounds($geometry))),
   make_point(xmin(bounds($geometry))+("1"+"2"+"3")*(xmax(bounds($geometry))-xmin(bounds($geometry))),ymax(bounds($geometry))),
   make_point(xmin(bounds($geometry))+ ("1"+"2"+"3"+"4")*(xmax(bounds($geometry))-xmin(bounds($geometry))),ymax(bounds($geometry)))),$geometry)

Once happy with the coloring part of things. You can duplicate each individual entry, turn it to a Point type Geometry Generator entry. Add centroid() around the existing geometry formulas to compute a point at thecentroid of each polygon subset.

To show the percentage value, set the marker to Font Marker', and in the Characters`, enter a custom formula to get the percentage value that you need:

enter image description here

If you only need the Austria and Germany (for instance) to use this symbology: then either add some case when "name_field" in ('Austria','Germany') then ... in all the layers to only have the generated Style applied to when the geometry "name_field" is Austria or Germany, and have a simple fill in other cases.

Or duplicate your layers, with one filtered to only show Austria & Germany, on top of the simple underlying one

11
  • Thank you for your kind response ! Please how did you create an attribute table out of nowhere like in the screenshots above ? I do not have an attribute table ready to test what you posted above for 1&2 . The only thing I have in mind is to export and digitize Austria 5 times and then write the values ( percentages ) so they could be in the attribute table . I have no idea how you did it . Could you also please explain how you created the attribute table with the 5 values ? I apologise , but I am not very advanced .
    – Zoro
    Jan 10, 2023 at 21:47
  • Thank you for your kind answer ! I can see now that the algorithm you mentioned could be applied to the other question too , but I am still having trouble with the code in the Answer above . So I added a column in the attribute table , wrote 4 values this time equal ( 25 ) within that column , wrote the code as in the model above , but the entire polygon dissapears from the map . Here is Austria before coding the symbology : i.stack.imgur.com/Wrtzq.png . Here is Austria after coding the symbology : i.stack.imgur.com/uAG2K.png . Could there be a mistake in the code ?
    – Zoro
    Jan 11, 2023 at 10:25
  • I will post some screenshots how I modified the code . Code 1 : i.stack.imgur.com/ZVLDY.png Code 2 : i.stack.imgur.com/ejd2r.png Code 3 : i.stack.imgur.com/Fpt5N.png and Code 4 : i.stack.imgur.com/PEQYF.png . It is either me writing the code badly or there could be a tiny mistake or more in both the codes within the Answer above . Please help ! Thank you very much !
    – Zoro
    Jan 11, 2023 at 10:30
  • I do not understand what I should do . Let me reexplain what I did . I opened the Attribute Table , added a new Column with just 4 values , each value being 25 ( 4 x 25 = 100 ) , then coded it according to the model above . Now you mention that if I want to use 4 values , I need to create 4 columns instead ? So it would be something like Column 1 ( 1 ) Value 25 , Column 2 ( 2 ) Value 25 , Column 3 ( 3 ) Value 25 and Column 4 ( 4 ) Value 25 ? Please enlighten me , because I do not know where the mistake is . Or could you please post a screenshot with the Attribute Table you created yesterday?
    – Zoro
    Jan 11, 2023 at 10:52
  • Seeing visually how the Attribute Table should look like will make things so much easier
    – Zoro
    Jan 11, 2023 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.