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.

This script is provided as-is, please see the disclaimer below for more information.

Download script

# 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

Disclaimer

All scripts are provided as-is, and no warranty is made as to the script's accuracy, completeness, or suitability for any particular purpose. This script may contain errors and require correction or modification by the modeler before its use.

The modeler is obligated to review the script code in its entirety and determine its suitability for its intended usage.

This script may also be updated from time to time to fix issues or provide enhanced functionality. If you have suggestions for improvement, please create a support ticket to let us know.