Select to view content in your preferred language

Legend style in arcpy.mapping - Index Error: 0

7323
4
Jump to solution
09-27-2012 06:53 AM
LeeEllenburg
Emerging Contributor

All,

 

I am currently working in ArcMap 10.1. I am trying to add layers to a map, then change the legend's style and location. Below is a sample code. legend variable is working, and I can move it around as a LayoutElement. However, when I want to update the style I keep getting an Index Error: 0. I've been trying my own custom style, but can't even get it to work with the ESRI.style.

 

Here's the code:

 

 

import arcpy from arcpy import env arcpy.env.overwriteOutput = True import os  mxd = arcpy.mapping.MapDocument ("C:\\TestFolder\\Untitled.mxd") df = arcpy.mapping.ListDataFrames (mxd, "Layers")[0]  style = "ESRI.style" lyr1 = arcpy.mapping.Layer("C:\\TestFolder\\GriDSSAT_Counties.lyr") lyr2 = arcpy.mapping.Layer("C:\\TestFolder\\TestLayer\\LGI_20120821.lyr") lyr3 = arcpy.mapping.Layer("C:\\TestFolder\\States.lyr") styleItem = arcpy.mapping.ListStyleItems(style, "Legend Items")[0] legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0] legend.autoAdd = False arcpy.mapping.AddLayer(df, lyr1) legend.autoAdd = True arcpy.mapping.AddLayer(df, lyr2, "BOTTOM") legend.autoAdd = False arcpy.mapping.AddLayer(df, lyr3, "BOTTOM")  print styleItem  legend.updateItem(lyr2, styleItem)   legend.title = "Whoala"    arcpy.mapping.ExportToPDF (mxd, "C:\\TestFolder\\PDFFolder\\Test.pdf")

 

 

The error message I receive is as follows:

 

Traceback (most recent call last):
  File "C:\TestFolder\Scripts\legend.py", line 24, in <module>
    legend.updateItem(lyr2, styleItem)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 772, in updateItem
    return convertArcObjectToPythonObject(self._arc_object.updateItem(*gp_fixargs((legend_item_layer, legend_item_style_item, preserve_style_sizes, use_visible_extent, show_feature_count, use_ddp_extent, index), True)))
IndexError: 0

 

Thanks for any insight on how to fix this

 

Lee

 

Message was edited by: Curtis Price 2014.10.25 typo in title

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffMoulds
Esri Contributor
After adding the lyr files to the map, use layer references to layers in the map, not lyr files on disk, in updateItem. For example:

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr") arcpy.mapping.AddLayer(df, lyrFile, "TOP") styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items")[0] legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0] lyr = arcpy.mapping.ListLayers(mxd, 'NE_Lakes', df)[0] # the name in the TOC for Rivers.lyr legend.updateItem(lyr, styleItem) # use lyr, not lyrFile arcpy.mapping.ExportToPDF(mxd, 'temp.pdf') del mxd


We made the same error in some of our doc samples, which I will change now. Sorry if the samples sent you down the wrong path.

View solution in original post

0 Kudos
4 Replies
JeffMoulds
Esri Contributor
After adding the lyr files to the map, use layer references to layers in the map, not lyr files on disk, in updateItem. For example:

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr") arcpy.mapping.AddLayer(df, lyrFile, "TOP") styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items")[0] legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0] lyr = arcpy.mapping.ListLayers(mxd, 'NE_Lakes', df)[0] # the name in the TOC for Rivers.lyr legend.updateItem(lyr, styleItem) # use lyr, not lyrFile arcpy.mapping.ExportToPDF(mxd, 'temp.pdf') del mxd


We made the same error in some of our doc samples, which I will change now. Sorry if the samples sent you down the wrong path.
0 Kudos
LudwigHilger
Deactivated User

@Hello,

I get the same error but I believe I am using a reference instead of the layer as I used arcpy.mapping.Layer() to reference the layer "icecurrentlayer".

what am i doing wrong?

Here ist the code:

arcpy.MakeRasterLayer_management(wdir  + "\\" + "Day_" + str(j) + "_ice_" + i + "cm.tif", "icecurrentlayer", "#")

arcpy.ApplySymbologyFromLayer_management("icecurrentlayer", "D:\\R_working_directory\\hales_and_roering_2007\\SGRD\\colormodel2.lyr")

reficecurrentlayer = arcpy.mapping.Layer("icecurrentlayer")

arcpy.mapping.AddLayer(data_frame = dfs[di[1].index(i)], add_layer = reficecurrentlayer, add_position = "TOP")# Provides the ability to add a layer to a data frame within a map document (.mxd) using simple placement options

# Change look of Legend for ice layers: You can look up the style_file_path, etc in the Style manager (GUI -> Customize)

styleItem = arcpy.mapping.ListStyleItems("C:/Program Files (x86)/ArcGIS/Desktop10.2/Styles/ESRI.ServerStyle", "Legend Items", "Horizontal Single Symbol Description Only")[0]

legend.updateItem(reficecurrentlayer, styleItem)

0 Kudos
curtvprice
MVP Esteemed Contributor

My guess is a list is coming up empty. A good way to test these things is to run them from the ArcMap python command line so you can see what you're getting.

By the way, it's better not to hard code the install folder, here's how you can avoid that:

InstallDir = arcpy.GetInstallInfo("desktop")["InstallDir"]

styleItem =  arcpy.mapping.ListStyleItems(

                 InstallDir + "/Styles/ESRI.ServerStyle",

                 "Legend Items",

                 "Horizontal Single Symbol Description Only")[0]

LeeEllenburg
Emerging Contributor
Funny story... I had to move projects after I posted this. I just lately have had enough time to get back to what I was doing and I came back to the same error. I googled it and found this.... the question I asked months ago!

Anyway, this works great!

Thanks Jeff
0 Kudos