Exporting individual polygons into their own FC

489
4
Jump to solution
08-29-2012 12:46 PM
by Anonymous User
Not applicable
I have a FC that contains 53 polygons.  I needed to get these each into another geodatabase in which each individual polygon in the master FC is its own FC in the other geodatabase. I am wondering if anyone knows of another method to do this in an easier (or more logical) way?  I will have to do this again with a few other files.

Here are the steps that I was thinking:
1. Make a feature layer for the original polygon file with a query (ID = x)
2. set up a loop where I have a counter to match ID for the query
3. Copy selected features into the geodatabase
4. Move on to next polygon with the counter and repeat till loop is finished

The script I wrote doing these steps works fine, but it is really slow. 

####  Exports all polygons in FC into individual FCs in different location import arcpy, os, sys, traceback  arcpy.env.workspace = 'G:\\PROJECTS\\Cedar\\Environmental\\FEMA\\Results\\lidar.gdb' arcpy.env.overwriteOutput = True  outpath = 'G:\\PROJECTS\\Cedar\\Environmental\\FEMA\\Results\\Processing.gdb' floodbuff = '\\FloodplainAE_1000ft' flyr = 'Stream_Reach'  try:      arcpy.MakeFeatureLayer_management(floodbuff, flyr)     result = arcpy.GetCount_management(flyr)      print result     print 'Exporting polygon features into their own fc'      fid = 1     while fid <= result:                  query = '"ID" = ' + str(fid)         arcpy.SelectLayerByAttribute_management(flyr, 'NEW_SELECTION', query)          # Copy selected polygon into its own FC         arcpy.CopyFeatures_management(flyr, outpath + os.sep + flyr + str(fid))          fid += 1      print 'All polygons exported successfully'      except:      arcpy.AddError(arcpy.GetMessages(2)) 


I feel like there has to be an easier way to do this, or one that will cut down on processing time (it took about 10 minutes for 53 features with only a few attributes).  Anyone have any ideas?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
No need to reinvent the wheel. There is the Split Layer By Attributes tool.

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9...
0 Kudos
by Anonymous User
Not applicable
Mathew,

Thank you!!! This is exactly what I was looking for!  I knew there had to be an easier way than what I did.  Creating a loop to do that didn't seem very practical. 🙂

Caleb
0 Kudos
SamCoggins1
New Contributor III
If you didn't want to use the Split Layer By Attributes tool, I used this recently in a script:

Replace everything between << >> with your own file names etc.


import arcpy
import os

# Phase I: Select and export individual polygons from the Polygon input file
count = 0
copyRow = ""
rowSearch = arcpy.SearchCursor( <<POLYGON FILE HERE>> )
for row in rowSearch:
    if copyRow != row.FID:
        copyRow = row.FID

    # Polygon output variable after Select tool has run
    pathPoly = <<WORKSPACE>> + '\ <<POLYGON NAME>>' + str(count)+ ".shp"

    # FID for Select tool
    FID = "\"FID\" = " + str(count) << GIVE UNIQUE IDENTIFIER HERE >>

    # Select tool to export individual polygons by row from the input variable
    arcpy.Select_analysis(Polygons, pathPoly, FID)

0 Kudos
by Anonymous User
Not applicable
Sam,

Thanks for sharing this with me as well.  I will try this one out with the next file I have to split up.

There was one thing I had to add to this to make it work properly in case anyone is interested:

if copyRow != row.FID:
        copyRow = row.FID


I had to add:

if copyRow != row.FID:
        count = row.getValue("FID")
        copyRow = row.FID


Other than that, Sam's script worked perfectly and was definitely much faster!
0 Kudos