1

I"m working with arcpy, where I'd like to write a script that examines the selection and if only 1 element is selected creates a buffer. I want to get the symbology from another layer, as the buffer needs to be hollow, becouse at the further part of the script I export the layout for printing. But I get the following error message:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1869, in UpdateLayer
    assert isinstance(update_layer, Layer)
AssertionError

My original code looks like:

if "something.mxd" in path or "otherthing.mxd" in path:
     if len(arcpy.Describe("mylayer").FIDSet.split('; ')) == 1:
         mxd =arcpy.mapping.MapDocument("CURRENT")
         df = arcpy.mapping.ListDataFrames(mxd)[0]
         arcpy.Buffer_analysis("Kutak", "Buffers.shp", "1000 Meters")
         updateLyr = arcpy.mapping.ListLayers(mxd, "Buffers.shp")
         sourceLayer = arcpy.mapping.Layer(r"style_ref.lyr")
         arcpy.mapping.UpdateLayer(df, updateLyr, sourceLayer, True)

Any ideas?

3

The problem is in the arcpy.mapping.UpdateLayer(df, updateLyr, sourceLayer, True) part. Your variable updateLyr is a python list. The documentation says that:

ListLayers always returns a Python list object even if only one layer is returned.

So, take the first index of the list, and you should be good as long as you have items in there. See the documentation for more info on ListLayers

  • I also think that you have to create feature layer from Buffers.shp and give it a good name, e.g. Buffers. As @Branco mentioned arcpy.mapping.ListLayers(mxd, "Buffers")[0] will do the rest – FelixIP Jul 21 '15 at 21:34
  • Both of you are right, the main problem was the list exactly, but I also needed a feature layer. Thanks – buboreka Jul 22 '15 at 7:50
2

So with the help of Branco and FelixIP I repaired my code. Actually I did not need the update layer to be a list at all and I also needed to add the buffer with an arcpy.mapping.AddLayer to be later seen on the layout:

if "something.mxd" in path or "otherthing.mxd" in path:
    if len(arcpy.Describe("myLayer").FIDSet.split('; ')) == 1:
            mxd =arcpy.mapping.MapDocument("CURRENT")
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            arcpy.Buffer_analysis("myLayer", "Buffer", "1000 Meters")
            updateLyr = arcpy.mapping.Layer("Buffer")
            sourceLayer = arcpy.mapping.Layer(r"style_ref.lyr")
            arcpy.mapping.UpdateLayer(df, updateLyr, sourceLayer, True)
            arcpy.mapping.AddLayer(df, updateLyr, "TOP")

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.