0

I'm quite new to ArcGIS, and now I have to work on a script that goes through a list of feature classes to:

  1. add a field to their attribute table and
  2. fill that field with a number that I retrieve from somewhere else

When I execute the following statements in the ArcMap Python window or in a minimal standalone script, it works just fine:

table = "JS4A"
arcpy.AddField_management(table, "BK_Code", "LONG")
arcpy.CalculateField_management(table, "BK_Code", "1", "PYTHON_9.3", "")

I can then open the attribute table of that feature class in ArcMap and see that this field has been added and populated with all 1s, just as expected.

Now the weird thing is, when I run the exact same code in the complex production script, the field is added as well, but when I open the attribute table, all the values are <Null>.

I managed to track the problem down to the following line of code in my production script, that comes before the part with AddField and CalculateField:

env.extent = desc.extent

where desc is the return value of an arcpy.Describe() call to some other, unrelated shapefile. All CalculateField_management calls before that line will work fine, all calls after that line produce <Null>. When I replace the above line with a line that sets the output extent to a hard-coded value, it works.

I don't understand this at all. Is this an ArcGIS bug?

Unfortunately I can't omit the above line because it is required in the production script.

3
  • 2
    Rather than so many sequential edits and especially while no one has yet offered an answer, I recommend that you revise your question to just include the critical details of where you are currently up to. At the moment it is a moving target. I am pretty sure that the issue will be procedural and not a bug.
    – PolyGeo
    Commented Jul 11, 2013 at 9:37
  • Sorry, I thought it would maybe help to illustrate everything I've tried so far. But I updated my post now that it seems clear that the output extent line is the root of the problem.
    – Richard
    Commented Jul 12, 2013 at 6:24
  • @Richard. Extent is an environment that the Calculate Field tool honors (if the input is spatial and not a table). Setting an extent that is outside the extent of the data will mean no records will be updated. More: gis.stackexchange.com/questions/136650/…
    – DWynne
    Commented May 20, 2015 at 0:05

3 Answers 3

1

I just ran the test below using ArcGIS for Desktop 10.1 SP1 and it ran fine:

import arcpy

desc = arcpy.Describe("C:/temp/test6.shp")
arcpy.env.extent = desc.extent

table = "C:/temp/test.gdb/test2"
arcpy.AddField_management(table, "BK_Code", "LONG")
arcpy.CalculateField_management(table, "BK_Code", "1", "PYTHON_9.3", "")

Is "1" actually hardcoded in your script?

If not, and unless you have a selection on table, I can't see how you can be getting <Null> rather than 1 in your table from CalculateField_management.

7
  • In the production script it's not supposed to be hardcoded, but I wanted to break it down to the simplest example possible.
    – Richard
    Commented Jul 14, 2013 at 7:25
  • Since the "simplest example possible" does not show the error I think you will need to try and give us code for "the next most simple example possible =" :-)
    – PolyGeo
    Commented Jul 14, 2013 at 8:41
  • I explained it all in the initial post. The least complex example that breaks it is when a line like env.extent = desc.extent comes BEFORE the calc field call
    – Richard
    Commented Jul 16, 2013 at 6:38
  • Check the code in my Answer - that is already the situation there - and it works!
    – PolyGeo
    Commented Jul 16, 2013 at 6:53
  • I tried the very same code (with different filenames obviously), and I get the <Null> result. If I write env.extent = arcpy.Extent(13.6, 51.0, 13.9, 51.2) it works. ArcMap 10.0 SP 5
    – Richard
    Commented Jul 17, 2013 at 11:22
1

I'm assuming you are running other tools as you shouldn't need to set the extents to add/calculate field. So my question would be why does it matter if calc field is before or after the line that is giving you difficulties?

That aside, it's hard to tell, but you might be having this issue...

When running a script from within ArcMap, you can drag and drop features from the table of contents, and it will run fine without the filename extension or setting the workspace as the current workspace is the default.

However, when running a standalone script, you must provide the extension (and optionally the workspace so that way all you need to specify is the file in the folder). If not specifying the workspace, you need to include the full path every time you access a file. For example:

arcpy.env.workspace = "D:/Data/"
arcpy.AddField_management("JS4A.shp", "Bk_code", "LONG")
1
  • I'm not at work just now to try what you describe, but the thing is that finding the correct shape file isn't the problem. As I described in my initial post, the AddField call works fine under all circumstances, as does the CalculateField call when I omit the problematic line. The reason why I need to limit the output extent is that later on in the script comes some FeatureToRaster conversion which otherwise would take ages.
    – Richard
    Commented Jul 12, 2013 at 6:21
0

The data type of the BK_Code field is LONG. Doesn't this mean a hard-coded 1 should not be wrapped in quotes? And if it is, and the system doesn't automatically cast the string "1" to a LONG, would that cause a NULL entry rather than an error message?

1
  • I derived the code from what the Model Builder tool exported as a Python script, and the 1 was wrapped in quotes there. Also note that it works under all circumstances except the one where the call comes after the line with the output extent.
    – Richard
    Commented Jul 14, 2013 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.