Error when trying to calculate a field

1135
5
Jump to solution
02-25-2013 08:32 AM
MichaelMiller2
Occasional Contributor III
Anyone tell me why this code is returning this error?  It creates the latTemp field, and I can see it after it crashes, but it will not calculate it.  

ERROR:
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000728: Field latSTR does not exist within table Failed to execute (CalculateField).  Failed to execute (ConvertDMS2DD). Failed at Mon Feb 25 11:25:18 2013 (Elapsed Time: 9.00 seconds)


CODE:
import arcpy, sys, os arcpy.env.overwriteOutput = 1  # select the feature class  fc = arcpy.GetParameter(0) # select the latitude field to convert latField2 = arcpy.GetParameter(1) latField = "%s%s%s" % ('!', latField2, '!') arcpy.AddMessage("latField2 = " + str(latField2) +"   latField = " +latField) lonField2 = arcpy.GetParameter(2) lonField = "%s%s%s" % ('!', lonField2, '!') arcpy.AddMessage("lonField2 = " + str(lonField2) +"  lonField = " +lonField) # set lattitude output field latFieldDD = "latDD" # set longitude output field lonFieldDD = "lonDD" # set temp lattitude field, used to store lat field as string used in function latTemp = "latSTR" # set temp longitude field, used to store lon field as string used in function lonTemp = "lonSTR"  latExp = "lat(%s%s%s)" % ('!',latTemp,'!') codeblockLAT = """def lat(dms):     latdeg = float(dms[0:2])     latmin = float(dms[2:4])     latsec = float(dms[4:6])     latdd = latdeg + (latmin / 60) + (latsec / 3600)     return latdd""" lonExp = "lon(%s%s%s)" % ('!',lonTemp,'!') codeblockLON = """def lon(dms):     londeg = float(dms[0:3])     lonmin = float(dms[3:5])     lonsec = float(dms[5:7])     londd = (londeg + (lonmin / 60) + (lonsec / 3600)) * -1     return londd"""  #copy the lat numeric to a string arcpy.AddField_management(fc, latTemp, "TEXT", "", "", 16) arcpy.AddField_management(fc, latFieldDD, "FLOAT", 16, 6, 16) arcpy.AddMessage("added lat fields: %s and %s" % (latTemp, latFieldDD))  #copy the lon numeric to a string arcpy.AddField_management(fc, lonTemp, "TEXT", "", "", 16) arcpy.AddField_management(fc, lonFieldDD, "FLOAT", 16, 6, 16) arcpy.AddMessage("added lon fields: %s and %s" % (lonTemp, lonFieldDD))  # do calculations arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON") arcpy.CalculateField_management(fc, latFieldDD, latExp, "PYTHON", codeblockLAT) arcpy.DeleteField_management(fc, latTemp)  arcpy.CalculateField_management(fc, lonTemp, lonField, "PYTHON") arcpy.CalculateField_management(fc, lonFieldDD, lonExp, "PYTHON", codeblockLON) arcpy.DeleteField_management(fc, lonTemp)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
I was successful running the entire script using the same test data set before converting to a script tool.  Seems to be behaving differently in the script tool.


You are using GetParameter(), which returns an arcpy Field object, not a field name -- if you set it up as a script tool. From the command line, or IDLE, etc., the parameter is always a string, but from a toolbox, ArcGIS can pass the argument as a bona-fide arcpy object.

If this is a Field parameter you should use GetParameterAsText() if you want the field name as text - for example, for use in a Calculate Field expression. Python's  sys.argv and arcpy.GetParameterAsText are fairly equivalent, although I am pretty sure GetParameterAsText can handle a longer string. (In code examples, you usually see GetParameterAsText because this allows the script to used easily inside or outside ArcGIS.)

Note, there is a new tool in 10x that does DMS to DD conversion, at least many of the conversions you'd want: ConvertCoordinateNotation_management. (I had been asking for this tool since the 1980's!)

View solution in original post

0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
I assume it is this line you are having the problem with?
arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")

Have you tried doing that manually in ArcMap to see if your results vary?
0 Kudos
MichaelMiller2
Occasional Contributor III
I assume it is this line you are having the problem with?
arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")

Have you tried doing that manually in ArcMap to see if your results vary?



Matt,

Yes, that's the line. I was successful running the entire script using the same test data set before converting to a script tool.  Seems to be behaving differently in the script tool. Strange that if the latTemp filed is already in the attribute table it will work.
0 Kudos
MathewCoyle
Frequent Contributor
What data types are you passing as parameters? You may want to consider using astext instead of parameter.

arcpy.GetParameterAsText()


I'd wager your problem as more to do with your latField variable than your latTemp variable.
0 Kudos
curtvprice
MVP Esteemed Contributor
I was successful running the entire script using the same test data set before converting to a script tool.  Seems to be behaving differently in the script tool.


You are using GetParameter(), which returns an arcpy Field object, not a field name -- if you set it up as a script tool. From the command line, or IDLE, etc., the parameter is always a string, but from a toolbox, ArcGIS can pass the argument as a bona-fide arcpy object.

If this is a Field parameter you should use GetParameterAsText() if you want the field name as text - for example, for use in a Calculate Field expression. Python's  sys.argv and arcpy.GetParameterAsText are fairly equivalent, although I am pretty sure GetParameterAsText can handle a longer string. (In code examples, you usually see GetParameterAsText because this allows the script to used easily inside or outside ArcGIS.)

Note, there is a new tool in 10x that does DMS to DD conversion, at least many of the conversions you'd want: ConvertCoordinateNotation_management. (I had been asking for this tool since the 1980's!)
0 Kudos
MichaelMiller2
Occasional Contributor III
You are using GetParameter(), which returns an arcpy Field object, not a field name -- if you set it up as a script tool. From the command line, or IDLE, etc., the parameter is always a string, but from a toolbox, ArcGIS can pass the argument as a bona-fide arcpy object.

If this is a Field parameter you should use GetParameterAsText() if you want the field name as text - for example, for use in a Calculate Field expression. Python's  sys.argv and arcpy.GetParameterAsText are fairly equivalent, although I am pretty sure GetParameterAsText can handle a longer string. (In code examples, you usually see GetParameterAsText because this allows the script to used easily inside or outside ArcGIS.)

Note, there is a new tool in 10x that does DMS to DD conversion, at least many of the conversions you'd want: ConvertCoordinateNotation_management. (I had been asking for this tool since the 1980's!)



What data types are you passing as parameters? You may want to consider using astext instead of parameter.

Code:
arcpy.GetParameterAsText()
I'd wager your problem as more to do with your latField variable than your latTemp variable.


Matt and Curtis,

Thank you very much for pointing this out to me. Yes, it now works as expected.

Thanks again.

Also, Curtis thanks for pointing out the new tool to do this and much  much more.
0 Kudos