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.
@@polygon_id = 1
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.
# A lookup from structure name to count of each structure
@@lookup_id = Hash.new
# 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