Can be done by modifying the
arcpy.mapping.Layer object properties.
- Find what unique values are present in a layer you are would like to update symbology for.
- Modify the symbology classes of the .lyr file object (in-memory).
- Apply the customized symbology which will have only classes that will have at least one corresponding value in the layer you are updating.
Streets feature class has been symbolized with 3 values for the direction of travel:
['B', 'F', 'T']
I have created a .lyr file on disk. Now I removed the rows with the
'T' value in a feature class
'Streets_1' and would like to apply the symbology from my .lyr file excluding
'T' as I won't have any rows for this class.
lyr = arcpy.mapping.Layer(r"C:\GIS\Temp\Streets.lyr")
print lyr.symbology.classValues # [u'B', u'F', u'T']
classes_present = list(set([f for f in arcpy.da.SearchCursor("Streets_1", "DIR_TRAVEL")])) # [u'B', u'F']
lyr.symbology.classValues = classes_present