0

I am trying to populate a field with a L/R bank designation based on a route measure (as part of a larger script), but I am struggling. I have a shapefile created (tir_pts_lyr), I have a blank column (L_R_Bank), I have the measure value column (M_off_str). So if the measure value is negative, I want "L_R_Bank" to be populated with an "L", if the measure is positive, an "R", and if zero, a "C".
I'm trying to use the sample codeblocks in the ArcHelp, but they are lacking in detail for my knowledge level. I have no clue what to put in the expression field for getClass and how the next codeblock line relates to that. Any hints for a newbie.

#calc left/right bank using M-distance measure
# Set local variables
inTable = "tir_pts_lyr"
fieldName = "L_R_Bank"
expression = "getClass(float(!SHAPE.M_Off_str!))"
codeblock = """def getClass(M_Off_str):
    if M_Off_str < 0:
        return "L"
    if M_Off_str > 0:
        return "R"
    else:
        return "C" """
#Execute CalculateField
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON", codeblock)
1

Through blind luck, I seemed to have solved it by removing the SHAPE. out of the expression.

No idea if this is the best way to do this, but it works, so I'm going with it!

  • In the expression you are just calling the function that you define in the codeblock. You are passing one parameter to your function. You just need to specify the field name or other piece of data to send to the function and as you discovered, do not need to add "SHAPE" unless you are trying to access the geometry of your Shapefile (ex. !SHAPE.area! ). SHAPE is only used for geometry and not used when trying to pass a regular field to your function. – Adam Apr 19 '14 at 3:52

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.