cmatthews

Adding Python result to ArcGIS ToC

Discussion created by cmatthews on Dec 19, 2013
Latest reply on Dec 19, 2013 by cmatthews
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

Outcomes