+ Reply to Thread
Results 1 to 6 of 6

Thread: Error when trying to calculate a field

  1. #1
    Michael Miller
    Join Date
    Jun 2011
    Posts
    119
    Points
    18
    Answers Provided
    4


    0

    Default Error when trying to calculate a field

    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:
    Code:
    <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:
    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)
    Michael A Miller
    GIS Analyst
    Idaho Transportation Department


    ArcGIS 10 SP 3 - ArcInfo License
    Windows 7 SP1 64-bit

  2. #2
    Mathew Coyle

    Join Date
    Feb 2011
    Posts
    1,392
    Points
    969
    Answers Provided
    151


    0

    Default Re: Error when trying to calculate a field

    I assume it is this line you are having the problem with?
    Code:
    arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")
    Have you tried doing that manually in ArcMap to see if your results vary?
    Mathew Coyle, EADA10
    GIS Analyst
    Alberta-Pacific Forest Industries Inc.
    ArcGIS 10.2.2 Testing
    ArcGIS 10.1 SP1
    Desktop, Server, ArcPad
    Windows 7 SP1 64-bit

  3. #3
    Michael Miller
    Join Date
    Jun 2011
    Posts
    119
    Points
    18
    Answers Provided
    4


    0

    Default Re: Error when trying to calculate a field

    Quote Originally Posted by mzcoyle View Post
    I assume it is this line you are having the problem with?
    Code:
    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.
    Michael A Miller
    GIS Analyst
    Idaho Transportation Department


    ArcGIS 10 SP 3 - ArcInfo License
    Windows 7 SP1 64-bit

  4. #4
    Mathew Coyle

    Join Date
    Feb 2011
    Posts
    1,392
    Points
    969
    Answers Provided
    151


    1

    Default Re: Error when trying to calculate a field

    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.
    Mathew Coyle, EADA10
    GIS Analyst
    Alberta-Pacific Forest Industries Inc.
    ArcGIS 10.2.2 Testing
    ArcGIS 10.1 SP1
    Desktop, Server, ArcPad
    Windows 7 SP1 64-bit

  5. #5
    Curtis Price

    Join Date
    Oct 2009
    Posts
    1,810
    Points
    875
    Answers Provided
    127


    1
    This post is marked as the answer

    Default Re: Error when trying to calculate a field

    Quote Originally Posted by pfxmichaelm View Post
    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!)
    Last edited by curtvprice; 02-25-2013 at 11:40 AM.

  6. #6
    Michael Miller
    Join Date
    Jun 2011
    Posts
    119
    Points
    18
    Answers Provided
    4


    0

    Default Re: Error when trying to calculate a field

    Quote Originally Posted by curtvprice View Post
    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.
    Michael A Miller
    GIS Analyst
    Idaho Transportation Department


    ArcGIS 10 SP 3 - ArcInfo License
    Windows 7 SP1 64-bit

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts