5 Replies Latest reply on Feb 25, 2013 10:49 AM by mimiller

    Error when trying to calculate a field

    mimiller
      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)
        • Re: Error when trying to calculate a field
          mzcoyle
          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?
          • Re: Error when trying to calculate a field
            mimiller
            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.
            • Re: Error when trying to calculate a field
              mzcoyle
              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.
              • Re: Error when trying to calculate a field
                curtvprice
                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!)
                • Re: Error when trying to calculate a field
                  mimiller
                  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.