legend.removeItem error

912
1
05-15-2014 07:42 AM
PaulFrank
New Contributor II
Hello,

I use a python script in arccatalog that creates an mxd, adds a layer, updates it's symbology, and then tries to remove it from the legend.  The error I receive is

Traceback (most recent call last):
  File "G:\NEIGHBOR PLAN\ArcView\Scripts_Extns\make_plan_amendment_map.py", line 107, in <module>
    legend.removeItem(removelyr)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\_mapping.py", line 745, in removeItem
    return convertArcObjectToPythonObject(self._arc_object.removeItem(*gp_fixargs((legend_item_layer, index), True)))
IndexError: 0

I post the entire script here, but the issue comes up at the very end.  I know there are some inefficiencies but I was having a hard time understanding that I hadn't created the necessary layer objects required for the UpdateLayer and AddLayer tools.  The offending code is commented out.

# ------------------------------------------------------------------------------
# Name:         make_plan_amendment_map.py
# Purpose:      Make and print a plan amendment map.  The user
#               enters a case number, and the script would zoom the map to that
#               case and fill out the title and other map elements based on the
#               case information.
# Created on:   December 20, 2011
# Updated on:   May 15, 2014
# By:           Paul Frank
# ------------------------------------------------------------------------------

# Import system modules
import arcpy
from arcpy import env
import os
env.overwriteOutput = True

#declare setup variables

print arcpy.GetMessages()
inworkspace = arcpy.GetParameterAsText(0) #declare workspace variable
incasenum = arcpy.GetParameterAsText(1) #case number to center map
innpa = arcpy.GetParameterAsText(2) #neighborhood planning area
innewuse = arcpy.GetParameterAsText(3)  #new land use
inolduse = arcpy.GetParameterAsText(4)  #old land use
inaddress = arcpy.GetParameterAsText(5)  #address

mxd = arcpy.mapping.MapDocument(r"G:\NEIGHBOR PLAN\ArcView\Projects\Templates\\planamendment_template.mxd")
newordinancemxd = inworkspace + "\\" + incasenum + "_ordinance_request.mxd"
newplanamendmeetingmxd = inworkspace + "\\" + incasenum + "_FLUM_BW.mxd"
notificationbufferlyr = r'G:\NEIGHBOR PLAN\ArcView\Themes\Neighborhood Planning Areas\planamendmentbufferlyrs\notification_buffer.lyr'
qyrstring = "CASE_NUMBER = " + "'" + incasenum + "'" #string to create definition query
amendbuffer = incasenum[0:13].replace('-', '')
notificationupdatelyr = "notification_buffer"

amendbufferpath = r"G:\NEIGHBOR PLAN\ArcView\Themes\Neighborhood Planning Areas\plan_amendments.gdb\Polygons" + "\\" + amendbuffer
bufferlayerlocation = r"G:\NEIGHBOR PLAN\ArcView\Themes\Neighborhood Planning Areas\planamendmentbufferlyrs" + "\\" + amendbuffer + ".lyr"
arcpy.AddMessage("Amendment buffer is " + amendbufferpath)
arcpy.AddMessage("Created variables!")

#create definition query for case and zoom to that polygon
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] #set dataframe object
flumlyr = arcpy.mapping.ListLayers(mxd, "FLUM", df)[0] #set layer object for selection target
zcaselyr = arcpy.mapping.ListLayers(mxd, "ZoningCases", df)[0] #set layer object to select from
zcaselyr.definitionQuery = qyrstring #create definition query for centering map

df.extent = zcaselyr.getSelectedExtent(False)
currentscale = df.scale
if currentscale < 2400:
    df.scale = 2400
else:
    df.scale = 3600    
arcpy.AddMessage("Created definition query for case and zoomed to case polygon!")

arcpy.Buffer_analysis(zcaselyr, amendbufferpath, "500 Feet", "FULL", "ROUND", "ALL")
bufferlayer = arcpy.MakeFeatureLayer_management(amendbufferpath, "notification_buffer").getOutput(0)
arcpy.SaveToLayerFile_management(bufferlayer, bufferlayerlocation, "ABSOLUTE")

