1

This question already has an answer here:

I am still new to ArcPy.

I'm running a really simple script, it selects a feature from a dataset (one town out of many), saves the data, and adds it to the (empty) dataframe.

    map = mapping.MapDocument("CURRENT")
    df = map.activeDataFrame   
    where_clause = "TOWN = " + "'"+GetParameterAsText(0)+"'"

    arcpy.MakeFeatureLayer_management(statewide_towns, "TOWNS")
    arcpy.SelectLayerByAttribute_management("TOWNS","NEW_SELECTION", where_clause)
    arcpy.CopyFeatures_management("TOWNS", "OUTPUT_Single_TOWN")
    arcpy.MakeFeatureLayer_management("OUTPUT_Single_TOWN", "TOWN_BND")
    mapping.AddLayer(df,"TOWN_BND","AUTO_ARRANGE")

Throws me this error:

Traceback (most recent call last):
  File "C:\path\to\myTool.py", line 58, in <module>
    mapping.AddLayer(df,"TOWN_BND","AUTO_ARRANGE")
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\mapping.py", line 49, in AddLayer
    assert isinstance(add_layer, Layer)
AssertionError

Failed to execute (myTool).

marked as duplicate by PolyGeo arcpy Jan 19 '18 at 5:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Don't forget to take the Tour and to read about what is expected in a code snippet. Here it looks like you have copy/pasted some code rather than providing a code snippet that works up to where you are stuck. – PolyGeo Jan 19 '18 at 5:10
  • Will take a look at this later. Thanks for the heads up. – user62160 Jan 19 '18 at 5:14
1

Instead of what you have, do the following to make your OUTPUT_Single_TOWN feature class.

Replace:

arcpy.MakeFeatureLayer_management(statewide_towns, "TOWNS")
arcpy.SelectLayerByAttribute_management("TOWNS","NEW_SELECTION", where_clause)
arcpy.CopyFeatures_management("TOWNS", "OUTPUT_Single_TOWN")

with:

arcpy.Select_analysis("TOWNS", "OUTPUT_Single_TOWN", where_clause)

then follow the instructions from Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy? to add the OUTPUT_Single_TOWN feature class as a layer in your map:

newlayer = arcpy.mapping.Layer("OUTPUT_Single_TOWN")
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")

You may or may not (I have not tested recently) need to provide a full pathname to the OUTPUT_Single_TOWN feature class when you create the Layer object (newLayer) above.

  • Worked like a charm. Now to tackle my raster layer. Aside from a simpler and easier to read code block, what exactly does Select_analysis() do that my code doesn't? I understand that shapefiles cannot be read in by arcpy, but rather a .lyr file. – user62160 Jan 19 '18 at 5:17
  • I think Select_analysis() is more ArcPy-thonic than using MakeFeatureLayer_management(), SelectLayerByAttribute_management() and CopyFeatures_management(). gis.stackexchange.com/questions/4882 shows that shapefiles can be read in without using a layer file by creating a Layer object directly from them instead. – PolyGeo Jan 19 '18 at 5:47