Adding python tool output to arcmap automatically when analysis is complete

7746
12
06-18-2012 12:13 PM
IanLeinwand
New Contributor II
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
Tags (2)
0 Kudos
12 Replies
NobbirAhmed
Esri Regular Contributor
I'm really sorry that nothing is working for you 😞 Which Service Pack you are using? I'm on 10 sp5. The tool is working as expected on my machine. Let me know if are on a different service pack - I'll try on that setup.
0 Kudos
IanLeinwand
New Contributor II
I'm really sorry that nothing is working for you 😞 Which Service Pack you are using? I'm on 10 sp5. The tool is working as expected on my machine. Let me know if are on a different service pack - I'll try on that setup.


I updated my service pack to service pack 4 and now your tool and the script works. I've updated my tool and its running now.

Thanks for all the help... updating the service pack worked. Lesson learned... alway update the service pack... I didn't see a down load for service pack 5 it said it would be release in July...

thanks

again

Ian
0 Kudos
NobbirAhmed
Esri Regular Contributor
If you have updated to sp4 please install this spatial join patch as well:

http://resources.arcgis.com/content/patches-and-service-packs?fa=viewPatch&PID=160&MetaID=1857

It fixes some defects introduced to spatial join field mapping in sp4.

Lesson learned: update only when your workflow breaks 🙂

* Please mark the thread 'answered'.
0 Kudos