johnmdye

Concatenate Items in Set that have spaces in Item Namespace

Discussion created by johnmdye on Oct 22, 2013
Latest reply on Oct 30, 2013 by msayler
Write a toolbox tool to parse features by unique attribute. Trying to make it fairly robust, got it working for String, not concerned about any of the other possible datatypes. However, I do have a concern that the end user will input a dataset which in it's unique value field, has values that contain spaces. Since spaces are not allowed in Feature Class names, I want to automatically scan the items in the list and if any of the items contain a space, go ahead and concatenate those two separate namespaces.

So if one of the unique values came up as:
"St Cloud"

The way the tool is currently set up, it outputs a feature class named:
"St"

But I want it to detect the space and just concatenate the two words in the given namespace when specifying the name for the output dataset in the select_analysis function, so the desired output feature class would be named:
"StCloud"


Not too sure how to go about it from here, but here's the code thus far, will add the other data types later but I think String is the only one that could possible contain a space within the item namespace anyway:
"""The source code of the tool."""
        # Instantiate Parameters
        ParseFeature = parameters[0].valueAsText
        UniqueVal_Field = parameters[1].valueAsText
        workspace = parameters[2].valueAsText

        # Set Workspace
        arcpy.env.workspace = workspace

        """ Determine the UniqueVal_Field's datatype """
        # Create a Field Object so that it's properties can be interrogated
        Field = arcpy.ListFields(ParseFeature, str(UniqueVal_Field))
        # Extract the Field Object's MetaData
        FieldDesc = arcpy.Describe(Field)
        # Extract the Field Object DataType from the Field Object's MetaData
        FieldType = FieldDesc.type
        # If the Field's DataType is determined to be of type 'Blob', 'Geometry', 'GUID', 'OID' or 'Raster'
        if FieldType == "Blob" or FieldType == "Geometry" or FieldType == "GUID" or FieldType == "OID" or FieldType == "Raster":
            # Raise an error and terminate as those datatypes are not supported by this tool
            arcpy.AddError("The Datatype of " + str(FieldType) + " is not supported by this tool.")
            sys.exit[1]
        # Otherwise
        else:
            # Continue
            pass

        """ Parse the Dataset """
        # Extract Unique Values from Parse Feature's UniqueVal_Field
        UniqueValues = set([r[0] for r in arcpy.da.SearchCursor(ParseFeature, [UniqueVal_Field])])
        # Iterate through each Unique Value in the UniqueValues set
        for UniqueValue in UniqueValues:
            # If the UniqueVal_Field's data type is of type 'String'
            if FieldType == "String":
                arcpy.AddMessage("Extracting Features with unique value " + str(UniqueValue) + "...")
                arcpy.Select_analysis(ParseFeature, str(UniqueValue), '"' + UniqueVal_Field + '" = ' + "'" + str(UniqueValue) + "'")

Outcomes