Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bixb0012/dfddcd1142b31b2986cc9feb155d4564 to your computer and use it in GitHub Desktop.
Save bixb0012/dfddcd1142b31b2986cc9feb155d4564 to your computer and use it in GitHub Desktop.
ArcPy (ArcMap): Modify Layer Symbology, Expanded
#!python2
# Example 1a adapted from https://www.reddit.com/r/gis/comments/4rhvhh/map_automation_arcpymapping_make_lyr/
#
# Reference: 1) http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm
# 2) https://docs.python.org/2/library/json.html
import arcpy
import json
lyr = # Layer object, typically from arcpy.mapping.ListLayers (arcpy._mapping.Layer)
# Example 1a: Modifying single-field renderer using undocumented _arc_object.renderer
new_field = # New field name for symbolizing layer
old_field = # Old field name for symbolizing layer
sym_xml = lyr._arc_object.renderer
lyr._arc_object.renderer = sym_xml.replace(old_field, new_field)
arcpy.RefreshActiveView()
# Example 1b: Modifying single-field renderer using updateLayerFromJSON
new_field = # New field name for symbolizing layer (string)
sym_dict = json.loads(lyr._arc_object.getsymbology())
sym_dict["renderer"]["field1"] = new_field
lyr.updateLayerFromJSON(json.dumps({"drawingInfo": sym_dict}))
arcpy.RefreshActiveView()
#
@jorgeluisyh
Copy link

Thanks, i used this as a guide to write a function to update symbology to only show classes with values instead of all the existing classes

@bixb0012
Copy link
Author

@jorgeluisyh , I am glad you are finding it useful. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment