AnsweredAssumed Answered

arcpy get the style item details from a legend item

Question asked by NJurko on Nov 29, 2016
Latest reply on Dec 21, 2016 by NJurko

Hi everybody,

 

Working with ArcGIS 10.4, ArcInfo Licence

 

 

I am writing an arcpy script to set up a process for map automation.   The user will design a map layout how they like and then use that layout to iterate through a list of rasters and export to pdf.   I have pre-programmed certain text elements and one legend element to be dynamic and change as the rasters are added/removed from the layout.

 

My issue is with the legend element and style item.  The user will add a legend and determine the item style of the raster layer using the legend wizard.  I'd like my arcpy script to read what that style item type is and apply it to the next raster that is added to the map.    I've been searching for ideas on how to 'describe' or 'get' the style item, but I can't find and arcpy method (and I'm way too novice to figure out the pure python way).   I think I have to read the style item path name. I assume the information I need is floating around there somewhere because the legend wizard needs to know what the file path is rights?!

 

Does anyone have an idea of where to find an answer of what type of command I'm looking for?  (even a search keyword would be useful!)

Below is a snippet of my script combined with how I want it to work (legend style item file variable).  Also included is a screenshot of the tool and the map layout with the dynamic elements.  These are works in progress!

Thanks,

Natasha

import arcpy
from arcpy import env
import arcpy.mapping as mp

# This is part of my script... I am envisioning something like.....

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

legendItemStyleFile = #This is where I want to 'get' the style path
                      # currently in use by the item already in my legend.

# Do some stuff here to add the rasters in my Raster List,
# and to make a map based only on the rasters in the Raster List.

for raster in rasterFileName:
    rasterLayer = mp.Layer(raster)
    rasterLayer.visible = True
    rasterSplit = re.split("[.,_?:]+", str(raster))

    rasterFWI = rasterSplit[0]
    rasterDate = rasterSplit[1]
    rasterFileType = rasterSplit[2]
   
    #Changes the date label if the boolean is true
    if labelDate == 'true':

        arcpy.AddMessage("layer date: " + rasterDate)
        #Date Text Element Change
        textDate = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "textDate")[0]
        textDate.text = rasterDate
        #arcpy.AddMessage(compareRaster)
  
    #Changes the header label if the boolean is true
    if labelHeader == 'true':
        textHeader = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "textHeader")[0]
        textHeader.text = rasterFWI.upper()
   
    #Update the legend item with the style from my original raster (legendItemStyleFile variable). 
    legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
    styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", legendItemStyleFile)[0]

    legend.updateItem(lyr, styleItem)

    #*** still have to write the export, turn off raster and turn on the next part of this script...


    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()

Outcomes