1

I am able to run the below code in the Python command window in ArcMap 10.2.1 without issue. However, when I run this as a toolbox script, it fails at line 15:

newLyr = arcpy.mapping.ListLayers(mxd, "New Feature Layer", df)[0]

IndexError: list index out of range

It seems to me that the feature layer is not being generated in line 14, causing the error in the following line. But I'm not sure. Does anyone have any idea why the processing would fail when executed as a script but not when run in the command window?

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.Delete_management("New Feature Layer") #clear any earlier instance from memory

attrLyr = arcpy.mapping.ListLayers(mxd, "SELECT FEATURES BY ATTR LAYER", df)[0]
locLyr = arcpy.mapping.ListLayers(mxd, "SELECT FEATURES BY LOCATION LAYER", df)[0]
srcLyr = arcpy.mapping.ListLayers(mxd, "SRC SYMBOLOGY LAYER", df)[0]

expression = "SELECTION EXPRESSION"

arcpy.SelectLayerByAttribute_management(attrLyr,"NEW_SELECTION",expression) #select features from attrLyr according to expression
arcpy.SelectLayerByLocation_management(locLyr, "INTERSECT", attrLyr) #select features from locLyr that intersect with attrLyr
arcpy.MakeFeatureLayer_management(locLyr, "New Feature Layer") #create feature layer from selected features in locLyr
newLyr = arcpy.mapping.ListLayers(mxd, "New Feature Layer", df)[0] #assign new layer to variable "newLyr"
arcpy.mapping.UpdateLayer(df, newLyr, srcLyr, True) #apply srcLyr symbology to newLyr

2 Answers 2

1

The key here is to assign the feature layer to a variable (addLayer) and then use AddLayer() to add it to the data frame. From there I could pass it as newLyr to the UpdateLayer() method.

arcpy.MakeFeatureLayer_management(locLyr, "New Feature Layer")
addLayer = arcpy.mapping.Layer("New Feature Layer")
arcpy.mapping.AddLayer(df,addLayer, "AUTO_ARRANGE")
newLyr = arcpy.mapping.ListLayers(mxd, "New Feature Layer", df)[0]
arcpy.mapping.UpdateLayer(df, newLyr, srcLyr, True)

I will note (gripe) that running MakeFeatureLayer in the command window will add the layer to the data frame automatically, so testing the above answer in the command window actually results in two layers being added to the map: the feature layer in line 1 and the layer in line 2.

0

You are only creating a new layer in memory, while not actually adding it to the table of contents. To add this layer to your TOC, use AddLayer(), then RefreshTOC() and only then it will show up in a list generated by a ListLayers() function.

2
  • Thanks for your help. I think the crux of my problem is that AddLayer() cannot take a feature layer as input e.g, (gis.stackexchange.com/questions/23348/…) Which seems counter-intuitive, since a feature layer would presumably need to be....added to a map. I will try generating a layer to disk instead of relying on a feature layer Commented Sep 16, 2014 at 16:08
  • No worries, glad I could help! Could you mark the post as answered?
    – Menno
    Commented Sep 18, 2014 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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