Adding Python result to ArcGIS ToC

535
4
12-19-2013 04:10 AM
ChrisMatthews
New Contributor III
I've created a python script and added it into ArcToolbox.

The script should create a database connection, create a query table, which is then used to create an event layer, finally a selection is made on this layer.

I've setup the Properties on the script within ArcGIS. When I run the tool it prompts for the four required user values, the script runs and reports it has completed successfully, however no layer is added to the ToC.

Any help would be appreciated.

Chris


# ---------------------------------------------------------------------------
# testPy.py
# Created on: Tue Dec 17 2013 11:25:26 AM
#   (generated by ArcGIS/ModelBuilder)
# Usage: testPy <strFromDate> <Road_Name> <Buffer_Distance> <strToDate> <strWhereClause>
# ---------------------------------------------------------------------------


#####################################################################################
#
# REQUIRED RESOURCES
#
# 1. odc Connection File
# 2. Roads layer
#
#
#####################################################################################


# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Script arguments...
strFromDate = sys.argv[1]
strToDate = sys.argv[2]
Road_Name = sys.argv[3]
Buffer_Distance = sys.argv[4]

strFromDate = strFromDate.replace("/","")
strToDate = strToDate.replace("/","")

strFromDate = strFromDate.replace("\\","")
strToDate = strToDate.replace("\\","")

# Local variables...
elmFoIqueryTab = "elmFoIqueryTab"
dbo_qryELM = "Database Connections\\ELM_Test.odc\\dbo.qryELM"
dbo_tblELM = "Database Connections\\ELM_Test.odc\\dbo.tblELM"
ELM_FoI_Results_Layer = "ELM FoI Results Layer"
ROADS = "ROADS"



if Buffer_Distance == '#':
    Buffer_Distance = "25 Meters" # provide a default value if unspecified

strDay = strFromDate[:2]
strMonth = strFromDate[2:-4]
strYear = strFromDate[-4:]

strNewFromDate = "'" + strYear + "-" + strDay + "-" + strMonth + "'"

strDay = strToDate[:2]
strMonth = strToDate[2:-4]
strYear = strToDate[-4:]

strNewToDate = "'" + strYear + "-" + strDay + "-" + strMonth + "'"

strWhereClause = "qryELM.elmPK = tblELM.elmPK AND dbo.tblELM.LoggedDate >= " + strNewFromDate + " AND dbo.tblELM.LoggedDate <= " + strNewFromDate + " AND dbo.tblELM.EnquiryFK IN (4,22,25,61)"

print"aa"
print strWhereClause


# Process: Make Query Table...
try:
    gp.MakeQueryTable_management("'Database Connections\\ELM_Test.odc\\dbo.qryELM';'Database Connections\\ELM_Test.odc\\dbo.tblELM'", elmFoIqueryTab, "USE_KEY_FIELDS", "dbo.qryELM.elmPK", "dbo.qryELM.elmPK dbo.qryELM.elmPK;dbo.qryELM.LoggedDate dbo.qryELM.LoggedDate;dbo.qryELM.ChannelFK dbo.qryELM.ChannelFK;dbo.qryELM.channelDesc dbo.qryELM.channelDesc;dbo.qryELM.statusName dbo.qryELM.statusName;dbo.qryELM.CategoryFK dbo.qryELM.CategoryFK;dbo.qryELM.CategoryDesc dbo.qryELM.CategoryDesc;dbo.qryELM.EnquiryFK dbo.qryELM.EnquiryFK;dbo.qryELM.EnquiryDesc dbo.qryELM.EnquiryDesc;dbo.qryELM.Description dbo.qryELM.Description;dbo.qryELM.Remarks dbo.qryELM.Remarks;dbo.qryELM.Location dbo.qryELM.Location;dbo.qryELM.statusFK dbo.qryELM.statusFK;dbo.tblELM.easting dbo.tblELM.easting;dbo.tblELM.northing dbo.tblELM.northing", strWhereClause)
except errorValue:
    print "Query Table error: " & errorValue

