Python scripting: using feature classes with GP services

I was working on a geoprocessing (GP) service that could allow a user to specify what feature class in a registered geodatabase on the server a GP task will work with. I’ve tested using the Truncate Table tool first to basically wipe all the rows from a feature class while preserving the schema.

After creating a simple model with just one tool and one model parameter (Input Table), I’ve run it successfully and have started the publishing process. Yet when publishing a model as a GP service, one would have three choices: “User defined value” (user specify value for the parameter – for instance, what features will be used as a FeatureSet), “Choice list” (this one gets created when running the model in ArcMap and having several layers added to the Table Of Contents), and finally “Constant value” (the same input data will be used for the parameter). The only applicable choice in my scenario is to use a user defined value when users specify what feature class in the geodatabase will be used. Since new feature classes will be created, the choice list option cannot be used. Using a constant value is not an option either since I want to let a user specify a feature class dynamically rather than let them run the GP task over the same feature class all the time.

However, FeatureClass is not a supported input type for GP Services which makes it impossible to publish and use my model directly. This means that I’ll have to implement a look-up logic and pass only a string to the service instead of the feature class itself. Such string would represent a feature class name. So, what I needed to do is to obtain a feature class name from a user, and then use the logic to append the name of the feature class to the geodatabase path. GP service will then find the feature class in a specified beforehand geodatabase, and run the Truncate Table tool.

I have had hard times making this possible in ModelBuilder by using the inline variable substitution for the input table path, so I’ve decided to completely switch to Python. I have created a script tool which asks for an input feature class name and then use this string in the data path which GP service can work with. The code is as below:

import arcpy
from arcpy import env
#Setting the env workspace to be an SDE geodatabase
env.workspace = r'C:\sde_at_DB101.sde'
#Getting the existing polygon feature class as input parameter
GetExistingPolyFC = arcpy.GetParameterAsText(0)
GetPolyFC = GetExistingPolyFC
#Truncate a feature class if it exists
if arcpy.Exists(GetPolyFC):
arcpy.AddMessage("Polygon feature class exists, start working...")
arcpy.TruncateTable_management(r'C:\sde_at_DB_101.sde\%s' %GetPolyFC)
arcpy.AddMessage("Truncating table is complete")
arcpy.AddMessage("Polygon feature class does not exist")

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s