Select to view content in your preferred language

Bug?: File GDB: Add Join, SelectbyAttribute, Calculate Field

2589
2
Jump to solution
10-12-2012 09:45 AM
ErikMartin
Frequent Contributor
I am having a problem with a file geodatabase join (2 feature classes) that is not happening when I use shapefiles.  I am using ArcGIS 10 SP5.  Here is my problem & workflow.


  1. Take Feature Class 1 and add a new field

  2. calculate that field to equal an existing field

  3. join the feature class to Feature Class 2

  4. use select by attributes to create a selection

  5. calculate the new field to equal something else, for only the selected records.  The new value is the sum of 2 fields of the same name in the 2 feature classes

  6. clear the selection and remove the join

The problem is that after I calculate the new field to equal something new, all of the old values (calculated in step 2) are set to null for records that do not join.  Here is my code:

import arcpy      Workspace = r"C:\Users\me\Desktop\test.gdb" arcpy.env.overwriteOutput = True arcpy.env.workspace = Workspace  FC = "PointFC1" FC2 = "PointFC2"   arcpy.AddField_management(FC, "newAttributeX", "DOUBLE") arcpy.CalculateField_management(FC, "newAttributeX", '[AttributeX]')  arcpy.MakeFeatureLayer_management(FC, "FC_lyr") FC_lyr = "FC_lyr"  #Join FCs and create selection arcpy.AddJoin_management(FC_lyr, "JoinField", "FC2", "JoinField") selection = '"%s.OBJECTID" IS NOT NULL' %FC2 arcpy.SelectLayerByAttribute_management(FC_lyr, "NEW_SELECTION", selection )  #Calc values for newAttributeX that join to second FC expression = "[%s.AttributeX] + [%s.AttributeX]" %(FC, FC2) arcpy.CalculateField_management(FC_lyr, "%s.newAttributeX" %FC, expression)   arcpy.SelectLayerByAttribute_management(FC_lyr, "CLEAR_SELECTION") arcpy.RemoveJoin_management (FC_lyr, "FC2")


Again, everything up to the selection is working properly... the problem is that all the null values (from those records that didn't join) are copied over to the new field, when in fact the field calculation should only be working on the selected records.

A viable work around is to wait to calculate the old values until the end:

  1. Take Feature Class 1 and add a new field

  2. join the feature class to Feature Class 2

  3. use select by attributes to create a selection

  4. calculate the new field to equal something else, for only the selected records.  The new value is the sum of 2 fields of the same name in the 2 feature classes

  5. clear the selection and remove the join

  6. select the values in the new field that didn't join

  7. calculate the null values in the new field to equal the values an existing field

However, the behavior I am getting is contrary to the documentation -- only the selected records should be calculated.  And as i mentioned above, this problem is not happening when I use 2 shapefiles.

Anyone else come across this problem?
-Erik
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ErikMartin
Frequent Contributor
FYI, this was confirmed by Esri support to be a bug in ArcGIS 10 SP5, but it has been fixed in 10.1:


This is Sai from Esri Tech Support and I have taken ownership of your incident #1086756. Thank you for providing the forum link and the code for testing. I was able to reproduce the same issue at my end in ArcGIS v10.0 SP5 (It works fine with a shapefile and not with a Geodatabase feature class). However, when I tested the same issue with v10.1, I was able to confirm that this has already been fixed.

View solution in original post

0 Kudos
2 Replies
by Anonymous User
Not applicable
I could be wrong here but this is what it looks like to me is throwing off your script:

arcpy.MakeFeatureLayer_management(FC, "FC_lyr")
FC_lyr = "FC_lyr"


I do not think that the FC_lyr variable will be accessing the feature layer you created in the line above, but rather is just creating the literal text string of "FC_lyr".  I could be wrong here but this is the only thing that I see that looks a little off. 

Try this instead:

FC_lyr = "FC_lyr"
arcpy.MakeFeatureLayer_management(FC, FC_lyr)


or

FC_lyr = arcpy.MakeFeatureLayer_management(FC, "FC_lyr")
0 Kudos
ErikMartin
Frequent Contributor
FYI, this was confirmed by Esri support to be a bug in ArcGIS 10 SP5, but it has been fixed in 10.1:


This is Sai from Esri Tech Support and I have taken ownership of your incident #1086756. Thank you for providing the forum link and the code for testing. I was able to reproduce the same issue at my end in ArcGIS v10.0 SP5 (It works fine with a shapefile and not with a Geodatabase feature class). However, when I tested the same issue with v10.1, I was able to confirm that this has already been fixed.
0 Kudos