Adding a layer (existing point shapefile) from tool output

2700
1
Jump to solution
11-28-2014 10:17 AM
by Anonymous User
Not applicable

My question: can someone please suggest a fix to the code below OR suggest a better way to add an existing point shapefile to a map (not to a geodatabase as this tool will be accessed from a web map using ArcGIS API for JS through REST services once it is published to a server)?

 

I can't seem to add a point shapefile layer to a map from within a tool, though it works fine from the command line. I've read this thread and followed its suggestions, but still nothing... The particular line that is failing is the last one from below:

 

precipLayer = arcpy.MakeFeatureLayer_management(precipShape,precipShape) # NOTE: precipShape = existing point shapefile

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP")

 

The error from running the tool is:

 

Traceback (most recent call last):

  File "C:\Users\...\getData.py", line 83, in <module>

    arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP")

  File "c:\program files\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_

    return fn(*args, **kw)

  File "c:\program files\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 49, in AddLayer

    assert isinstance(add_layer, Layer)

AssertionError

Failed to execute (getData).

 

If I run this from the command line, then it works (adds the layer twice, of course: once from MadeFeatureLayer and once from AddLayer). But if I remove the .getOutput(0) and run it in at the command line, then I basically get the same error as running it as a tool:

 

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "c:\program files\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_

    return fn(*args, **kw)

  File "c:\program files\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 49, in AddLayer

    assert isinstance(add_layer, Layer)

AssertionError

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Jeff Barrette‌ made the following suggestion through another post‌ and the tool is now working:

Replace:

precipLayer = arcpy.MakeFeatureLayer_management(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")

With:

precipLayer =arcpy.mapping.Layer(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")

And remove getOutput(0):

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

#arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP")   # GOT ERROR: getOutput(0) doesn't exist

arcpy.mapping.AddLayer(df, precipLayer, "TOP")

View solution in original post

0 Kudos
1 Reply
by Anonymous User
Not applicable

Jeff Barrette‌ made the following suggestion through another post‌ and the tool is now working:

Replace:

precipLayer = arcpy.MakeFeatureLayer_management(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")

With:

precipLayer =arcpy.mapping.Layer(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")

And remove getOutput(0):

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

#arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP")   # GOT ERROR: getOutput(0) doesn't exist

arcpy.mapping.AddLayer(df, precipLayer, "TOP")

0 Kudos