Nifty Script for creating a Unique ID in the Open Data Import Setting

Have you ever gone to import a data set and realized that the Unique ID is not unique – or worse there isn’t one ?

This nifty little script will simply apply a numeric ID that will increment (act as the row number for the dataset).  In the example below we are using it for polygons but could be adapted to any model object.

class Importer
    @@polygon_id = 1

   def Importer.OnEndRecordPolygon(obj)
      obj[‘polygon_id’] = @@polygon_id

      @@polygon_id += 1

The script is written in Ruby language and needs to be saved as a file with extension type .rb  This can then be used in the Open Data Import Centre (as shown below) within InfoWorks and InfoNet.  You still do have to map a field to the Unique ID – but the script overwrites this information.



The script above can be extended as follows to add more complexity to the generated Unique ID.

class Importer

    # A lookup from structure name to count of each structure
    @@lookup_id =

    def Importer.OnEndRecordPolygon(obj)

        # Get the value of the ‘Structure’ in the source object
        structure = obj[‘Structure’]

        # Get the current counter for this structure
        counter = @@lookup_id[structure]

        # Set the counter to 1 if it isn’t already set
        # (Shorthand for doing “counter = 1 if counter.nil?”)
        counter ||= 1

        # Set the Polygon ID in the network to structure name + the counter
        obj[‘polygon_id’] = structure + ” ” + counter.to_s

        # Increment our counter for the structure
        @@lookup_id[structure] = counter + 1

Share this post!

    About Ann Pugh

    Ann has worked in system planning, operations, strategic analysis and marketing roles for water companies and consulting firms. She has worked for Innovyze (and earlier incarnations) since 2000.
    This entry was posted in InfoNet, InfoWorks ICM, InfoWorks WS. Bookmark the permalink.