1

I was hoping someone could help me with an issue I've been having while trying to aggregate data into a new column in an attribute table in arcgis. This is part of a model built using model builder. I added a new column, but the model always fails when it reaches the part where it has to populate the new column.

I'm trying to group specific land uses into a new land type (for example: field, pasture, and crop should all be coded as 'Agriculture' in the new column). I have been using the field calculator tool to populate the new column, but it's not working. I think either field calculator is the wrong tool to use in this scenario, or my python is wrong. Any advice? My python is below.

def landuse_groupFromName(LUDCODE):    
if LUDCODE in ['23', '1', '36', '35', '2']: 
return 'Agriculture'   
elif LUDCODE in ['15', '16', '5']:
return 'Commercial/Industry'    
elif LUDCODE in ['11', '10']:       
return 'High_Density_Res'    
elif LUDCODE in ['13', '38']:
return 'Low_Density_Res'   
elif LUDCODE in ['12']:  
return 'Med_Density_Res'    
elif LUDCODE in ['34', '26', '29', '7', '8', '17', '9']:        
return 'Recreation'    
elif LUDCODE in ['18']:       
return 'Transportation'    
elif LUDCODE in ['31']:        
return 'Urban'    
elif LUDCODE in ['39', '19']:       
return 'Waste'    
elif LUDCODE in ['20']:       
return 'Water'    
elif LUDCODE in ['37', '14']:        
return 'Wetlands'    
elif LUDCODE in ['40', '3', '4', '6', '24', '25']:        
return 'Wild'    
else:        
return 'undefined'
  • 3
    Fix your indention – Shiko Apr 28 '17 at 2:45
  • 1
    Is that indentation indicative or is it caused by formatting into GIS.SE? I have noticed multiple return statements aren't liked by the field calculator, try replacing return with Rval = then on the last line return Rval. – Michael Stimson Apr 28 '17 at 2:45
  • @MichaelMiles-Stimson I think indentation is formatted in GIS SE. – Shiko Apr 28 '17 at 2:49
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Apr 28 '17 at 2:52
  • 2
    Are you looking up integers/numbers? Or are you looking up values stored as text? should elif LUDCODE in ['15', '16', '5']: rather be elif LUDCODE in [15, 16, 5]:? – Midavalo Apr 28 '17 at 2:53
2

Indentation need to be fixed. Be careful for indentation when working with Python. Try below:

def landuse_groupFromName(LUDCODE):    
    if LUDCODE in ['23', '1', '36', '35', '2']: 
        return 'Agriculture'   
    elif LUDCODE in ['15', '16', '5']:
        return 'Commercial/Industry'    
    elif LUDCODE in ['11', '10']:       
        return 'High_Density_Res'    
    elif LUDCODE in ['13', '38']:
        return 'Low_Density_Res'   
    elif LUDCODE in ['12']:  
        return 'Med_Density_Res'    
    elif LUDCODE in ['34', '26', '29', '7', '8', '17', '9']:        
        return 'Recreation'    
    elif LUDCODE in ['18']:       
        return 'Transportation'    
    elif LUDCODE in ['31']:        
        return 'Urban'    
    elif LUDCODE in ['39', '19']:       
        return 'Waste'    
    elif LUDCODE in ['20']:       
        return 'Water'    
    elif LUDCODE in ['37', '14']:        
        return 'Wetlands'    
    elif LUDCODE in ['40', '3', '4', '6', '24', '25']:        
        return 'Wild'    
    else:        
        return 'undefined'
0

I suspect the reason your code is failing is because you are looking for text values - elif LUDCODE in ['15', '16', '5']: - rather than numeric values - elif LUDCODE in [15, 16, 5]:. Python won't find text in a numeric field.

You can put everything into a python dictionary and have a single lookup/return for your values rather than multiple if/elif/else.

def landuse_groupFromName(LUDCODE):    
    lookupDict = {
                   1: 'Agriculture',
                   2: 'Agriculture',
                  23: 'Agriculture',
                  35: 'Agriculture',
                  36: 'Agriculture',
                   5: 'Commercial/Industry',
                  15: 'Commercial/Industry',
                  16: 'Commercial/Industry',
                  10: 'High_Density_Res',
                  11: 'High_Density_Res',
                  13: 'Low_Density_Res',
                  38: 'Low_Density_Res',
                  12: 'Med_Density_Res',
                   7: 'Recreation',
                   8: 'Recreation',
                   9: 'Recreation',
                  17: 'Recreation',
                  26: 'Recreation',
                  29: 'Recreation',
                  34: 'Recreation',
                  18: 'Transportation', 
                  31: 'Urban',
                  19: 'Waste',
                  39: 'Waste',
                  20: 'Water', 
                  14: 'Wetlands',
                  37: 'Wetlands',
                   3: 'Wild',
                   4: 'Wild',
                   6: 'Wild',
                  24: 'Wild', 
                  25: 'Wild',
                  40: 'Wild'
                 }
    if LUDCODE in lookupDict:
        return lookupDict[LUDCODE]
    else:
        return 'undefined'

As I mentioned at the start, if your values in the LUDCODE field are actually stored as text rather than as numeric you will need to surround every value with quote marks e.g. '1': 'Agriculture',

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.