4

I just created a layer in which I hand drew a lot of polygons.

Now I want to fill out the Attribute table through commands since it will save me a bunch of time.

Therefore, I calculated the area of each polygon using $area/10000 to have the size in ha.

I want to prioritize each area

  • above 10 ha as Prio 1
  • each below 1 ha as Prio 3
  • each in between as Prio 2

Does anyone have a smart solution to the problem?

My closest approach is: IF ("Size ha" > 10, 1, 2)

That however still leaves me with having to distinguish the areas of Prio 2 to Prio 3 manually.

Philip Gatzlaff
  • 169
  • 1
  • 6

3 Answers3

6

This should work

if($area/10000 > 10,1,if($area/10000 < 1,3,2))

Just a capsuled if-condition, first checking for areas greater 10 ha, then checking for those smaller than 1 ha, then giving everything else Prio 2.

Taras
  • 32,823
  • 4
  • 66
  • 137
Erik
  • 16,269
  • 1
  • 24
  • 43
6

Using the CASE-statement

CASE
  WHEN "Size ha" >= 10 THEN 'Prio 1'
  WHEN "Size ha" > 1 AND "Size ha" < 10 THEN 'Prio 2'
  WHEN "Size ha" <= 1 THEN 'Prio 3'
END

Or as was provided by @Erik, the upper formula can be rewritten using the if-statement

if("Size ha" >= 10, 'Prio 1', if("Size ha" <= 1, 'Prio 3', 'Prio 2'))

References:

Taras
  • 32,823
  • 4
  • 66
  • 137
  • Ah, thanks. I tried using WHEN clauses, but somehow didnt end up at a fitting result. My command looked different though. I will try it out! – Philip Gatzlaff Feb 26 '19 at 10:23
  • Another possbile solution, though I do not like doublechecks on conditions, they tend to be a bit complicated in my experience. – Erik Feb 26 '19 at 10:23
  • @Erik, can you be so kind and refer me to one of such issues when applying doublechecks on conditions? Thank you – Taras Feb 26 '19 at 10:29
  • 1
    @Taras that is my own judgement, I usually mess up conditions when having to check upper and lower boundaries ;-) – Erik Feb 26 '19 at 10:32
  • 1
    @Erik, I think I will happen to me as well. However, when I have for instance 20 conditions I will be lost within a number of parenthesis in If-condition :-) – Taras Feb 26 '19 at 10:35
  • Is there a solution, where I could have another collum which only holds the prio information? Similar to If collum "Area_size" > 10, THEN "Prio" = 1? – Philip Gatzlaff Feb 26 '19 at 10:37
  • 1
    Just create a new column and fill it with CASE formula. – Taras Feb 26 '19 at 10:39
  • True, that was my mistake all along. I tried to fill in the column with a condition regarding its own. Thanks. You both have been a great help! – Philip Gatzlaff Feb 26 '19 at 10:43
1

There is no need for the AND in the CASE-WHEN-... construct. Since the first matching result is returned, you can simply make an ascending order of the values you would like to test for:

CASE
  WHEN "Size ha" <= 1 THEN 'Prio 3'
  WHEN "Size ha" < 10 THEN 'Prio 2'
  ELSE 'Prio 1'
END

Note also, that there is no explicit test needed for "Size ha" >= 10.

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117