I’m quite sure that it’s evident by now, based on my previous programming posts, and others, that I do a lot of seemingly random tasks. One of the things I love about using Python in ArcGIS is that it allows me to perform tasks which would be tedious otherwise.
I wrote this script to create symbology for a layer which needed to be symbolised by the values in the range domain on a certain field (in this case, max 12). The feature class was empty at this point, so I couldn’t Right Click > Properties > Symbology > Categories > Unique Values > Add All Values. I also didn’t feel like using Add Values and manually typing in each number. Yes, my max was 12, but what if it was 50 in the future?
This file contains 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
|# @date 27/05/2015|
|# @author Cindy Williams|
|# Updates the values for a unique symbology renderer|
|# based on range domain values. The layer must have|
|# the unique symbols renderer enabled manually.|
|# For use in the Python window in ArcMap.|
|mxd = arcpy.mapping.MapDocument("CURRENT")|
|lyr = arcpy.mapping.ListLayers(mxd) # 5th layer in TOC|
|rvd_name = "rvd_1_12" # Name of range value domain|
|rvd_values = |
|# Courtesy of pythoncentral.io – works like range(), but for floats|
|# The range domain type is Short, but the min and max are stored as floats|
|def frange(start, stop, step):|
|i = start|
|while i < stop:|
|i += step|
|# Check if the layer has the unique symbology renderer|
|if lyr.symbologyType == 'UNIQUE_VALUES':|
|# Access domains in the workspace using ListDomains|
|for domain in arcpy.da.ListDomains(lyr.workspacePath):|
|# Check for name|
|if domain.name == rvd_name:|
|for n in frange(dom.range, dom.range + 1, 1):|
|# Can't append directly into classValues|
|lyr.symbology.classValues = rvd_values # Set unique value list|
At this point, I’m basically using any excuse to get into Python. I do try at least to start the task in ArcMap, before asking myself, “Can this be done using Python?” Invariably, the answer is always a resounding “Yes”.