Nested watershed

I'm trying to split up this nested watersheds by name. I want each watershed to be it's own shapefile. Is there a faster way to do this besides highlighting each watershed and then exporting it? I'm using Arcmap 10.3.

1 Answer 1


Arcpy is excellent for this. What you want is two loops: one to build a list of watersheds from the name field, and then one to select each watershed by turn, and then copy it to a new output shapefile.

Here's some code I wrote to solve this problem. It's intended as a custom script in the Toolbox, but you could modify it for one shot. Or feel free to bring it into the Toolbox if you want to use it for other projects.

I would also recommend looking through the ArcPy documentation - there are a lot of ways to automate the yak-shaving parts of GIS if you do your research.

import arcpy, time, os, traceback
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 = int(arcpy.GetParameterAsText(3))
# Overwrite output

#try loop

    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
                CodeString = ''.join(e for e in AttString if e.isalnum())
                IndNum = IndNum + 1
            elif IndNum == MaxSplit - 1:
                "{0} classes identified. Proceeding with script.".format(MaxSplit))

    #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.Select_analysis("Split_lyr", ClassFile)
        #arcpy.CopyFeatures_management("Split_lyr", ClassFile)
        arcpy.AddMessage("{0} has been created.".format(ClassFile))
        IterNum = IterNum + 1

except Exception:
    e = sys.exc_info()[1]
    arcpy.AddMessage("ERROR! \n Call Stack: " + traceback.format_exc())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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