Select to view content in your preferred language

missing link between toolbox and tool

3212
1
09-22-2012 05:07 AM
ElaineKuo
Regular Contributor
System ArcGIS 9.3
Windows Vista

Hello

I want to run a arcscript (as attached) to do batch work.

However, the error always showed that environment or tool not found.
(gp.SplitByAttribute(inputFC, fieldName, outputWS, filePrefix) AttributeError: Object: Tool or environment not found)

Please kindly help check if the following code is correct.
If so, please kindly advise if it is possible to have missing link between the toolbox and tool.

PS shapefile is also attached in test1.

Thank you.

 ##Script Name: split rows
##Description: using 
##Created By: Elaine Kuo
##Date: 23/09/2012


#Import standard library modules
import arcgisscripting
import os

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


#Set the workspace.
gp.Workspace= "H:/temp/test1"


#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp/test1"

inputFC = "H:/temp/test1/geor0247_Dissolve.shp"
fieldName = "C0247"
outputWS = "H:/temp/test1"
filePrefix = "geor0427"


# Add a toolbox with a model to the geoprocessor and set the workspace
gp.AddToolbox("C:/TEMP/AdditionalAnalysis/Additional Analysis - Generic Tools.tbx")



#### Execute Script Tool ###
gp.SplitByAttribute(inputFC, fieldName, outputWS, filePrefix)
  

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

0 Kudos
1 Reply
MarcinGasior
Frequent Contributor
If you still can't get this external tool to use try the following script which do basicaly the same. You can then incorporate it in your script.
import arcgisscripting, os
gp = arcgisscripting.create(9.3)

inputFC = r"Z:\TestSHP\geogrC001.shp"
fieldName ="C001"
outWorkspace = r"Z:\Work\TestSHPout"
filePrefix ="geor" + fieldName[1:] #eg. geor001

#find a type of declared field
fields = gp.ListFields(inputFC, fieldName)
fieldType = fields[0].Type


#create a list of values in declared field
valuesLst = []
searchCursor = gp.SearchCursor(inputFC, "", None, fieldName)
row = searchCursor.next()
while row:
    valuesLst.append(row.getValue(fieldName))
    row = searchCursor.next()
del searchCursor

#make the list unique
uniqueValuesLst = set(valuesLst)


#create new feature class for each unique value
for value in uniqueValuesLst:
    #build output feature class path
    outFCName = filePrefix + "_" + str(value)
    outFC = os.path.join(outWorkspace, outFCName)

    #build SQL WHERE clause depending :
    if fieldType == "String":
        whereClause = fieldName + " = '%s'" % value
    else:
        whereClause = fieldName + " = %s" %value

    #extract values to new feature classes
    gp.Select_analysis(inputFC, outFC, whereClause)
0 Kudos