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