Using Ruby to add Nodes to Subcatchments and Polygons in ICM InfoWorks and SWMM Networks
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+
This Ruby code to add nodes to selected polygons could be important and useful to InfoWorks ICM and SWMM modelers for a few reasons:
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.
Section 2: InfoWorks SIM and SWMM SWMMSim Engines
领英推荐
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.
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