class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "COF" self.alias = "cof" # List of tool classes associated with this toolbox self.tools = [Split] class Split(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "Split by Unique Value" self.description = "Splits an input featureclass into multiple feature classes by a unique value." self.canRunInBackground = True def getParameterInfo(self): """Define workspace parameter""" workspace = arcpy.Parameter( displayName = "Workspace", name = "in_workspace", datatype = "DEWorkspace", parameterType = "Required", direction = "Input",) """Apply workspace parameter filter""" workspace.filter.list= ["Local Database"] """Define in_featureclass parameter""" in_featureclass = arcpy.Parameter( displayName = "Feature Class to Split", name = "in_fc", datatype = "Feature Layer", parameterType = "Required", direction = "Input") """Apply a Geometry Type Filter to in_featureclass""" in_featureclass.filter.list = ["Point", "Polygon", "Polyline", "Multipoint"] """Define unique field parameter""" unique_field = arcpy.Parameter( displayName = "Field containing Unique Values", name = "unique_values", datatype = "Field", parameterType = "Required", direction = "Input") """Apply a dependency to unique field parameter""" unique_field.parameterDependencies = [in_featureclass.name] """Define outfc naming prefix parameter""" pfx = arcpy.Parameter( displayName = "Output Prefix", name = "out_pfx", datatype = "GPString", parameterType = "Required", direction = "Input") params = [workspace, in_featureclass, unique_field, pfx] return params def isLicensed(self): return True def updateParameters(self, parameters): return def updateMessages(self, parameters): return def execute(self, parameters, messages): """The source code of the tool.""" arcpy.env.workspace = parameters[0].valueAsText fc = parameters[1].valueAsText field = parameters[2].valueAsText name = parameters[3].valueAsText uniqueSet = set([r[0] for r in arcpy.da.SearchCursor (fc, [field])]) for uniqueVal in uniqueSet: prefix = name + "_" + str(uniqueVal) arcpy.AddMessage("Splitting " + str(uniqueVal) + "...") arcpy.Select_analysis(fc, str(prefix), field + " = " + str(uniqueVal)) arcpy.AddMessage("Success.") arcpy.AddMessage("All Unique Values successfully exported.") return
Solved! Go to Solution.
Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??
Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??
Thanks Curt!
That was it, except now it's saying my expression is invalid.
ExecuteError: ERROR 000358: Invalid expression DID = 27242
Failed to execute (Select).
Can you spot anything wrong with it?
arcpy.Select_analysis(fc, str(prefix), "'" + field + "' = " + "'" + str(uniqueVal) + "'")
The name is invalid because names in the gdb cannot contain ".".
The ".0" is appearing I would guess because if the input field is float or double, the string representation in Python includes the ".0".