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.
value = getValue(<DN>)
to see if it works that way.