0

I am attempted to calculate a field in a feature class ("Feat_Class_1", by using the count of selected features within another feature class ("Feat_Class_2") as a numerator, while using the value of a field in Feat_Class_1 as the denominator. I am iteratively selecting each row in Feat_Class_1 to calculate this. The script executes successfully right up until the end, when the selected row retains 0 as its value (I am expecting a proportional value). My script is as follows:

## Select Routes which are less than 800m, count the values and retain as denominator
feat_class_2_subset = str(feat_class_2+"_short")
print feat_class_2_subset
arcpy.MakeFeatureLayer_management(feat_class_2, feat_class_2_subset)
arcpy.SelectLayerByAttribute_management(feat_class_2_subset, "NEW_SELECTION", "Total_Kilometers < 0.8")
result = arcpy.GetCount_management(feat_class_2_subset)
count = str(result.getOutput(0))
print count ##<-to confirm that the subset has been successfully selected
feat_class_1_subset = str(feat_class_1+"_individual_record")
print feat_class_1_subset
arcpy.MakeFeatureLayer_management(feat_class_1, feat_class_1_subset)
selection_sub_expression = str("'"+SA2_String+"'")
print selection_sub_expression
selection_expression = str("SA2_String = "+selection_sub_expression)
print selection_expression
arcpy.SelectLayerByAttribute_management(feat_class_1_subset, "NEW_SELECTION", selection_expression2)
test_results = arcpy.GetCount_management(feat_class_1_subset)
test_count = str(test_results.getOutput(0))
print test_count ##<-to determine if the subset has been selected properly
exp = str(count+"/!Total_Properties!")
print exp
arcpy.CalculateField_management(feat_class_1_subset, "Train_800m_Perc", exp, "PYTHON", "")

This works right up until the "test_count" component but then fails to actually execute the calculation.

Any ideas on where I'm going wrong? I've been careful to ensure my expression is a string for inclusion in the function.

  • 1
    Hey Xavier - On line 16, arcpy.SelectLayerByAttribute(..., you've got a variable called selection_expression2 placed as an argument to the function. Where is this variable defined? I'm looking around the rest of the script and I'm not seeing a selection_expression2 variable defined - this might be the issue? Or, is this variable defined somewhere earlier in the script (not shown here in this question)? This could just be a typo, could the correct variable name just be selection_expression?. – Jim Jun 20 '16 at 15:27
  • Hi Jim it should actually be selection_expression (and it is in my script) just a typo here randomly, good spotting thanks! – Xavier Goldie Jun 20 '16 at 23:23
3

I believe the issue is that you are giving two integers in the CalculateField expression, so an integer is returned. You need to force one to a float or decimal to get a proportional value back. Try:

exp = str(float(count)) + "/!Total_Properties!"
  • 1
    exp = "{} / !Total_Properties!".format(float(count)) - I think always safer to use format() to concatenate rather than + especially when handling numbers – Midavalo Jun 20 '16 at 18:23
  • Thanks @Midavalo that worked a treat. I've never had a difficulty forcing integers into float proportions via a string formatted expression, but it seems to have reared its ugly head here. Cheers! – Xavier Goldie Jun 21 '16 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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