Calculate Field Hangs

3203
6
Jump to solution
12-30-2015 09:46 AM
RichardBunten
New Contributor III

I have a large script that does a bunch of scheme changes, joins, calculates fields and other stuff to a file geodatabase.  After this script runs I immediately run another test script that joins two feature classes and calculates one field to another field.  When I run this script it hangs and won't complete.  However, if I duplicate this operation in ArcGIS manually (join two Feature classes, calculate the values from one field to another and remove join and close down ArcGIS) and then run the script again to calculate the field, it works.

I am wondering if I have a memory issue in my 1st script that is resolved when I run the operation manually in ArcGIS.

Does anyone have an idea of how to clear the memory.  These two feature classes have join operations and calculations done on them in the 1st script.  Do I need to remove "FeatureLayer" or "Join" from memory in my 1st script.

Let me know if you have any ideas.

0 Kudos
1 Solution

Accepted Solutions
RichardBunten
New Contributor III

I had been trying to use the UpdateCursor method, but had not been able to get it to work because I was using the AddJoin_management which creates a TableView.  UpdateCursor does not work with tableviews.  However, I finally figured out that I can use JoinField_management which appends the field I am trying to calculate from to the Feature Class.  I am then able to use UpdateCursor and then delete the field I joined to the Feature Class.  It works.  Thanks for your help.

View solution in original post

6 Replies
DarrenWiens2
MVP Honored Contributor

Does it help if you Remove Join?

RichardBunten
New Contributor III

I tried adding the RemoveJoin_management (in_layer_or_view, {join_name}) to the first script and it did not help and then added it to the second script and it did not solve the problem.  Thanks for the idea.

If I just bring the two feature classes into ArcGIS and join them and remove the join then it works.  I don't have to calculate anything.  Thought you idea would work.

thanks,

Richard

0 Kudos
JamesCrandall
MVP Frequent Contributor

It's difficult to make a suggestion without seeing any code, but have you considered replacing CalculateField_management with da.UpdateCursor?

I honestly don't know if that's going to help, but it's pretty much all guessing without digging into the details.

RichardBunten
New Contributor III

I had been trying to use the UpdateCursor method, but had not been able to get it to work because I was using the AddJoin_management which creates a TableView.  UpdateCursor does not work with tableviews.  However, I finally figured out that I can use JoinField_management which appends the field I am trying to calculate from to the Feature Class.  I am then able to use UpdateCursor and then delete the field I joined to the Feature Class.  It works.  Thanks for your help.

JamesCrandall
MVP Frequent Contributor

With the dictionary and da.UpdateCursor solution I provided you would not need any AddJoin_management, TableView or CalculateField_management at all. 

An alternative is to convert both FC's to NumPy arrays, do appropriate join, calculate the desired column and then can invoke arcpy.da.NumpyArrayToFeatureClass to return to the ESRI stack.  However this will mean a new output feature class rather than a simple field calculation.

Both have their places and I have implementations with each setup.  But I also do have simple AddJoin and CalculateField methods employed as well!   Just depends on which toolset is best suited to satisfy the requirements.

JamesCrandall
MVP Frequent Contributor

You can fill a dictionary and then use that in conjunction with the arcpy.da.UpdateCursor to calculate the field.  Something like this works and will likely be efficient for large sets compared to CalculateField_management:

def JoinAndCalcuWithUpdateCursor():
    baseFC = r'H:\Documents\ArcGIS\Default.gdb\[layer to update]'
    joinFC = r'H:\Documents\ArcGIS\Default.gdb\[layer to join with]'

    joincols= ['<join column>', '<column to use in calculate>']
    joindict = {}
    with arcpy.da.SearchCursor(joinFC, joincols) as rows:
        for row in rows:
            joinvals = row[0]
            updatevals = row[1]            
            joindict[joinvals]=[updatevals]

    del row, rows
    # un-comment if you need to add a column to hold the calculated values
    #arcpy.AddField_management(baseFC, "CalcField", "TEXT", "25")

    basecols = ['<join column>', '<column to calculate>']
    with arcpy.da.UpdateCursor(baseFC, basecols) as updRows:
        for updRow in updRows:
            keyval = updRow[0]
            if joindict.has_key(keyval):
                updRow[1] = joindict[keyval][0]
            else:
                updRow[1] = ''
            updRows.updateRow(updRow)
    del updRow, updRows