Calculate Field not working when iterating through a list of feature classes

759
8
05-20-2022 01:33 PM
AlfredBaldenweck
MVP Regular Contributor

Hi all,

I'm trying to calculate the same field in multiple feature classes in a list.

The first time it runs after opening Pro, it works great. However, each time after that, it will calculate the field for the first feature class in the list and ignore the rest.

Does anyone have any ideas as to what might be causing it?

 

 

FCList= [FC1, FC2]
for FC in FCList:
    arcpy.management.CalculateField(FC, 'SampleField', '"Hello world"', 'ARCADE')

 

 

 

Edit: 

My current best guess is that later in the script I delete the field that's being calculated, and that's what's causing it to freak out. Does anyone have any ideas for how to prevent that from happening? 

 

FCList= [FC1, FC2]
for FC in FCList:
    arcpy.management.CalculateField(FC, 'SampleField', '"Hello world"', 'ARCADE')

#code
#code
'''This is where the problem is, I think?'''
arcpy.management.DeleteField(FC, 'SampleField')
0 Kudos
8 Replies
RhettZufelt
MVP Frequent Contributor

 

Not showing enough code to have an idea as to what is in the FCList (what is FC1 equal to, etc.).

So, if you use the following code, do you see it listing ALL the feature classes?

Once you get it listing the appropriate FC's, then you can put the CalculateField back in there.

 

FCList= [FC1, FC2]
for FC in FCList:
    print(FC.name)

 

R_

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

This is a stripped down example, but yes.

I'm adding some functionality in the middle of the script, and my current set-up works for everything else I want to do. The FCList is a list of layers returned as a Parameter from the scripting tool.

In this case, I'm actually watching it create the field it needs in each FC if it isn't already there, so it's definitely working on the right things. I have also experimented with defining FC as FC.datasource to see if that was part of the problem, but it didn't really change anything.

This behaviour occurs regardless of if I'm using a scripting tool or the Python window in Pro.

Best I can tell, (and this is wildly inconsistent as to the exact numbers, which has made figuring this out really hard), the first run goes great. The second run may also go well, or only the first Feature class will get calculated. Similarly for the third run. Eventually neither feature class will get calculated. (Testing with a different expression each run, e.g. "Run1", "Run2").

I am using the same data for each run. 

 

My current best guess (based on some experimenting) is that later in the script I delete the field that's being calculated, and that's what's causing it to freak out. Does anyone have any ideas for how to prevent that from happening? 

0 Kudos
RhettZufelt
MVP Frequent Contributor

If it's running differently on each pass, the problem is obviously somewhere in script that isn't posted.

Mention scripting tool, Pro, parameters, and such.  sounds almost like a model, which changes everything with iterating.

If the script is passing the proper path/featureclass to the calculate tool, it should be calculating the values.

I'm actually watching it create the field it needs in each FC if it isn't already there, so it's definitely working on the right things.

Why not move the calculate field directly below the add field, and do it in one iteration.

Often it is easier to hard code the path in the tool, instead of a value passed from a variable/parameter.

Then, once it is working and not "freaking out", go back in and change to the parameter/variable substitution, and it will often make the issue more obvious.

R_

 

 

 

 

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Yes, at this point I am pretty sure that it is something about the field being deleted that is causing the issue; when I comment that out, it's fine, then the issue occurs on the next run immediately after uncommenting it. I have edited my original post, and mentioned this in my last comment.

I'm actually watching it create the field it needs in each FC if it isn't already there, so it's definitely working on the right things.

Why not move the calculate field directly below the add field, and do it in one iteration.

This is what I mean (from the Calculate Field Documentation page😞

If a field with the specified name does not exist in the input table, it will be added.

It's all being done in the same iteration. The tool is supposed to add a field, calculate it, and then delete it at the end because the field is not needed any longer. it seems that the deletion is causing issues.

The script is working on the right feature classes; I can tell because it's creating fields when trying to calculate them. Like I said, something about that field being deleted at the end of the tool causes the next run to be messed up.

To reiterate, with DeleteField() active at the end of the script, the Field Calculation will either not calculate at all or calculate the field as Null, with the exception of the first feature class in the list, which is fine.

This behavior begins as soon as DeleteField() is active, and can only be fixed by turning it off and restarting Pro.

If you are wondering why I keep running this on the same set of data over and over, it's for testing purposes.

0 Kudos
by Anonymous User
Not applicable

I’d throw in that it could be a possible dataset caching issue with Pro. It’s caching has caused odd results in several of my scripts, as well as others that I have helped troubleshoot for work. I close/ reopen Pro and the attributes are different. ‘Refresh’ or disconnecting from the database doesn’t work unfortunately. It got really bad one time and had mapped attributes from one featureclass to a different one, duplicating all of its fields with _1. Other datasets were ‘empty’, but get count returned with 45k results.  Hopefully they can get the caching working properly.

AlfredBaldenweck
MVP Regular Contributor

This is a good point. I was wondering if anyone had a way to clear the cache without closing Pro?

Edit: through scripting, not just going into Options.

Edit 2: Options> Clear Cache does not fix the problem.

It looks like Clear Workspace Cache (Data Management)—ArcGIS Pro | Documentation only works for Enterprise GDBs.

0 Kudos
BlakeTerhune
MVP Regular Contributor

To refresh the data, try switching the table of contents to list by data source, right click on the geodatabase, choose refresh.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

As it turns out, that is only available in Enterprise GDBs, and refreshing the map, the GDB in Catalog, and the Tables, don't do anything. 

I might have to chalk this up to an error. Not sure if to report it as a bug or request it as an Idea.

0 Kudos