Learn more about how this script was created by reading the article
Last active
January 9, 2020 20:53
-
-
Save lbutler/cdf4ab46c26fad5d323feab89008e40f to your computer and use it in GitHub Desktop.
Infoworks Ruby Script - Using Ruby with the open data import centre
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'FileUtils' | |
| open_net = WSApplication.current_network | |
| # Prompt user for a folder | |
| shp_dir = WSApplication.folder_dialog 'Select a folder to import files', true | |
| # We keep our CFG files with the Ruby script | |
| cfg_dir = File.dirname(WSApplication.script_file) | |
| err_file = shp_dir + '\errors.txt' | |
| script_file = cfg_dir + '\script.bas' | |
| layers = { | |
| "hydrant" => { "cfg" => cfg_dir + '\hydrant.cfg', "shp" => shp_dir + '\hydrants.shp'}, | |
| "valve" => { "cfg" => cfg_dir + '\valve.cfg', "shp" => shp_dir + '\valves.shp'}, | |
| "polygons" => { "cfg" => cfg_dir + '\polygons.cfg', "shp" => shp_dir + '\dma.shp' }, | |
| "pipe" => { "cfg" => cfg_dir + '\pipe.cfg', "shp" => shp_dir + '\mains.shp' } | |
| } | |
| layers.each do | layer, config | | |
| puts "Importing IW layer: #{layer}" | |
| # If the file to import doesnt exist then skip over it | |
| if File.exist?( config["shp"] ) == false | |
| puts "Shape file not found for #{layer} layer - #{config["shp"]}" | |
| next | |
| end | |
| options = { | |
| "Error File" => err_file, # Save error log path | |
| "Set Value Flag" => 'IG', # Import Flag | |
| "Script File"=> script_file # Import Script (ICM .rb / WS .bas) | |
| } | |
| open_net.odic_import_ex( | |
| 'shp', # Date Source: Source Type | |
| config["cfg"], # Field Mapping Configuration | |
| options, # Additional options in Ruby Hash | |
| layer, # InfoWorks Layer to Import | |
| config["shp"] # Shape file location | |
| ) | |
| # Append the error file with layer completed | |
| File.write(err_file, "\n End of #{layer} import \n", File.size(err_file)) | |
| end | |
| # Open the error log and output to console | |
| File.readlines(err_file).each do |line| | |
| puts line | |
| end | |
| puts "Finished Import" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sub OnBeginRecordHydrant | |
| if Importer.Field("operation") = "Abandoned" Then | |
| Importer.WriteRecord = false | |
| end if | |
| end sub | |
| public sub OnBeginRecordPolygons | |
| if Importer.Field("type") <> "DMA" Then | |
| Importer.WriteRecord = false | |
| end if | |
| end sub | |
| public sub OnBeginRecordPipe | |
| if Importer.Field("operation") = "Abandoned" Then | |
| Importer.WriteRecord = false | |
| end if | |
| end sub | |
| public sub OnBeginRecordValve | |
| if Importer.Field("operation") = "Abandoned" Then | |
| Importer.WriteRecord = false | |
| end if | |
| end sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment