1

In my data I have one field (L1_SOILTYP) and a new field I created (SoilClass). I am trying to use update cursor to assign classification values (1-17) based on the values found in the L1_SOILTYP field. For example... 'Clay' and 'CLAY' should be assigned '1'. I am using ArcGIS Pro for this.

Following an example on this page... https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/updatecursor-class.htm ... this is the code I have come up with...

import arcpy
fc = 'D:\GIS\PollinatorProject\Pollinator\Pollinator.gdb\soil_samples'
fields = ['L1_SOILTYP', 'SoilClass2']

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        if(row[0] == 'CLAY,DUMP'or row[0] == 'CLAY(WEATHERED SHALE)'or row[0] == 'FILL,CLAY'):
            row[1]= '1'
        elif(row[0] == 'SAND' or 'TOO SANDY' or row[0] == 'Sand, coarse' or row[0] == 'COARSE SAND'):
             row[1]= '2'
        elif(row[0] =='Sandy Clay' or row[0] == 'SAND & CLAY' or row[0] == 'SAND SOME CLAY'):
            row[1] = '3'
      
        cursor.updateRow(row)

Using this code the clay values are all correctly assigned the 1 value, but all the other soil types are assigned to 2. This is a shortened version of what I have, but each time I run the code, all values are assigned '1'

2
  • 1
    I'd use a dictionary for this pattern. Load all the valid values in a single dictionary to start, and assign row[1] = options[row[0]] if row[0] in options else None. Then you can debug your assignment issues.
    – Vince
    Apr 27, 2021 at 2:25
  • What I mean to say is that all the clay values are assigned to 1 and all other values are assigned to 2 ie. sand, loam, concrete when I would like them to have unique values.
    – carlymar
    Apr 27, 2021 at 15:49

2 Answers 2

3

You can use a dictionary to shorten your code:

import arcpy
fc = r'D:\GIS\PollinatorProject\Pollinator\Pollinator.gdb\soil_samples'
fields = ['L1_SOILTYP', 'SoilClass2']

type_to_class = {'CLAY,DUMP':'1', 'CLAY(WEATHERED SHALE)':'1', 'FILL,CLAY':'1',
                 'SAND':'2', 'TOO SANDY':'2', 'Sand, coarse':'2', 'COARSE SAND':'2',
                 'Sandy Clay':'3', 'SAND & CLAY':'3', 'SAND SOME CLAY':'3'} #Add all to dictionary

missing_types = []

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        if row[0] in type_to_class: #If you can have leading/trailing whitespaces, use if row[0].strip()
            row[1] = type_to_class[row[0]] #And here
            cursor.updateRow(row)
        else:
            missing_types.append(row[0])

    if len(missing_types)>0:
        print('Cant find soil types: {0}'.format(set(missing_types)))
0
1

Using BERA's suggestion this is the dictionary that was successful in creating the soil class values.


fc= r'D:\GIS\PollinatorProject\Pollinator\Pollinator.gdb\soil_samples'
fields = ['L1_SOILTYP', 'ClassofSoil']

type_to_class = {
    'CLAY, DUMP': '1',
    'DRY CLAY': '1',
    'Clay' : '1',
    'CLAY' : '1',
    'CLAY MOIST' : '1',
    'CLAY(WEATHERED SHALE)' : '1',
    'FILL, CLAY' : '1',
    'SAND' : '2',
    'TOO SANDY' : '2',
    'Sand, coarse' : '2',
    'COARSE SAND' : '2',
    'Sand' : '2',
    'CLEAN SAND' : '2',
    'VERY SANDY' : '2',
    'SANDY' : '2',
    'FINE SAND' : '2',
    'Fine Sand' : '2',
    'COURSE SAND' : '2',
    'Coures Sand' : '2',
    'Sandy Clay' :'3',
    'SAND & CLAY' :'3',
    'SAND SOME CLAY' :'3',
    'Clayey Sand' :'3',
    'CLAY SOME SAND' :'3',
    'CLAY,SOME SAND' :'3',
    'Sandy Caly' :'3',
    'SANDY CLAY' :'3',
    'COURSE SAND SOME CLAY' :'3',
    'SILT' : '4',
    'Silit' : '4',
    'SILTY' : '4',
    'Silty' : '4',
    'Silt' : '4',
    'MEDIUM' : '5',
    'FIRM' : '5',
    'NO DUMP' : '5',
    'SLAB/PLASTIC' : '5',
    'DUMP' : '5',
    'Dump' : '5',
    'TRACES OF DUMP' : '5',
    'SLAB' : '5', 
    'SOME DUMP' : '5',
    'SOME WOOD' : '5',
    'REJECTION' : '5',
    'TO 6(2)' : '5',
    'VERY SOFT' : '5',
    'WOOD' : '5',
    '2)' : '5',
    'VERY DAMP' : '5',
    'FILL' : '5',
    'SLAG': '5',
    'ASPHALT/FILL' : '6',
    'ASPHALT' : '6',
    'asphalt is newer' : '6',
    'asphalt is new' : '6',
    'Gravel & Sand' : '7',
    'SAND & GRAVEL (STRUCTURAL FILL' : '7',
    'SILTY GRAVEL & SAND' : '7',
    'GRAVLE & SAND' : '7',
    'Sand w/ Gravel' : '7',
    'COURSE SAND & GRAVEL' : '7',
    'Sand w/ gravel' : '7',
    'SAND & GRAVEL' : '7',
    'GRAVEL & SAND' : '7',
    'CLAYEY SAND & GRAVEL' : '7', 
    'Sand & Gravel' : '7',
    'SAND & gRAVEL' : '7',
    'Sand & Pea Gravel': '7', 
    'SAND & GRAVEL SOME CLAY' : '7',
    'Gravel Ballast' : '8',
    'LARGE GRAVEL' : '8',
    'GRAVEL' : '8',
    'SiltyClay' : '9',
    'Clayey Silt' :'9',
    'SILTY CALY' : '9',
    'SILTY CLAY' : '9',
    'CLAYEY SILT' : '9',
    'GRASS/TOPSOIL' :'10',
    'TOP SOIL' :'10',
    'CLAY & GRAVEL' :'11',
    'Sandy Clay & Gravel' :'11',
    'DUMP, BRICK' : '12',
    'CONCRETE' : '12',
    'RED BRICK' : '12',
    'BRICK' : '12',
    'SAND SOME BRICK' : '12',
    'Silty Sand' : '13',
    'Sandy Silt' : '13',
    'SAND & SILT' : '13',
    'SANDY SILT'  : '13',
    'SILTY SAND' : '13',
    'FINE SAND SILTY' : '13',
    'BASALT & SHALE' : '14',
    'SHALE' : '14',
    'BROWN SHALE' : '14',
    'CLAY, CINDERS' : '15', 
    'CINDERS & SILT' : '15', 
    'CINDERS' : '15', 
    'SANDSTONE' : '16', 
    'CLAYEY SANDSTONE' : '16', 
    'ROCK' : '17', 
    'LOAM' : '18', 
    'n/a' : '<NULL>',
    ' ' : '<NULL>'
}

missing_types = []

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        if row [0] in type_to_class:
            row[1] = type_to_class[row[0]]
            cursor.updateRow(row)
        else: 
            missing_types.append(row[0])
    if len(missing_types)>0:
        print('Cant find soil types: {0}'.format(set(missing_types)))
    ```

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.