0

I am using the advanced python field calculator to change the numeric values from a field "DN" to text using the following script:

def getValue(DN):
if DN == '10':
    value = 'undifferentiated urban'
elif DN == '11':
    value = 'Urban'
elif DN == '12':
    value = 'Commercial'
elif DN == '13':
    value = 'Major Roads'
elif DN == '20':
    value = 'Undifferentiated Cropland'
elif DN == '21':
    value = 'Cropland'
elif DN == '30':
    value = 'Undifferentiated Cutblocks'
elif DN == '31':
    value = 'Graminoid Cutblock'
elif DN == '32':
    value = 'Shrubby Cutblock'
elif DN == '33':
    value = 'Tree Cutblock'
elif DN == '40':
    value = 'Undifferentiated Burn'
elif DN == '41':
    value = 'Graminoid Burn'
elif DN == '42':
    value = 'Shrubby Burn'
elif DN == '43':
    value = 'Treed Burn'
elif x == '44':
    value = 'New Burn'
elif DN == '51':
    value = 'Closed Black Spruce'
elif DN == '52':
    value = 'Closed Jack Pine'
elif DN == '53':
    value = 'Closed White Spruce'
elif DN == '54':
    value = 'Closed Undifferentiated Conifer'
elif DN == '55':
    value = 'Closed Deciduous'
elif DN == '56':
    value = 'Closed Coniferous Leading Mixedwood'
elif DN == '57':
    value = 'Closed Deciduous Leading Mixedwood'
elif DN == '58':
    value = 'Closed Mixwood'
elif DN == '61':
    value = 'Closed Shrub'
elif DN == '63':
    value = 'Closed Upland Shrub'
elif DN == '71':
    value = 'Fescue Grassland'
elif DN == '72':
    value = 'Mixed Grassland'
elif DN == '81':
    value = 'Emergent Wetlands'
elif DN == '82':
    value = 'Graminoid Wetlands'
elif DN == '83':
    value = 'Shrubby Wetlands'
elif DN == '85':
    value = 'Lichen Bog'
elif DN == '86':
    value = 'Black Spruce Bog'
elif DN == '88':
    value = 'Undifferentiated Wetland'
elif DN == '91':
    value = 'Water'
elif DN == '101':
    value = 'Permanent Ice and Snow'
elif DN == '102':
    value = 'Rock Talus'
elif DN == '103':
    value = 'Exposed Soil'
elif DN == '105':
    value = 'Upland Dune Field'
elif DN == '112':
    value = 'Cloud'
elif DN == '113':
    value = 'Haze Shadow'
elif DN == '152':
    value = 'Open Pine'
elif DN == '153':
    value = 'Open Spruce'
elif DN == '154':
    value = 'Open Undifferentiated Conifer'
elif DN == '155':
    value = 'Open Deciduous'
elif DN == '163':
    value = 'Open Shrub'
else:
    value = 'none'
return value

However, I get the following log error:

Traceback (most recent call last): File "C:/PROGRA~1/QGIS3~1.14/apps/qgis/./python/plugins\processing\algs\qgis\FieldPyculator.py", line 176, in processAlgorithm exec(bytecode, new_ns) File "", line 1, in NameError: name 'DN' is not defined

Execution failed after 0.03 seconds

Loading resulting layers Algorithm 'Advanced Python field calculator' finished

Eventually, I would like to include this script at the tail end of a graphical modelling task I've completed. The field name "DN" will be the same as well.

Advanced Py Field Calculator Parameters

Log Error

2
  • I think that field names in Python Field Calculator must be written between the <> symbols. Please try with value = getValue(<DN>) to see if it works that way. Aug 19, 2020 at 1:26
  • Joining lookup table will do the same work with 10 times less efforts.
    – FelixIP
    Aug 19, 2020 at 1:53

2 Answers 2

1

As requested, please find the final code and screenshot:

def getValue(DN):
    if DN == 10:
        value = 'undifferentiated urban'
    elif DN == 11:
        value = 'Urban'
    elif DN == 12:
        value = 'Commercial'
    elif DN == 13:
        value = 'Major Roads'
    elif DN == 20:
        value = 'Undifferentiated Cropland'
    elif DN == 21:
        value = 'Cropland'
    elif DN == 30:
        value = 'Undifferentiated Cutblocks'
    elif DN == 31:
        value = 'Graminoid Cutblock'
    elif DN == 32:
        value = 'Shrubby Cutblock'
    elif DN == 33:
        value = 'Tree Cutblock'
    elif DN == 40:
        value = 'Undifferentiated Burn'
    elif DN == 41:
        value = 'Graminoid Burn'
    elif DN == 42:
        value = 'Shrubby Burn'
    elif DN == 43:
        value = 'Treed Burn'
    elif DN == 44:
        value = 'New Burn'
    elif DN == 51:
        value = 'Closed Black Spruce'
    elif DN == 52:
        value = 'Closed Jack Pine'
    elif DN == 53:
        value = 'Closed White Spruce'
    elif DN == 54:
        value = 'Closed Undifferentiated Conifer'
    elif DN == 55:
        value = 'Closed Deciduous'
    elif DN == 56:
        value = 'Closed Coniferous Leading Mixedwood'
    elif DN == 57:
        value = 'Closed Deciduous Leading Mixedwood'
    elif DN == 58:
        value = 'Closed Mixwood'
    elif DN == 61:
        value = 'Closed Shrub'
    elif DN == 63:
        value = 'Closed Upland Shrub'
    elif DN == 71:
        value = 'Fescue Grassland'
    elif DN == 72:
        value = 'Mixed Grassland'
    elif DN == 81:
        value = 'Emergent Wetlands'
    elif DN == 82:
        value = 'Graminoid Wetlands'
    elif DN == 83:
        value = 'Shrubby Wetlands'
    elif DN == 85:
        value = 'Lichen Bog'
    elif DN == 86:
        value = 'Black Spruce Bog'
    elif DN == 88:
        value = 'Undifferentiated Wetland'
    elif DN == 91:
        value = 'Water'
    elif DN == 101:
        value = 'Permanent Ice and Snow'
    elif DN == 102:
        value = 'Rock Talus'
    elif DN == 103:
        value = 'Exposed Soil'
    elif DN == 105:
        value = 'Upland Dune Field'
    elif DN == 112:
        value = 'Cloud'
    elif DN == 113:
        value = 'Haze Shadow'
    elif DN == 152:
        value = 'Open Pine'
    elif DN == 153:
        value = 'Open Spruce'
    elif DN == 154:
        value = 'Open Undifferentiated Conifer'
    elif DN == 155:
        value = 'Open Deciduous'
    elif DN == 163:
        value = 'Open Shrub'
    else:
        value = 'error'
    return value

Advanced Python field calculator screenshot

0

The error were as follows:

  1. It looks like it was an indentation error at the return value
  2. There was an x == '44': instead of DN == '44':

However,

The results field in the attribute table now has all values = none

3
  • You are comparing numeric value with string, remove ' around them.
    – FelixIP
    Aug 19, 2020 at 4:11
  • It works wonderfully now. Thank you. I have learned what those ' ' are to be used for now.
    – MJM
    Aug 19, 2020 at 20:00
  • Can you please post a screenshot of the Advanced Python Field Calculator dialog with the code that work? Aug 20, 2020 at 0:56

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.