Back to all posts

Script example: Set layer attribute based on another attribute

This script example sets any attribute value in a model layer or background layer based on another attribute value in the same layer. For example, users may use it to set up conduit Manning's n value based on the pipe material and to set up Hazen-Williams C value for all forcemains.

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

Download script

# Sets attribute values based on other attributes using lookup tables

# Lookup tables (dictionaries) are defined on lines 17 and 21

# define the function
def set_attribute(entities, lookup_table, use_attr_name, set_attr_name):
  for entity in entities:
    use_attr_value = entity[use_attr_name]          # e.g. PVC material
    if use_attr_value in lookup_table:
      set_attr_value = lookup_table[use_attr_value] # e.g. 0.013
      entity[set_attr_name] = set_attr_value
# get entities to be edited
conduits = pcpy.Map.Layer['Conduits'].get_entities()

# define and apply look-up table to set conduit roughness based on pipe material
roughness_lookup_table = {'PVC':0.013, 'DI':0.012, 'CI': 0.012}
set_attribute(conduits, roughness_lookup_table, 'Material', 'Roughness')

# define and apply look-up table to set all forcemains to a Hazen-Williams C value of 140
forcemain_lookup_table = {'FORCE_MAIN':140}  
set_attribute(conduits, forcemain_lookup_table, 'XSection', 'Geom2')

# This example script is provided as is: CHI cannot guarantee its accuracy, 
# completeness or suitability for any particular purpose. All sample data is 
# fictional and should be replaced with reasonable values. The modeller is 
# obligated to verify the applicability of the content presented and exercise 
# professional engineering judgement throughout the modeling exercise.


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.