2

We have to generate 100's of pdf's (from mxd) for 100's of shapefiles based on a column value "dummy". We have a template lyr file with predefined symbols for each category type in the "dummy" column. But when we use

arcpy.ApplySymbologyFromLayer_management(input_layer,source_layer)

code, all the category type are getting listed in the ToC. As in the image, though "Borefield, Coreyard, etc..." has zero counts - still they are getting listed in the ToC and subsequently in the legend.

enter image description here

1

Can be done by modifying the arcpy.mapping.Layer object properties.

  1. Find what unique values are present in a layer you are would like to update symbology for.
  2. Modify the symbology classes of the .lyr file object (in-memory).
  3. 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[0] for f in arcpy.da.SearchCursor("Streets_1", "DIR_TRAVEL")])) # [u'B', u'F']
lyr.symbology.classValues = classes_present 
arcpy.ApplySymbologyFromLayer_management("Streets_1", lyr)
  • Thanks. Worked like a charm. But, since we are only bothered about the output pdf - i am using MAJ742's solution. – joseph_k Aug 14 '18 at 0:48
2

Have you looked at modifying the legend rather than the ToC?

In the legend properties > Items > Map Extent Options, tick [Only show classes that are visible in the current map extent]. That will cut your legend down to only show whats visible and therefore ignore any empty categories.

If this is saved in your .mxd hopefully it will pull through if you're doing the exporting with ArcPy (although I've not tested that stage).

Legend properties

http://desktop.arcgis.com/en/arcmap/10.5/map/page-layouts/working-with-legends.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.