3

For a building layer I have an attribute table with several fields for the building type where data was entered with a manually created form using checkboxes. Example:

Production: 1 = (yes), 0 = (no)
Office: 1 = (yes), 0 = (no)
Storage: 1 = (yes), 0 = (no) 

and so on...

Now I want to label this layer with first letter of the respective building type. Like e.g: Production = P, Office = O, Storage = S and so on...

In case its only only one type its easy. I create a new field and use an expression like this:

CASE WHEN "Production" = 1 THEN 'P' ELSE '' END

But many buildings refer to multiple types, so I'd like to have expression that wraps everything up. It should check the state of the check box fields and concatenate the respective letters in a new field: Examples:

P/S

O/S

P/O

P/O/S

How to do so?

  • Are the slashes necessary? – Erik Apr 24 at 11:58
  • Well, would be nice to have. If it gets too complicated like that, only spacing might be sufficient as well – Philippo Storino Apr 24 at 12:01
3

You can evaluate each condition using if(), combine the outputs using ||, and remove the last slash using a regex expression:

regexp_replace(  if("production"=1,'P/','') || if("office"=1,'O/','') || if("storage"=1,'S/',''),'/$','')
  • So '/$' finds me the last / - or any other character? Nice to know. – Erik Apr 24 at 12:17
  • 2
    @Erik $ is for end of line, / is for /, so the combination looks for a / followed by end of line (i.e the last one only) – JGH Apr 24 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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