4

In the Attribute Table there is a field with the diameter and width of the pipe. I want to transfer all the data from this field to a new field. While in one line there can be several values of the diameter and width as shown in the screenshot, I need only transfer the maximum diameter, in the field calculator.

enter image description here

For example - in the screenshot, move to the new field only the maximum values of 1020 * 10.5 in the new field, And the values of 720 * 11 should not be transferred to a new field. enter image description here

  • I see both ; and : as delimiters: is it a typo? – mgri Mar 21 '17 at 14:19
  • A few doubts - Do you want to copy the diameter-width pair value based on the maximum value of diameter only or the evaluation of expression 1020*10.5? Also there seems to be some inconsistency of the data in the column - like 108x6,1020*10.5: 720*11 and 530*8-12; with the separators being different x,*,:,-. Is it a typo or the data is like that? – nash Mar 21 '17 at 14:21
  • There is no typo, all tabulation such as:; I will then replace – zho ra Mar 21 '17 at 14:29
  • * It is a separator between a diameter and a width, as for example 100x100 it is needed – zho ra Mar 21 '17 at 14:31
  • @zhora Thanks, I got it. Please, see if my answer solves your issue (It should consider all the tabulations you provided as an example). – mgri Mar 21 '17 at 16:21
1

You may run the following code from the Python Console, remembering to preliminary edit the name of the fields of interest (see the comments in the code below):

from qgis.core import *

layer = iface.activeLayer()
field = 'test' # replace this with the name of your starting field
newfield = 'result' # replace this with the name of your new field

layer.startEditing()
for feat in layer.getFeatures():
    string = feat[field]
    if string:
        tmp_dict = {}
        repl1=string.replace(' ', '')
        repl2=repl1.replace(':', ';')
        fields = repl2.split(';')
        max_value = 0
        for fd in fields:
            splitted = fd.split('*')
            if not splitted[0].isdigit():
                splitted = fd.split('x')
            if float(splitted[0]) > max_value:
                max_value = float(splitted[0])
                attribute = fd
        feat[newfield] = attribute
        layer.updateFeature(feat)
layer.commitChanges()

I tested it on this Attribute Table:

enter image description here

and this is the result I have obtained:

enter image description here

as expected.

0

You can use the function editor in the Field Calculator to define a function for your task. Assuming the diameter-width pairs are separated by ; and * is the separator between diameter and width, the following function select_maxdiam defined in the Function Editor should work -

@qgsfunction(args='auto', group='Custom')
def select_maxdiam(value1, feature, parent):
    #Removing all the space characters 
    newval = value1.replace(' ','')
    #Splitting the diameter-width pairs
    newval = newval.split(';')
    maxval = 0
    fin = ''
    for i in newval:
        a = i.split('*')[0]
        if float(a)>maxval:
            maxval = float(a)
            fin = i
    return fin

Here a contains the value of the diameter part of each pair which is compared with the rest to find the maximum among them.
In the expression tab you can just type select_maxdiam("dimensions"), where dimensions should be replaced by your column name containing the data.

Following screenshots are for reference -

Function editor Function editor

Field Calculator Field calculator

Result looks like this - enter image description here

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.