2

I am looking for a way to split a feature class into multiple feature classes based on the number of records, probably using python. E.g first 100, next 100, etc. I have so far only seen options to split by attributes.

10
  • 3
    Add a new field, calculate it to !Objectid! / 100. Use this new field in Split By Attribute.
    – klewis
    Jun 9, 2017 at 20:07
  • 1
    @ketar Have you tried that solution? Please don't say it doesn't work without trying it. Works perfectly for me.
    – Midavalo
    Jun 9, 2017 at 20:25
  • 3
    Please edit your question to include the code you used to split by attributes. Calculating !OBJECTID! / 100 should give you 100 records with the same ObjectID value, so when splitting by attributes the calculated value should put 100 records into each output feature class.
    – Midavalo
    Jun 9, 2017 at 20:39
  • 1
    @Bjorn There are ways around that if that is an issue. Calculating a new sequential field for example.
    – Midavalo
    Jun 9, 2017 at 20:42
  • 2
    The USGS offers a SplitByAttribute tool for ArcGIS 10.3. umesc.usgs.gov/management/dss/split_by_attribute_tool.html
    – klewis
    Jun 9, 2017 at 20:54

2 Answers 2

7

The following code will split the features into groups of 100 (or the value set in outputNum) then copy them to individual feature classes.

import arcpy

outputNum = 100
outputFCName = "OutputFC"

def listSplit(myList, n):
    for i in xrange(0, len(myList), n):
        yield myList[i:i + n]

arcpy.env.workspace = r"c:\gis\se\gisse.gdb"
lyr = arcpy.mapping.Layer("RandomPoints")
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
fList = list()

with arcpy.da.SearchCursor(lyr, "OID@") as cursor:
    for row in cursor:
        fList.append(row[0])

listGroup = listSplit(fList, outputNum)

for x in listGroup:
    lyr.setSelectionSet("NEW", x)
    arcpy.CopyFeatures_management(lyr, arcpy.CreateUniqueName(outputFCName))
4

As @klewis mentioned you could use the Split By Attribute feature:

Esri provides the following sample code that you could use as mentioned:

# Name: SplitByAttributes.py  
# Description: Use the SplitByAttributes tool to split a feature class by 
# unique values.

# Import required modules
import arcpy

#Set local variables  
in_feature_class = 'c:/data/base.gdb/ecology'  
target_workspace = 'c:/data/output.gdb'  
fields = ['REGION', 'ECO_CODE']  

arcpy.SplitByAttributes_analysis(in_feature_class, target_workspace, fields)
1
  • That would give me a feature class for each record. I want each feature class to have 100 features. E.g feature class 1 has 100 features, feature class 2 has the next 100, etc.
    – ketar
    Jun 9, 2017 at 20:14

Your Answer

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

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