1

I am (re-)learning ArcGIS for a new job after couple of years on PostGIS (and QGIS/pyqgis) and can't figure out how I can address a Field within the pre-logic-block of the field calculator.

Pre-Logic-Field:

def createStyleField(kindofuse):
    if kindofuse == 'wa':
        listofrelevantfields= {
            1: ['level1', 'level1_wa'],
            2: ['level2', 'level2_wa']
        }
    elif kindofuse == 'ge':
        listofrelevantfields= {
            1: ['level1', 'level1_ge'],
            2: ['level2', 'level2_ge']
        }
    elif kindofuse == 'gi':
        listofrelevantfields= {
            1: ['level1', 'level1_gi'],
            2: ['level2', 'level2_gi']
        }
    results = {}
    for levelkuerzel, liste in listofrelevantfields.iteritems():
        for entry in liste:
            if '!' + entry + '!' == 1: ## THIS IS WHERE I AM TRYING TO ADDRESS A FIELD
                results[levelkuerzel] = True
                break
            else:
                continue
    for i in range(1, 5) : #up to 5 levels, the lowest will be returned
        try:
            if results[i] == True:
                return unicode(i)
        except KeyError as err:
            continue
        except Exception as err:
            raise err
    return 0

And the other Field:

createStyleField('wa')

I need this for a couple of more complex calculations with a big number and case-dependent selection of fields. How do I do this?

Or should I address this issue totally different?

1

I found a solution for this case by not addressing a field dynamically, but there are other cases where dynamically choosing of field come in handy...

Especially because it will be used later in an arcpy-script.

Pre-Logic:

def createStyleField(listofrelevantfields):
##    listofrelevantfields = {}
##    if kindofuse == 'wa':
##        listofrelevantfields= {
##            1: ['level1', 'level1_wa'],
##            2: ['level2', 'level2_wa']
##        }
##    elif kindofuse == 'ge':
##        listofrelevantfields= {
##            1: ['level1', 'level1_ge'],
##            2: ['level2', 'level2_ge']
##        }
##    elif kindofuse == 'gi':
##        listofrelevantfields= {
##            1: ['level1', 'level1_gi'],
##            2: ['level2', 'level2_gi']
##        }
    results = {}
    for levelkuerzel, liste in listofrelevantfields.iteritems():
        for entry in liste:
##            if '!' + entry + '!' == 1:
            if entry == 1:
                results[levelkuerzel] = True
                break
            else:
                continue
    for i in range(1, 5) :
        try:
            if results[i] == True:
                return unicode(i)
        except KeyError as err:
            pass
        except Exception as err:
            raise err
    return 0

Calling it:

createStyleField({1: [!level1!, !level1_wa!], 2: [!level2!, !level2_wa!]})

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.