Back to all posts

Script example: Summarize total pipe length group by an attribute

This script example summarizes the total pipe length, grouped by selected attributes in the Conduits model layer. The attribute can be a model or user-defined attribute. In this example, the user-defined attributes are the cross-section shape and roughness factor.


# defines a function called length_summary
def length_summary(conduits, by_parameter):
  # calculate length summary based on by_parameter
  dict = {}
  for con in conduits:        # iterate each conduit
    val = con[by_parameter]   # get the user-defined attributes
    length = con['Length']    # get the conduit length
    if not val in dict:
      dict[val] = 0.
    dict[val] += length       # create a dictionary to group length by user-defined attribute
  return dict

# create a customized print in order to format values (i.e., rounded to two decimals)
def myprint(dict):            
  for key, value in dict.iteritems():
    print key, '%.2f' % value

conduits = pcpy.Map.Layer["Conduits"].get_entities()   # get all Conduit entities
myprint (length_summary(conduits, 'XSection'))         # call the function and print results by cross-section shape
myprint (length_summary(conduits, 'Roughness'))        # call the function and print results by roughness factor