AnsweredAssumed Answered

Calculate Field with Code Block Python Script?

Question asked by LANDVEST on Nov 14, 2018
Latest reply on Nov 15, 2018 by Dan_Patterson

I've spent quite a bit of time looking at all the similar issues on GeoNet and Stack Exchange but I still can't find an answer. I'm sure that someone will be quick to say that I should use a cursor, but this exact same code works in ModelBuilder just fine!

Is there the potential for a carriage return or something? I copied and pasted my codeblock so that might be a possibility.  Here's my code (the FC in question does indeed have a field called 'BEARING'):

# Add Quandrant Bearing Field
arcpy.AddField_management(BoundSplit, "Q_BEARING", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Calculate Quadrant Bearing
expression = """ DD_DMS_Line(!BEARING!,{0}) """.format(correction)
arcpy.AddMessage(expression)
codeblock = """def DD_DMS_Line(bearing, correction=0):
value = bearing + correction
if value <= 90:
numb = value
prefix = "N"
suffix = "E"
elif value > 90 and value < 180:
numb = 180 - value
prefix = "S"
suffix = "E"
elif value >= 180 and value < 270:
numb = value - 180
prefix = "S"
suffix = "W"
elif value > 270 and value <= 360:
numb = 360 - value
prefix = "N"
suffix = "W"
elif value > 360:
numb = value - 360
prefix = "N"
suffix = "E"

degrees = int(numb)
submin = abs( (numb - int(numb) ) * 60)
minutes = int(submin)
subseconds = abs((submin-int(submin)) * 60)
notation = prefix + str(degrees) + u"\u00b0" + str(minutes) + "\'" +\
str(subseconds)[0:5] + "\"" + suffix
return notation"""


arcpy.CalculateField_management(BoundSplit, "Q_BEARING", expression, "PYTHON", codeblock)

My expression appears to be fine (highlighted in this image).

ugh, been banging my head against the wall on this one!

Outcomes