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.
2 Answers
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))
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)
-
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.– ketarJun 9, 2017 at 20:14
!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.