Python Script: Create separate feature classes for each record in shapefile

3104
12
05-28-2019 06:12 AM
MarkNorris-Rogers
New Contributor II

Dear Forum Members,

I need to create a separate feature class from each record in a shapefile
(i.e. each row in original shapefile needs to be a new feature class in its own right).
I need to name each new feature class by its ID number from the AWS_ID field in the original shapefile
See Python script below:

1.#Import required modules
2.import arcpy, os, traceback
3.from arcpy import env
4.
5.env.workspace = r"C:\temp\"
6.env. overwriteOutput = True
7.
8.# Script arguments
9.awsInput = r"C:\Temp\AWS.shp"
10.awsName = "AWS_ID" # Field in AWS.shp to be used as name for output shpfile
11.cursor = arcpy.SearchCursor(awsInput)
12.
13.for aws in cursor:
14. awsID = aws.getValue(awsName)
15. awsName = "{}_AWS".format(aws)
16. arcpy.CopyFeatures_management(awsInput, r"C:\Temp\DataExtract_Work.gdb\awsName", "0", "0", "0")

(NB. line numbers will affect indentation!)

I have tried to get it working by
a) searching the arcpy help - source of line 11, SearchCursor
b) reviewing Esri online Python courses I have done - source of line 15
c) viewing online video tutorials on ModelBuilder iteration

Before I added lines 14,15 but ran line 16,the script ran but either overwrote all but the last record or
added all the records as per the original shapefile. Currently I get a RuntimeError: ERROR 999999,
referring to line 14. Prior to adding lines 15-16, and having a print aws.getValue(aws) statement,
it printed out each ID number on a separate line.

Question 1. Where have I messed up?

Question 2. Does one have to first create a list if one is only using a single feature class/shapefile as the input?

Any help will be appreciated.

Kind regards,
Mark

0 Kudos
12 Replies
JoeBorgione
MVP Emeritus

First, use the arcpy.da.SearchCursor. It's faster and more reliable.  One thing you seem to be missing is a selection statement.  You'll need that to isolate each record.  In order to make a selection, you'll need to use arcpy.MakeFeatureLayer().

Psuedo code:

import arcpy
arcpy.MakeFeatureLayer('yourFeatureClass', 'layerName')

fields = ['OBJECTID', 'YourNeededFields']

with arcpy.da.SearchCursor('layerName,fields) as cursor:
  for row cursor:
     arcpy.SelectLayerByAttribute_management('layerName','NEW_SELECTION','OBJECTID = 
     {}.format(row[0])
     arcpy.CopyFeatures_management('layerName','YourOutFeatureClass')
That should just about do it....
MarkNorris-Rogers
New Contributor II

Hi Joe,

Thanks very much for your help. I did wonder about a select statement but the example code I looked at did not seem to use one - obviously I was looking at the wrong thing! I will apply your suggestions.

Thanks again,

Mark

0 Kudos
Egge-Jan_Pollé
MVP Regular Contributor

Hi Mark Norris-Rogers‌,

You might also have a look at this discussion, and at the answers given there: arcpy - Loop for exporting shapefiles from an existing shapefile - Geographic Information Systems St... 

HTH,

Egge-Jan

MarkNorris-Rogers
New Contributor II

Hi Egge-Jan,

Thanks very much. I will look up that link.

Kind regards,

Mark

0 Kudos
Luke_Pinner
MVP Regular Contributor

Have you considered the split by attributes tool? Split By Attributes—Help | ArcGIS Desktop 

MarkNorris-Rogers
New Contributor II

Hi Luke,

Thanks for this suggestion. I was not aware of this tool, but it looks like it will be of help. 

Kind regards,

Mark

0 Kudos
NeilAyres
MVP Alum

Perhaps the real question here is "why would you do that?". Very strange.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Sometimes it's necessary. When the consolidated database is just too big and unwieldy to use, even with spatial and attribute indexes and professional database tuning there's no other option. Think continental scale cadastre, tenure, species distributions. Definition queries just don't cut it in these cases.

MarkNorris-Rogers
New Contributor II

Hi Neil,

The client requires a separate table of attributes for each record in the point file. For each point, data needs to be extracted from a number of separate raster files to provide a record of all the raster attributes for that point, hence the need to break up the point data.

Thanks,

Mark

0 Kudos