1

I want to split a shapefile so each feature becomes its own shapefile. The below works:

with arcpy.da.SearchCursor(catch10, ["FID", "SHAPE@"]) as cursor:
     for row in cursor:
      out_name = "Sup_" + str(row1[0]) # 
      arcpy.FeatureClassToFeatureClass_conversion(row1[1],outSup, out_name, **field_mapping=**)

But the attributes don't come with it. It gives back a shapefile with FID (0), Shape, ID (0). I have tried different entries under field mapping -- long strings copied from results and shorter {"Name" etc) -- but nothing has changed it. I'm missing something

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
woot
  • 133
  • 8
  • I think this is a duplicate of http://gis.stackexchange.com/questions/9998/exporting-feature-class-into-multiple-feature-classes-based-on-field-values-usin – PolyGeo May 25 '16 at 22:10
  • The split by attribute tool seems perfect for the job, but what happened to it? The code above is based on aaron's answer to that question (further down). This doesn't bring over my attributes – woot May 25 '16 at 22:30
  • Did you run this tool? http://www.umesc.usgs.gov/management/dss/split_by_attribute_tool.html – klewis May 25 '16 at 22:37
  • Try including all fields in your arcpy.da.SearchCursor... you're limiting it to ["FID", "SHAPE@"] according to http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000 specify '*' to have all fields. – Michael Stimson May 25 '16 at 22:40
  • I think this is the best answer there (http://gis.stackexchange.com/a/152165/115) - it is a similar coding pattern to the one I teach in the eLearning video tutorial that I mention in my answer there. – PolyGeo May 25 '16 at 22:42
  • @klewis -- can one use that tool (an add-in on the toolbar) with arcpy? how do you call it? – woot May 25 '16 at 23:15
  • @MichaelMiles-Stimson -- I tried a few versions of * ( should be ["*"] or "*", right?) but the tool throws an error. I also tried ["Name"] but the tuple index is out of range – woot May 25 '16 at 23:21

1 Answers1

2

The row object can't be used in the manner that you're attempting:

arcpy.FeatureClassToFeatureClass_conversion(row1[1],outSup...

Because it's a row and not a feature class, to split your feature class to a single feature per output:

desc = arcpy.Describe(catch10)
OID_Field = desc.OIDFieldName # the name of the OID field, could be OID, OBJECTID, FID or other

with arcpy.da.SearchCursor(catch10,'*') as SCur: # really all you need here is arcpy.da.SearchCursor(InData,[OID_Field])
    for row in SCur:
        print 'OID {0}'.format(row[0])
        out_name = "Sup_{0}".format(row[0])
        arcpy.FeatureClassToFeatureClass_conversion(catch10,outSup,out_name,'{0} = {1}'.format(OID_Field,row[0]))

It is important to describe the data first and obtain the OID field name, this saves maintaining a script for shapefiles and a different one for geodatabases, then start your cursor with only the OID field, as that's all that's needed to supply the whereclause OIDFieldName = This_row_OID, then specify the source data in FeatureClassToFeatureClass, note that the Select tool also could be implemented here (different to the other select tool so make sure you're using the right one).

Michael Stimson
  • 25,566
  • 2
  • 35
  • 74