ianleinwand

Adding python tool output to arcmap automatically when analysis is complete

Discussion created by ianleinwand on Jun 18, 2012
Latest reply on Jun 20, 2012 by nahmed-esristaff
Greeting all...

I've written a python scrip that is run in arctool box given user specified inputs... at this point the tool works but I would like to have the tool automatically
add the resulting output datasets to the ArcMap table of contents.

I've tried a few variation of the following code... keep getting the same error... see below. I think it all goes back to this line of code where its not recognizing which dataset to add to the map. Any help is appreciated.

Code...

mxd = arcpy.mapping.MapDocument(r"D:\Risk_Models\Sample_Design\sample_design_tool.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

print "ready to add layer"
print sample_plots


newlayer1 = arcpy.mapping.Layer(r"D:\Risk_Models\Sample_Design\Sample_design_tool\sample_design_tool.gdb\state_GA")
#newlayer2 = arcpy.mapping.Layer(sample_points)

print newlayer1

arcpy.mapping.AddLayer(df, newlayer1, "TOP")
#arcpy.mapping.AddLayer(df, newlayer2, "Bottom")

arcpy.RefreshActiveView()
arpy.RefreshTOC()
del mxd, df, newlayer1

end code


Error... reported in python

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "D:\Risk_Models\Sample_Design\Sample_design_tool\FHTET_Sample_Design_Tool\FHTET_sample_design_tool_6_18_2012_add_data_update_test.py", line 94, in <module>
    newlayer1 = arcpy.mapping.Layer(r"D:\Risk_Models\Sample_Design\Sample_design_tool\sample_design_tool.gdb\state_GA")
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 255, in __init__
    super(LayerMixin, self).__init__(lyrfile)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__
    for arg in args))
ValueError: Object: CreateObject Layer invalid data source

Outcomes