I am working on making improvements to my collection of Python scripts to produce statistical data reports on my in-service infrastructure system. Currently, I am using a file GDB with Feature Class to Feature Class AND Summary Statistics tools with SearchCursor tool to produce the counts. However, as my infrastructure grows and becomes more complex, the script is becoming less efficient.
I figured out how to do a SearchCursor to give me the total counts of a point feature class per specific attribute criteria:
# ALL MEWCo POLES
with arcpy.da.SearchCursor("eSUPPORT_STRUCTURE", "SHAPE@", "POLE_OWNER = 806") as cursor:
rows = {row[0] for row in cursor}
count = 0
for row in rows:
count += 1
print "MEWCo Poles: %s" % count
However, what I am trying to do is take this same Cursor and use it to produce the total lengths on a polyline feature class from my existing code:
# OVERHEAD CONDUCTOR COUNTS
# Process: Primary Conductor FC to Temp GDB - OH Primary #1/0 CU
arcpy.FeatureClassToFeatureClass_conversion(ePRIMARY_CONDUCTOR, TempGDB_gdb, "OHPrimary10CU", "PLACEMENT = 500 AND CONDUCTOR_TYPE = '#1/0 CU' AND LIFE_CYCLE_STATUS = 8")
# Process: Summary Statistics - OH Primary #1/0 CU
arcpy.Statistics_analysis(OHPrimary10CU, OHPrimary10CU_Stats, "Shape_Length SUM", "")
I am a little stuck trying to work with a polyline FC versus a point FC. Appreciate your help
Solved! Go to Solution.
you are on the right track, but I'm going to give example using the newer da searchcursor (like in your first post), as it performs much better.
Something like this (need to make sure the field names are correct) should get you on track:
fc = "Y:\\MEWCo GIS System - LOCAL\\ELECTRIC SYSTEM\\GEODATABASEs\\TempGDB.gdb\\OHPrimary10CU_Statistics"
total = 0
fields = ['CONDUCTOR_TYPE','SUM_SHAPE_Length']
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
total += row[1]
print "There are {} feet of {} lines".format(row[1],row[0])
print "And a total of {} feet".format(total)
if you wanted to know the number of lines for each as well, just add FREQUENCY to the fields variable and (row[2]) to the print statement:
fc = "Y:\\MEWCo GIS System - LOCAL\\ELECTRIC SYSTEM\\GEODATABASEs\\TempGDB.gdb\\OHPrimary10CU_Statistics"
total = 0
fields = ['CONDUCTOR_TYPE','SUM_SHAPE_Length','FREQUENCY']
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
total += row[1]
print "There are {} features for {} feet of {} lines".format(row[2],row[1],row[0])
print "And a total of {} feet".format(total)
R_
Easy way to get the count of points in the feature class.
Make a feature layer out of it so you can apply the where clause (POLE_OWNER = 806), then get count on the feature layer to find total number of points:
supportStructs = "eSUPPORT_STRUCTURE"
expr = "POLE_OWNER = '806'"
arcpy.MakeFeatureLayer_management(supportStructs, "supportStructs_lyr",expr)
total_points = int(arcpy.GetCount_management("supportStructs_lyr").getOutput(0))
print(total_points)
Similar with the line statistics, no need to create temp feature class, just make feature layer to utilize the where clause, then run Statistics analysis on the feature layer:
expr = "PLACEMENT = 500 AND CONDUCTOR_TYPE = '#1/0 CU' AND LIFE_CYCLE_STATUS = 8"
arcpy.MakeFeatureLayer_management(ePRIMARY_CONDUCTOR, "ePRIMARY_CONDUCTOR_lyr",expr)
arcpy.Statistics_analysis("ePRIMARY_CONDUCTOR_lyr", OHPrimary10CU_Stats, "Shape_Length SUM", "")
This should give you an output table with just one record in it containing the total length for all lines matching the where clause.
(no case field)
Depends on your final goal. Could also change it so and remove the CONDUCTOR_TYPE field from the where clause, and add it to the case field the the statistics analysis. this will create a table with a row for each conductor type and the total lengths by type.
expr = "PLACEMENT = 500 AND LIFE_CYCLE_STATUS = 8"
arcpy.MakeFeatureLayer_management(ePRIMARY_CONDUCTOR, "ePRIMARY_CONDUCTOR_lyr",expr)
arcpy.Statistics_analysis("ePRIMARY_CONDUCTOR_lyr", OHPrimary10CU_Stats, "Shape_Length SUM", "CONDUCTOR_TYPE")
(I used Owner field as case field for example below)
R_
This is a huge help. I have a follow-up question to complete my script which will make my statistics workflow be much more efficient.
Here is the GDB Table that is produced:
My previous script(s) would look at each individual FC that was exported to a temp GDB and "Print" the value:
SC1 = arcpy.SearchCursor("Y:\\MEWCo GIS System - LOCAL\\ELECTRIC SYSTEM\\GEODATABASEs\\TempGDB.gdb\\OHPrimary10CU_Statistics")
field_name1 = 'SUM_SHAPE_Length'
for row in SC1:
print("Total Length of OH Primary Conductor(Feet) - #1/0 CU:")
print row.getValue(field_name1)
print("----------------")
I still want to be able to print the values from getValue for each Conductor type to the Python Shell. Having a difficult time figuring out the code to print each value from the SUM_Shape_Length field while displaying the CONDUCTOR_TYPE attribute.
The purpose of this is I have multiple python scripts composed into a batch file and the getValue that prints to the Python Shell is ALSO printed to a .TXT file.
Thank you in advance
you are on the right track, but I'm going to give example using the newer da searchcursor (like in your first post), as it performs much better.
Something like this (need to make sure the field names are correct) should get you on track:
fc = "Y:\\MEWCo GIS System - LOCAL\\ELECTRIC SYSTEM\\GEODATABASEs\\TempGDB.gdb\\OHPrimary10CU_Statistics"
total = 0
fields = ['CONDUCTOR_TYPE','SUM_SHAPE_Length']
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
total += row[1]
print "There are {} feet of {} lines".format(row[1],row[0])
print "And a total of {} feet".format(total)
if you wanted to know the number of lines for each as well, just add FREQUENCY to the fields variable and (row[2]) to the print statement:
fc = "Y:\\MEWCo GIS System - LOCAL\\ELECTRIC SYSTEM\\GEODATABASEs\\TempGDB.gdb\\OHPrimary10CU_Statistics"
total = 0
fields = ['CONDUCTOR_TYPE','SUM_SHAPE_Length','FREQUENCY']
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
total += row[1]
print "There are {} features for {} feet of {} lines".format(row[2],row[1],row[0])
print "And a total of {} feet".format(total)
R_
BINGO!!
Exactly what I was looking for and the script completes at a fraction of the time compared to my previous script(s). Appreciate your assistance.