3

I wrote an ArcMap script to go through a feature layer, identify unique values in a given field, and output a feature class from each unique value to its own feature class or shapefile. As an exercise, I decided to experiment with allowing the user to set a limit on the number of outputs, so the script would only take the first 10 or 20 unique values and create a feature class from each in a designated workspace.

However, if there are more unique values than the limit, the script deletes whatever geodatabase or folder is designated as its output location, along with any files in that location. Occasionally, the script will throw a 000210 error when creating its first file for no reason that I can determine (the workspace is not open in ArcCatalog or ArcMap, the file name is valid, and nobody else in my agency is using it) and if this happens, the output location is also deleted.

Below is my script in its entirety, except for a header that is all comments. Has anyone experienced similar behavior from a script? Any ideas why this is happening? I've written a number of scripts, both stand-alone and added to ArcMap, and I'm terrified to write any more until I know why this one is acting so oddly.

import arcpy, time, os
from arcpy import env

#set parameters
# SplitFeat - input features
SplitFeat = arcpy.GetParameterAsText(0)
# SplitField - input field
SplitField = arcpy.GetParameterAsText(1)
# OutLocation - output folder
OutLoc = arcpy.GetParameterAsText(2)
#MaxSplit = Maximum number of files to create
MaxSplit = arcpy.GetParameterAsText(3)
# Overwrite output

arcpy.env.workspace = OutLoc
arcpy.env.overwriteOutput = True

IndNum = 0

arcpy.MakeFeatureLayer_management(SplitFeat, "Split_lyr")
CatList = []
CodeList = []
FldList = (SplitField)
with arcpy.da.SearchCursor("Split_lyr", SplitField) as cursor:
    for row in cursor:
        if row[0] not in CatList and IndNum < MaxSplit:
            AttString = str(row[0])
            print AttString
            CatList.append(AttString)
            CodeString = ''.join(e for e in AttString if e.isalnum())
            CodeList.append(CodeString)
            IndNum = IndNum + 1
        elif IndNum == MaxSplit:
            arcpy.AddMessage("{0} classes identified. Proceeding with script...")
    else:
        "All classes identified. Proceeding with script..."

#splitting features
#new iteration number
IterNum = 0
for Cat in CatList:
    CodeName = CodeList[IterNum]
    ClassName = CodeName[0:30]
    ClassFile = os.path.join(OutLoc, ClassName)
    arcpy.AddMessage("Creating file {0}...".format(ClassFile))
    #select by attributes
    SelClause = "{0!s} = '{1!s}'".format(SplitField, Cat)
    arcpy.SelectLayerByAttribute_management("Split_lyr", "NEW_SELECTION", SelClause)
    #Copy features, using CodeList index as key
    arcpy.CopyFeatures_management("Split_lyr", ClassFile)
    arcpy.AddMessage("{0} has been created.".format(ClassFile))
    IterNum = IterNum + 1
  • Your code looks pretty solid to me. You could try alternatives for export, such as feature class to feature class (conversion) or select (analysis). select is good because you can skip the prior select layer by attribute. – Emil Brundage Mar 7 '16 at 15:56
  • I tried a Select_analysis and had the same problem - hit an 000210 error when creating its first child file, and deleted the destination folder. What the heck could be doing this? – Andy Bradford Mar 7 '16 at 16:15
  • 1
    There is no obvious reason why anything is being deleted. This is not a definite fix nor an answer as to the "why" but have you tried restarting your system? ArcMap and the IDLE, in my experience, are notorious for getting some wires crossed for no apparent reason, and can be "fixed" with a system restart. – Barbarossa Mar 7 '16 at 16:34
  • Hmmm. It doesn't make much sense that anything would be deleted, because the only real action in the entire script is the CopyFeatures. – Tom Mar 7 '16 at 16:34
  • 2
    Update: Found a candidate for the problem after looking at the first version, without a limit option. There, the parameter OutLoc is set as an Input. In V2 (the script posted above) OutLoc was set as an Output under Tool Properties>Parameters>Direction. For V2's Toolbox tool, I've set OutLoc's Direction to Input, and it's stopped deleting the output workspace when it hits an error. I suspect, but haven't been able to prove from ESRI documentation, that anything defined as an Output will get deleted as part of the normal clean-up process when a tool fails. – Andy Bradford Mar 7 '16 at 17:39
2

I tested your script it works as expected provided you change

MaxSplit = arcpy.GetParameterAsText(3)

to

MaxSplit = int(arcpy.GetParameterAsText(3))

Also I suggest to check option "Results are temporary by default" in Geoprocessing options

  • As I noted in comments above, my problem was the Output Location parameter set to "Output," which I think made ArcMap delete the output when the script exited half-done. But changing MaxSplit to an Int helped solve some bugs I hadn't been able to track down. Thanks! – Andy Bradford Mar 7 '16 at 21:26
  • Indentation is wrong as well with else: – FelixIP Mar 7 '16 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.