Discover total line lengths in Feature Classes

2202
7
04-25-2017 12:52 PM
LarryAdgate
Occasional Contributor

I have 38  line Feature Classes located on my Enterprise Server. Without merging the files into one Feature Class, is there a way to discover the grand total of all line lengths in all 38 Feature Classes.  

0 Kudos
7 Replies
MicahBabinski
Occasional Contributor III

Hi Larry,

I should think so. How about something like this:

import arcpy

# point the workspace to your enterprise geodatabase
arcpy.env.workspace = r"[path to enterprise GDB]"

# set the environment to overwrite output
arcpy.env.overwriteOutput = True

# list the line feature classes (might have to modify 
# this depending on how many FCs are in your workspace
linear_fcs = arcpy.ListFeatureClasses("*", "Arc")

# create a float variable to store the total length
total_length = 0.0

# copy each FC into the in memory workspace, add a 
# length attribute, and add the length of each to the total
for fc in linear_fcs:
    # copy
    arcpy.CopyFeatures_management(fc, r"in_memory\temp")
    # add length attribute
    arcpy.AddGeometryAttributes_management(r"in_memory\temp", "LENGTH", "FEET_US")
    # add the length of each feature to the total
    with arcpy.da.SearchCursor(r"in_memory\temp", ("LENGTH")) as cursor:
        for row in cursor:
            total_length += row[0]

# print the result
print("The total length is {} feet.".format(total_length)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍)

Hope this helps.

Micah

LarryAdgate
Occasional Contributor

Thank You, I will give this a try

0 Kudos
JonathanQuinn
Esri Notable Contributor

What about using Summarize Statistics?

The first example appears to summarize the SHAPE_LENGTH field into an output table.

LarryAdgate
Occasional Contributor

Thanks You, I getting a lot of very good Ideas how to make this happen,

Larry Adgate

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Instead of copying data sets into temporary workspaces, you should be able to use the fact that cursors can do projection on the fly.  The following code uses the ArcPy Walk function to find all polyline feature classes in a geodatabase and sum the length of all features in all of those feature classes:

import arcpy
import os

gdb = # path to GDB or GDB connection file
SR = # common spatial reference for calculating lengths

walk = arcpy.da.Walk(gdb, datatype="FEATURECLASS", type="POLYLINE")
sum_length = 0

for root, dirnames, filenames in walk:
    for name in filenames:
        sum_length += sum(
            length
            for length,
            in arcpy.da.SearchCursor(os.path.join(root,name),
                                     "SHAPE@LENGTH",
                                     spatial_reference=SR)
        )

print sum_length

If you don't wall all polyline feature classes, you could add some additional logic to exclude or include feature classes of interest.

LarryAdgate
Occasional Contributor

Hi Joshua, Thank You for your help on this issue, but this project has evolved into something much bigger. We are a water utility Company with hundreds of miles of pipeline, valves and various equipment. For water quality purposes, I have been asked to provide a total inventory of everything and that includes pipeline sizes & lengths and the various types of valves and equipment. Unfortunately not all our GIS fields are numeric but are text. As a solution, I could merge similar feature classes into one big feature class but this could be labor intensive. Your thoughts Please.

Larry    

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Honestly, it is difficult to provide specific suggestions without more information about the structure of tables within the DB and the schema of the tables.  The specifics of how the data is stored and structured can make a big difference on which approach makes sense, or doesn't make sense.  I am not sure how comfortable you are with sharing more information, or are even allowed to, but sharing more might help myself or others provide specific suggestions.

0 Kudos