How to allow empty output layers from geoprocessing tool

01-27-2019 02:17 PM
New Contributor III

I'm running ArcGIS Server 10.6 and have a geoprocessing tool (built as part of a Python toolbox) that, depending on inputs, can have between 1 and 120 output layers created (a mixture of feature classes and raster datasets). I have some input tickboxes (a handful, not 120) which determine which of these output layers are generated. The output layers are populated by using the arcpy.SetParameter function.

I publish the tool with all the tickboxes ticked - i.e. all the outputs are generated before publishing. Then I run the geoprocessing task on the server with only some of the tickboxes ticked. In the code, this means that the arcpy.SetParameter function is only called for some of the output parameters. If I do this, then I get the following error:

Invalid return value: d:\arcgis\arcgisserver\directories\arcgisjobs\emptydatasettest_gpserver\j7d4d794fa57f46fe9740e483f3f18e79\scratch\outputSF.shp

Here's a much simplified example of what I'm talking about:

Tool parameters

params = []

# 0 Tickbox
param = arcpy.Parameter() = u'Tickbox'
param.displayName = u'Output the shapefile as a layer?'
param.parameterType = 'Required'
param.direction = 'Input'
param.datatype = u'Boolean'
param.value = u'False'

# 1 Input_shapefile
param = arcpy.Parameter() = u'Input_shapefile'
param.displayName = u'Input shapefile'
param.parameterType = 'Required'
param.direction = 'Input'
param.datatype = u'Feature Layer'

# 2 Output_shapefile
param = arcpy.Parameter() = u'Output_shapefile'
param.displayName = u'Output shapefile'
param.parameterType = 'Derived'
param.direction = 'Output'
param.datatype = u'Feature Layer'

return params

Tool execution code

if tickbox == 'true':
    outShapefile = os.path.join(arcpy.env.scratchFolder, "outputSF.shp")
    arcpy.CopyFeatures_management(inShapefile, outShapefile)
    arcpy.SetParameter(2, outShapefile)

If the tickbox is checked then the output layer displays correctly. If the tickbox is not ticked then the above error is thrown. I've been able to add a workaround so that if the tickbox isn't checked then an empty shapefile is used to set the parameter value. However, as I've got 120 potential outputs, I don't want to employ this workaround as I'd possibly end up with (say) 5 output layers that have values and 115 ones that are empty, which is pretty ugly.

If I publish the tool with none of the tickboxes checked, then the output layers are not generated even if I tick the boxes.

Does anyone know of any way to have an output layer declared in the tool parameters and is then not populated by the code without generating this error?


0 Replies