Select to view content in your preferred language

How to Sum a Listing of Counts

942
3
Jump to solution
06-27-2012 11:14 AM
JakobHowe
Emerging Contributor
I'm somewhat of a novice at Python but I created a script that performs a series of selections on some layers and then performs a count on each selection and displays the count in message window.  So far it's straight forward but I have two additionalcounts that require the summation of two other previous counts.  How do I achieve this summation of counts?  I've included my script below.  The last two line of the code are the ones in question that need to be summed.  Thank you for your help and advice, much appreciated.

[PHP]import arcpy

# Local variables:
ProductionGIS_GISADM_NCS_CLEARANCE = "ProductionGIS.GISADM.NCS_CLEARANCE"
ProductionGIS_GISADM_NCS_MILEPOST = "ProductionGIS.GISADM.NCS_MILEPOST"
ProductionGIS_GISADM_NCS_EQUATIONS = "ProductionGIS.GISADM.NCS_EQUATIONS"
ProductionGIS_GISADM_NCS_SIGN = "ProductionGIS.GISADM.NCS_SIGN"
ProductionGIS_GISADM_NCS_SIGNAL = "ProductionGIS.GISADM.NCS_SIGNAL"
ProductionGIS_GISADM_NCS_INTERLOCKING = "ProductionGIS.GISADM.NCS_INTERLOCKING"
ProductionGIS_GISADM_NCS_SWITCH = "ProductionGIS.GISADM.NCS_SWITCH"
ProductionGIS_GISADM_NCS_XING = "ProductionGIS.GISADM.NCS_XING"
ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events = "ProductionGIS.GISADM.NCS_SUB_CODE_EVT Events"
Output_Layer_Name = "ProductionGIS.GISADM.NCS_CLEARANCE"
Output_Layer_Name__2_ = "ProductionGIS.GISADM.NCS_MILEPOST"
Output_Layer_Name__3_ = "ProductionGIS.GISADM.NCS_EQUATIONS"
Output_Layer_Name__4_ = "ProductionGIS.GISADM.NCS_SIGN"
Output_Layer_Name__5_ = "ProductionGIS.GISADM.NCS_SIGNAL"
Output_Layer_Name__6_ = "ProductionGIS.GISADM.NCS_INTERLOCKING"
Output_Layer_Name__7_ = "ProductionGIS.GISADM.NCS_SWITCH"
Output_Layer_Name__8_ = "ProductionGIS.GISADM.NCS_XING"

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_CLEARANCE, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (2)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_MILEPOST, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (3)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_EQUATIONS, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (4)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_SIGN, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (5)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_SIGNAL, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (6)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_INTERLOCKING, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (7)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_SWITCH, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

# Process: Select Layer By Location (8)
arcpy.SelectLayerByLocation_management(ProductionGIS_GISADM_NCS_XING, "INTERSECT", ProductionGIS_GISADM_NCS_SUB_CODE_EVT_Events, "", "NEW_SELECTION")

result1 = arcpy.GetCount_management(Output_Layer_Name)
featurecount1 = int(result1.getOutput(0))

result2 = arcpy.GetCount_management(Output_Layer_Name__2_)
featurecount2 = int(result2.getOutput(0))

result3 = arcpy.GetCount_management(Output_Layer_Name__3_)
featurecount3 = int(result3.getOutput(0))

result4 = arcpy.GetCount_management(Output_Layer_Name__4_)
featurecount4 = int(result4.getOutput(0))

result5 = arcpy.GetCount_management(Output_Layer_Name__5_)
featurecount5 = int(result5.getOutput(0))

result6 = arcpy.GetCount_management(Output_Layer_Name__6_)
featurecount6 = int(result6.getOutput(0))

result7 = arcpy.GetCount_management(Output_Layer_Name__7_)
featurecount7 = int(result7.getOutput(0))

result8 = arcpy.GetCount_management(Output_Layer_Name__8_)
featurecount8 = int(result8.getOutput(0))

arcpy.AddMessage("Clearance Point Count = " + str(featurecount1))
arcpy.AddMessage("Milepost Point Count = " + str(featurecount2))
arcpy.AddMessage("Equations Point Count = " + str(featurecount3))
arcpy.AddMessage("Sign Point Count = " + str(featurecount4))
arcpy.AddMessage("Signal Point Count = " + str(featurecount5))
arcpy.AddMessage("Interlocking Point Count = " + str(featurecount6))
arcpy.AddMessage("Switch Point Count = " + str(featurecount7))
arcpy.AddMessage("Xing Point Count = " + str(featurecount8))
arcpy.AddMessage("Nodes Point Count = " + str(featurecount3)+str(featurecount7))
arcpy.AddMessage("Milepost Helper Point Count = " + (str(featurecount3)+str(featurecount7)+str(featurecount5)+str(featurecount2)))[/PHP]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
I would sum like this.

arcpy.AddMessage("Milepost Helper Point Count = {0}".format(featurecount3+featurecount7+featurecount5+featurecount2))

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Honored Contributor
I would sum like this.

arcpy.AddMessage("Milepost Helper Point Count = {0}".format(featurecount3+featurecount7+featurecount5+featurecount2))
0 Kudos
JakobHowe
Emerging Contributor
This works just as you said, thank you so much for the quick reply. Regards.
0 Kudos
MathewCoyle
Honored Contributor
This works just as you said, thank you so much for the quick reply. Regards.


No problem.

And if you want something a little prettier, you can do something like this. Untested and not really optimal, but a step in the pythonic direction.

import arcpy

# Local variables:
events = "ProductionGIS.GISADM.NCS_SUB_CODE_EVT Events"

layer_list = ["ProductionGIS.GISADM.NCS_CLEARANCE",
"ProductionGIS.GISADM.NCS_MILEPOST",
"ProductionGIS.GISADM.NCS_EQUATIONS",
"ProductionGIS.GISADM.NCS_SIGN",
"ProductionGIS.GISADM.NCS_SIGNAL",
"ProductionGIS.GISADM.NCS_INTERLOCKING",
"ProductionGIS.GISADM.NCS_SWITCH",
"ProductionGIS.GISADM.NCS_XING"
]

result_list = []
nodes_list = ["Equations","Switch"]
milepost_additions = ["Milepost","Signal"]
node_count = 0
milepost_count = 0

for layer in layer_list:
    arcpy.SelectLayerByLocation_management(layer, "INTERSECT", events, "", "NEW_SELECTION")
    result = arcpy.GetCount_management(layer)
    result_list.append([layer.split("_")[-1],int(result.getOutput(0))])

for name,count in result_list:
    name = name.capitalize()
    arcpy.AddMessage("{0} Point Count = {1}".format(name,count))
    if name in nodes_list:
        node_count += count
    if name in milepost_additions:
        milepost_count += count

arcpy.AddMessage("Nodes Point Count = {0}".format(node_count))
arcpy.AddMessage("Milepost Helper Point Count = {0}".format(node_count+milepost_count))
0 Kudos