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 setup conduit Manning's n value based on pipe material, and to setup Hazen-Williams C value for all forcemains.


# 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.