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)
def LocalMean(input,option,expression): import arcpy newGrid = arcpy.sa.CellStatistics(input,"MEAN") return newGrid
Solved! Go to Solution.
def LocalMean(input,option,expression): import arcpy newGrid = arcpy.sa.CellStatistics(input.split(';') ,"MEAN") return newGrid
def LocalMean(ws, input, option, expression): import arcpy, os rasters = [os.path.join(ws, r) for r in input.split(';')] newGrid = arcpy.sa.CellStatistics(rasters ,"MEAN") return newGrid
def fixArgs(arg, arg_type=list): ''' fixes arguments from a script tool. for example, when using a script tool with a multivalue parameter, it comes in as "val_a;val_b;val_c". This function can automatically fix arguments based on the arg_type. Another example is the boolean type returned from a script tool. Instead of True and False, it is returned as "true" and "false" arg: argument from script tool (arcpy.GetParameterAsText() or sys.argv[1]) (str) arg_type: type to convert argument from script tool parameter # Example: >>> # example of list returned from script tool multiparameter argument >>> arg = "val_a;val_b;val_c" >>> fixArgs(arg, list) ['val_a', 'val_b', 'val_c'] ''' if arg_type == list: if isinstance(arg, str): # need to replace extra quotes for paths with spaces return map(lambda a: a.replace("'",""), arg.split(';')) else: return list(arg) if arg_type == float: try': return float(arg) except: return 0 if arg_type == int: try: return int(arg) except: return 0 if arg_type == bool: if str(arg).lower() == 'true' or arg in [True, 1]: return True else: return False if arg_type == str: if arg in [None, '', '#']: return '' return arg
def LocalMean(input,option,expression): import arcpy newGrid = arcpy.sa.CellStatistics(input.split(';') ,"MEAN") return newGrid
def LocalMean(ws, input, option, expression): import arcpy, os rasters = [os.path.join(ws, r) for r in input.split(';')] newGrid = arcpy.sa.CellStatistics(rasters ,"MEAN") return newGrid
def fixArgs(arg, arg_type=list): ''' fixes arguments from a script tool. for example, when using a script tool with a multivalue parameter, it comes in as "val_a;val_b;val_c". This function can automatically fix arguments based on the arg_type. Another example is the boolean type returned from a script tool. Instead of True and False, it is returned as "true" and "false" arg: argument from script tool (arcpy.GetParameterAsText() or sys.argv[1]) (str) arg_type: type to convert argument from script tool parameter # Example: >>> # example of list returned from script tool multiparameter argument >>> arg = "val_a;val_b;val_c" >>> fixArgs(arg, list) ['val_a', 'val_b', 'val_c'] ''' if arg_type == list: if isinstance(arg, str): # need to replace extra quotes for paths with spaces return map(lambda a: a.replace("'",""), arg.split(';')) else: return list(arg) if arg_type == float: try': return float(arg) except: return 0 if arg_type == int: try: return int(arg) except: return 0 if arg_type == bool: if str(arg).lower() == 'true' or arg in [True, 1]: return True else: return False if arg_type == str: if arg in [None, '', '#']: return '' return arg