#select nearby features to draw in legend
nameofflumlyr = flumlyr.name
arcpy.AddMessage("Flum layer is" + nameofflumlyr)
arcpy.SelectLayerByLocation_management(flumlyr, "WITHIN_A_DISTANCE", zcaselyr, "2000 Feet", "NEW_SELECTION") #select nearby features
outputlyr = "FLUM_Select" #declare variable to add selected features to display
selLayer = arcpy.MakeFeatureLayer_management(flumlyr, outputlyr).getOutput(0) #make feature layer from selection
arcpy.AddMessage("Drew Legend!")


#adjust symbology and legend
arcpy.ApplySymbologyFromLayer_management(selLayer, flumlyr) #use symbology from parent layer
arcpy.mapping.AddLayer(df, selLayer)
arcpy.mapping.RemoveLayer(df, flumlyr) #remove parent layer
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] #set legend object
legend.adjustColumnCount(2) #adjust legend object
flumstring = innewuse.replace("'", "")
zcaselyr.name = flumstring
arcpy.AddMessage("Adjusted Symbology in Legend!")

#adjust title and subtitle
elmtitle = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Title")[0]
elmtitle.text = "Exhibit A" + '\n' + innpa + " Neighborhood Planning Area" + '\n' + "Amendment " + incasenum
arcpy.AddMessage("Adjusted Title!")
elmcallout = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Callout")[0]
elmcallout.text = inaddress + '\n' + " " + '\n' + "Future Land Use Designation:" + '\n' + innewuse
arcpy.AddMessage("Adjusted Callout!")


#create final mxd
mxd.saveACopy(newordinancemxd) #save to another file
arcpy.AddMessage("Saved ordinance request map!")

elmtitle.text = innpa + " Neighborhood Planning Area" + '\n' + incasenum
elmcallout.text = inaddress + '\n' + "Future Land Use Map Request:" + '\n' + "From: " + inolduse + '\n' + "To: " + flumstring
arcpy.AddMessage("Saved plan amendment meeting map!  Open maps and make necessary adjustments")

#create meeting mxd with buffer
mxd.saveACopy(newplanamendmeetingmxd) #save to another file
meetingmxd = arcpy.mapping.MapDocument(newplanamendmeetingmxd)
dfmeetingmxd = arcpy.mapping.ListDataFrames(meetingmxd)[0]
planamendbwaddlayer = arcpy.mapping.Layer(bufferlayerlocation)
arcpy.mapping.AddLayer(dfmeetingmxd, planamendbwaddlayer, "TOP")
notifylayer = arcpy.mapping.ListLayers(meetingmxd, "notification_buffer")[0]
planamendbwaddlayersymbol = arcpy.mapping.Layer(notificationbufferlyr)
arcpy.mapping.UpdateLayer(dfmeetingmxd, notifylayer, planamendbwaddlayersymbol, True)
arcpy.AddMessage("updated layer!")
#removelyr = arcpy.mapping.ListLayers(meetingmxd, "notification_buffer", dfmeetingmxd)[0]
#legend.removeItem(removelyr)
meetingmxd.save()

del mxd
arcpy.AddMessage("Created Map!")
Tags (2)
0 Kudos
1 Reply
PaulFrank
New Contributor II
Hello again,

I found my problem.  Thanks to all who looked.  The issue was I hadn't created legend object for the second map.  After discovering that, I copied the code for the first legend, then forgot to change the mxd object to the second map.  I also cleaned up some of the variables.  Here is the last part of the script where I made the fixes:

mxd.saveACopy(newplanamendmeetingmxd) #save to another file
meetingmxd = arcpy.mapping.MapDocument(newplanamendmeetingmxd)
dfmeetingmxd = arcpy.mapping.ListDataFrames(meetingmxd)[0]
planamendbwaddlayer = arcpy.mapping.Layer(bufferlayerlocation)
arcpy.mapping.AddLayer(dfmeetingmxd, planamendbwaddlayer, "TOP")
notifylayer = arcpy.mapping.ListLayers(meetingmxd, notificationupdatelyr, dfmeetingmxd)[0]
arcpy.mapping.UpdateLayer(dfmeetingmxd, notifylayer, notifysymlyr, True)
removelyr = arcpy.mapping.ListLayers(meetingmxd, notificationupdatelyr, dfmeetingmxd)[0]
arcpy.AddMessage("removelayer is " + removelyr.name)
meetinglegend = arcpy.mapping.ListLayoutElements(meetingmxd, "LEGEND_ELEMENT", "Legend")[0] #set legend object
meetinglegend.removeItem(removelyr)
meetingmxd.save()
0 Kudos