2

Working in QGIS, I have an attribute consisting of scientific names of species. I would like to label the points on the map with the first 3 left letters of both parts of the binominal scientific name.

enter image description here

If I could make those letters uppercase, it would be even more awesome. For example:

FRICOE
EPIBUG
MEGMAC
ACRUNG

I cannot create fields as it is a PostgreSQL database.

2 Answers 2

6

Use this expression in the Single Labels value:

with_variable('strings',
    string_to_array(replace(upper("taxon"),' ',',')),
    left(array_get(@strings,0),3) || left(array_get(@strings,1),3))

It generates a string, replacing spaces by commas in your "taxon" field and transforming it to all caps along the way.

Then this string is converted to an array of substrings (getting rid of the commas)

From this, the first 3 left characters of substring 0 and 1 are concatenated

enter image description here

1
  • Thank You all, both methods do the job excellently, as requested. I accept both answers. Cheers! Feb 15 at 11:48
2

You can use the following expression in the label engine to get the desired output:

concat(
    substr(
        string_to_array(
            upper("FieldName"), 
        ' ')[0],
    1,3),
    substr(
        string_to_array(
            upper("FieldName"),' ' 
        )[1],
    1,3)
)

You need to change the "FieldName" with the name of the field that you want to use in the expression above, "taxon" in your case.

enter image description here

Output:

enter image description here

1
  • Thank You all, both methods do the job excellently, as requested. I accept both answers. Cheers! Feb 15 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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