Returning SUM of Values from a Field Within a Selection Set

3311
3
Jump to solution
10-31-2013 10:47 AM
DamonOsbourne
New Contributor II
Basically I want to select line features based on a polygon feature, then return a value that is the sum of all the features in a field that is within the selected set of line features.  Then do this for say, a hundred more feature classes.  I figured there would be a tool, called GetSum or something similar that would do this but did not see it in the Help.  I've explored using Summary Statistics, but that returns a table, not a simple value.  Here is what I've got.  Ideas?

# Create a list of all the polygon feature classes zonePolys = arcpy.ListFeatureClasses("Zone*")  for fc in zonePolys:     arcpy.AddField_management(fc,"SumOfValues","LONG")         arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")        arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)          # Calculate the sum of the values in the selected features in OriginalLayer_Lyr     SumValues = ??     # Take the sum value of the line features field and populate the polygon layer's new SumOfValues field with it     arcpy.CalculateField_management(fc,"SumOfValues",SumValues)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Damon,

Does each feature class only have one polygon feature?  You can use a search cursor to loop through the selected lines and get a sum of a field value.  Ex:

zonePolys = arcpy.ListFeatureClasses("Zone*")  for fc in zonePolys:     arcpy.AddField_management(fc,"SumOfValues","DOUBLE")         arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")        arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)          # Calculate the sum of the values in the selected features in OriginalLayer_Lyr     with arcpy.da.SearchCursor("OriginalLayer_Lyr", ["Length"]) as cursor:         sumValues = 0         for row in cursor:             sumValues += row[0]     # Take the sum value of the line features field and populate the polygon layer's new SumOfValues field with it     arcpy.CalculateField_management(fc,"SumOfValues",sumValues)


You will just need to change the field 'Length' to the field you want to sum.

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Damon,

Does each feature class only have one polygon feature?  You can use a search cursor to loop through the selected lines and get a sum of a field value.  Ex:

zonePolys = arcpy.ListFeatureClasses("Zone*")  for fc in zonePolys:     arcpy.AddField_management(fc,"SumOfValues","DOUBLE")         arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")        arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)          # Calculate the sum of the values in the selected features in OriginalLayer_Lyr     with arcpy.da.SearchCursor("OriginalLayer_Lyr", ["Length"]) as cursor:         sumValues = 0         for row in cursor:             sumValues += row[0]     # Take the sum value of the line features field and populate the polygon layer's new SumOfValues field with it     arcpy.CalculateField_management(fc,"SumOfValues",sumValues)


You will just need to change the field 'Length' to the field you want to sum.
0 Kudos
ChrisSnyder
Regular Contributor III
Use the sum() function. For example:

zonePolys = arcpy.ListFeatureClasses("Zone*")
for fc in zonePolys:
    arcpy.AddField_management(fc,"SumOfValues","LONG")
    arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")
    arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)
    someValue = sum([r[0] for r in arcpy.da.SearchCursor("OriginalLayer_Lyr",["FIELD_NM"])]) 
    arcpy.CalculateField_management(fc,"SumOfValues",someValue) #haha get it...
DamonOsbourne
New Contributor II
someValue, funny!  Thanks guys it worked.
0 Kudos