3

I'm grouping different geological time scales into more general time scales.

An example of what I would want is

"if Oligocene-miocene is in descript_title, write Oligocene-Miocene in descript_category".

How would I write this as an expression?

enter image description here

As suggested by @ahmadhanb, I could extract a specific part of the feature to the target field.

However, how should I write this code properly? The goal is to do what @ahmadhanb but there is a specific line for each feature.

IF 'Basement Complex (pre-jurassic)' IN descript_title
substr("descript_title",0,17)

2 Answers 2

1

If only you need to extract Oligocene-miocene and put it in the category column, you can use the substr() function from Field Calculator to update the descript_category column as follows:

substr("descript_title",0,17)

Which will extract only Oligocene-miocene from the descript_title and add it to the descript_category.

enter image description here


Another way is to create a custom function, name it splitText, to split the text based on first whitespace as follows:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def splitText(labeltext, feature, parent):
    split_text = labeltext.split()
    if '-' in split_text[0]:
        out_text = '{0}'.format(split_text[0])
    else:
        out_text = '{0} {1}'.format(split_text[0],split_text[1])
    return out_text

Then from Custom Expression menu you can use the defined function as follows:

 splitText("descript_title")

Here is the output:

enter image description here

5
  • This works but there are many features under descript_title. This could be my last resort. Thank you, @ahmadhanb.
    – BallpenMan
    Commented Mar 23, 2020 at 2:57
  • 1
    @BallpenMan I updated my answer to give another way to do what you want.
    – ahmadhanb
    Commented Mar 23, 2020 at 3:06
  • Thank you! You wrote this so fast. Wow.
    – BallpenMan
    Commented Mar 23, 2020 at 3:08
  • It works for geological eras that are single word only/separated by a dash like neogene or oligocene-miocene but not for eras like upper miocene-pliocene. Thank you anyways. Learned a lot from your code.
    – BallpenMan
    Commented Mar 23, 2020 at 3:12
  • @BallpenMan I updated my answer to include eras like upper miocene-pliocene
    – ahmadhanb
    Commented Mar 23, 2020 at 6:56
1

ahmadhanb's answer works, but it seems to me that the part you want to remove always is in paranthesis, so if you split on '(', you can simplify as the first part split is returning is always what you are looking for:

from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def splitText(labeltext, feature, parent):
    split_text = labeltext.split('(')
    return(split_text[0].strip())

(strip() to remove trailing whitespace)

1
  • Thank you, @MortenSickel. I'll try this later and get back if it works.
    – BallpenMan
    Commented Mar 24, 2020 at 7:32

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.