Using Ruby to add Nodes to Subcatchments and Polygons in ICM InfoWorks and SWMM Networks
Using Ruby to add Nodes to ICM InfoWorks and SWMM Networks

Using Ruby to add Nodes to Subcatchments and Polygons in ICM InfoWorks and SWMM Networks

This Ruby code to add nodes to selected polygons could be important and useful to InfoWorks ICM and SWMM modelers for a few reasons:

  1. Allows automated subdivision of large subcatchments or polygons: By adding a centroid node and vertex nodes, this code provides a way to easily subdivide large subcatchments or polygons into smaller areas. This can help improve model accuracy. I like it for making test models to show how a feature works in ICM or SWMM.
  2. Facilitates parameter assignment: The added nodes allow modelers to easily assign different parameters (roughness, infiltration rate, etc) to different sections of a large subcatchment based on land use, soil type etc. Micro catchments.
  3. Enables detailed modeling: Adding nodes to polygons enables users to model each sub-section explicitly as a subcatchment with its own parameters. This allows for more detailed modeling.
  4. Supports result mapping and visualization: The added nodes provide additional points where results can be mapped or visualized, allowing users to see the spatial distribution.
  5. Aid in model calibration: Comparing results at vertex nodes with observed data can guide subcatchement parameter calibration to match real-world measurements.

Section 1: How does it Work?

The Ruby script reads the polygon boundary of a subcatchment or polygon, finds the vertex points and centroid, and makes a new node.

Here is the network before: no nodes in the polygon and only an outlet node for the subcatchment.

After running the script for hw_polygons, the new nodes at the polygon points and the centroid of the shape area are added.

After running the script for hw_subcatchment, the new nodes at the subcatchment vertices and the centroid of the subcatchment area are added.

The small model before the new nodes are added by Ruby. The red shapes are polygons. The other colored shapes are subcatchments.

After running the script for hw_polygons, the new nodes at the polygon points and the centroid of the shape area are added.


After running the script for hw_subcatchment, the new nodes at the subcatchment vertices and the centroid of the subcatchment area are added.
Section 2: InfoWorks SIM and SWMM SWMMSim Engines
InfoWorks SIM and SWMM SWMMSim Engines and Table Names
This script creates new nodes at the centroid and periphery of each selected polygon or subcatchment in an InfoWorks network.
Section 2: Readme.md file for the Ruby Code
# Ruby Code to Add Nodes to Selected Polygons and/or Subcatchments

## Overview
This code adds new nodes at the centroid and vertices of selected polygons in an InfoWorks network.

## Functionality
- Starts transaction to commit all changes at once
- Loops through selected polygons
  - Gets boundary coordinate array
  - Calculates centroid coordinates
  - Creates centroid node 
  - Creates node at each vertex
- Commits transaction to save changes

## Variables
- `boundary_array` - Polygon boundary coordinates
- `centroid_x/y` - Calculated centroid coords 

## Methods  
- `selected?` - Checks if selected
- `boundary_array` - Gets coordinates 
- `new_row_object()` - Creates new node
- `transaction_` - Commits changes

## Output
- New nodes created at centroid and vertices of selected polygons
- Added nodes committed to network         
Section 3: Where do I find this code for ICM InfoWorks and SWMM Networks?

You can find the Ruby code for both the ICM InfoWorks and SWMM Networks from the InfoWorks ICM Technical Information Hub for InfoWorks and SWMM Networks in the subfolder 01 InfoWorks ICM\01 Ruby\02 SWMM0021 - Create nodes from polygon, subcatchment boundary

Section 4: Copy of Code for ICM SWMM and InfoWorks Networks
# Get the current network object
net = WSApplication.current_network

# Begin a transaction. This allows all changes to be committed at once at the end of the script.
net.transaction_begin

# Iterate over all polygon objects in the network or subatchments for hw_subcatchment
net.row_object_collection('hw_polygon').each do |polygon|
    # Check if the polygon is selected
    if polygon.selected?
        # Get the boundary array of the polygon
        boundary_array = polygon.boundary_array

        # Calculate the centroid of the polygon
        centroid_x = boundary_array.each_slice(2).map(&:first).sum / (boundary_array.size / 2)
        centroid_y = boundary_array.each_slice(2).map(&:last).sum / (boundary_array.size / 2)

        # Create a new node at the centroid for a SWMM model use sw_node
        centroid_node = net.new_row_object('hw_node')
        centroid_node['node_id'] = polygon.id + '_centroid'
        centroid_node['x'] = centroid_x
        centroid_node['y'] = centroid_y
        centroid_node.write

        # Create a new node at each vertex
        boundary_array.each_slice(2).with_index do |(x, y), index|
            vertex_node = net.new_row_object('hw_node')
            vertex_node['node_id'] = "#{polygon.id}_vertex_#{index}"
            vertex_node['x'] = x
            vertex_node['y'] = y
            vertex_node.write
        end
    end
end

# Commit the transaction, making all changes permanent
net.transaction_commit        

Closing Note: Thank you so much for journeying with me through this content. This space is reserved for future updates and insights. Your engagement and time are truly appreciated. Until next time! You can also see my past articles on LinkedIn (91 in 2023).

Extra Poem

In the world of water, so vast and so wide, A Ruby script's journey, a tech-savvy guide. ?? To subcatchments large, it brings a new node, Subdividing areas, a helpful new code.

?? For land of different uses, a parameter's tune, Roughness and infiltration, under sun and moon. ?? In soil type's embrace, each section's own trait, Micro catchments it forms, with detail so great.

?? In modeling's realm, where data's king, Nodes in polygons, such clarity they bring. Each sub-section modeled, with care and with might, Detailing the water's flow, both day and night.

?? For results to be seen, in maps so vivid, Visualization's key, in this domain vivid. ?? At each node, results take a stand, Mapping the journey, across the watered land.

?? Calibration's aid, a tool so profound, Matching reality, where truth is found. ?? At vertex nodes, observations align, For models so real, like stars that shine.

In this world of water, where Ruby code plays, Accuracy and insight, in so many ways. ?? For modelers of ICM, and SWMM's deep dive, This script's a beacon, making models come alive.


Robert Dickinson

Autodesk Water Technologist for Storm Sewer and Flood | Expert in ICM InfoWorks ICM SWMM/Ruby | 18 Years at Innovyze/Autodesk | 52 Years with EPASWMM TAC for CIMM.ORG SWMM5+

10 个月

You can find the Ruby code for both the ICM InfoWorks and SWMM Networks from the InfoWorks ICM Technical Information Hub for InfoWorks and SWMM Networks in the subfolder 01 InfoWorks ICM\01 Ruby\02 SWMM0021 - Create nodes from polygon, subcatchment boundary

回复

要查看或添加评论,请登录

Robert Dickinson的更多文章

社区洞察

其他会员也浏览了