# Process: Make XY Event Layer...
try:
    gp.MakeXYEventLayer_management(elmFoIqueryTab, "dbo.tblELM.easting", "dbo.tblELM.northing", ELM_FoI_Results_Layer, "PROJCS['British_National_Grid',GEOGCS['GCS_OSGB_1936',DATUM['D_OSGB_1936',SPHEROID['Airy_1830',6377563.396,299.3249646]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',400000.0],PARAMETER['False_Northing',-100000.0],PARAMETER['Central_Meridian',-2.0],PARAMETER['Scale_Factor',0.9996012717],PARAMETER['Latitude_Of_Origin',49.0],UNIT['Meter',1.0]];IsHighPrecision")
except errorValue:
    print "XYEventLayer error: " & errorValue

# Process: Select Layer By Location...
try:
    gp.SelectLayerByLocation_management(ELM_FoI_Results_Layer, "WITHIN_A_DISTANCE", ROADS, Buffer_Distance, "NEW_SELECTION")
except errorValue:
    print "SelectLayer error: " & errorValue

# Process: Select Layer By Attribute...
try:
    gp.SelectLayerByAttribute_management(ELM_FoI_Results_Layer, "ADD_TO_SELECTION", "dbo.qryELM.Location LIKE '%Road Name%'")
except errorValue:
    print "SelectLayerByAttribute error: " & errorValue
Tags (2)
0 Kudos
4 Replies
GeraldineEGGERMONT
New Contributor II
Hello Chris,

I suppose you work with 9.x as you use gp and not arcpy.
With ArcGIS 10.x, your outputs are added to ArcMap when you run your script from the Python window. But this is not always convenient if you have input parameters to enter before running the tool.
When running your script as a script tool, you should use gp.SetParameterAsText() to turn your results into output parameters, in this case they will be added to the TOC.
You should then also use gp.GetParameterAsText() instead of sys.argv[].
In the properties of your script tool, add parameters to match your script parameters. Set the type of your 'result' parameters to 'Derived' and direction to 'Output'.

A small example:

Workspace = gp.GetParameterAsText(0) # a gdb
FCname = "line" 
fc = Workspace + "\\" + FCname
gp.CreateFeatureclass_management(Workspace, FCname, "POLYLINE")
gp.SetParameterAsText(1, fc)




This works with 10.x, I suppose it is the same with 9.x.
Hope this helps.
0 Kudos
ChrisMatthews
New Contributor III
I've looked at your example I assume as I'm creating a temporary dataset (i.e. an event layer) that I only need;

gp.SetParameterAsText(1, ELM_FoI_Results_Layer)

I'm still getting the same result i.e. script reports running successfully, but I get no output.




Hello Chris,

I suppose you work with 9.x as you use gp and not arcpy.
With ArcGIS 10.x, your outputs are added to ArcMap when you run your script from the Python window. But this is not always convenient if you have input parameters to enter before running the tool.
When running your script as a script tool, you should use gp.SetParameterAsText() to turn your results into output parameters, in this case they will be added to the TOC.
You should then also use gp.GetParameterAsText() instead of sys.argv[].
In the properties of your script tool, add parameters to match your script parameters. Set the type of your 'result' parameters to 'Derived' and direction to 'Output'.

A small example:

Workspace = gp.GetParameterAsText(0) # a gdb
FCname = "line" 
fc = Workspace + "\\" + FCname
gp.CreateFeatureclass_management(Workspace, FCname, "POLYLINE")
gp.SetParameterAsText(1, fc)




This works with 10.x, I suppose it is the same with 9.x.
Hope this helps.
0 Kudos
GeraldineEGGERMONT
New Contributor II
Chris,

Are you sure your feature layer is actually created? If you run your script tool in ArcMap (or even in ArcCatalog) and then open a tool using a feature layer as input (e.g. Select Layer By Location or Attribute) from the toolbox, do you see, in the input parameter window, a drop-down with your feature layer listed?
0 Kudos
ChrisMatthews
New Contributor III
Chris,

Are you sure your feature layer is actually created? If you run your script tool in ArcMap (or even in ArcCatalog) and then open a tool using a feature layer as input (e.g. Select Layer By Location or Attribute) from the toolbox, do you see, in the input parameter window, a drop-down with your feature layer listed?


It is not appearing as an input parameter.

I've updated my code and added the following line, to create a layer file. The script is creating the layer file and I'm able to load this manually. I believe this proves the script is successfully creating the correct result.

gp.SaveToLayerFile(ELM_FoI_Results_Layer, "c:/temp/_event_test.lyr")
0 Kudos