Wednesday 28 August 2019

arcpy - Multivalue parameter as text in Python toolbox - reading in to imported module function


I'm building a Python toolbox with a tool which calls specific functions from a custom module.


The main input parameter for this tool is a multivalue list of raster layers.


In the execute block of my tool, I use parameter[2].valueAsText to read in said parameter, as normal, and then feed it into a custom module function as follows:


output          = parameters[0].valueAsText
input = parameters[2].valueAsText
function = parameters[1].valueAsText
functionname = "Local" + function
option = parameters[3].valueAsText

expression = parameters[4].valueAsText

newGrid = getattr(localfuncs,functionname)(input,option,expression)
newGrid.save(output)

In this case, I've tested it with one custom function (corresponding to 'functionname'):


def LocalMean(input,option,expression):
import arcpy
newGrid = arcpy.sa.CellStatistics(input,"MEAN")
return newGrid


The trouble is that the 'input' (multivalue read in as text) ends up being a semicolon-delimited string of file names, and the function in the custom module can't seem to convert it back to a list of raster layers for Cell Statistics to actually process. As a result I get this ERROR 000732:


Script failed because:      ERROR 000732: Input Raster: Dataset Development;Vegetation;Hydrology;Elevation does not exist or is not supported at this location: 

I've tried reading in 'input' with .values instead of .valueAsText, so it comes in as a list, but then I get the error that 'cannot concatenate 'str' and 'list' objects' in the getattr line.


Any ideas what I can do about this?



  • I know this overall structure may seem unnecessarily complicated; but it's a key part of the bigger project. I'm not attached to specifically using getattr here, but solutions which preserve the basic structure, namely calling functions from a custom module using a string of the function's name, would be ideal




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...