2

I want to write a for-loop where I select random subsets of a polygon shapefile (10% of the 600 total polygons) and save each subset to a new shapefile, named subset1, subset2, etc.

My code successfully runs and outputs shapefiles with the correct name; however, each output shapefile is identical and contains all of the polygons in the original shapefile, not the subset selected prior. It doesn't seem to honor my feature selections the way that manually right-clicking Data > Export Data > Export (Selected features) does.

#define variables 
Polygons600 = r"C:\DataFolder\600Polygons.shp"
out_path = r"C:\OutputFolder"

# defines subset selection tool, ignoring any current selections in ArcMap
def SelectRandomByPercent (layer, percent):
    #layer variable is the layer name in ToC
    #percent is percent as whole number  (0-100)
    if percent > 100:
        print "percent is greater than 100"
        return
    if percent < 0:
        print "percent is less than zero"
        return
    import random
    fc = arcpy.Describe (layer).catalogPath
    featureCount = float (arcpy.GetCount_management (fc).getOutput (0))
    count = int (featureCount * float (percent) / float (100))
    if not count:
        arcpy.SelectLayerByAttribute_management (layer, "CLEAR_SELECTION")
        return
    oids = [oid for oid, in arcpy.da.SearchCursor (fc, "OID@")]
    oidFldName = arcpy.Describe (layer).OIDFieldName
    delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
    randOids = random.sample (oids, count)
    oidsStr = ", ".join (map (str, randOids))
    sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
    arcpy.SelectLayerByAttribute_management (layer, "", sql)

for i in range(1,100):
    SelectRandomByPercent("My Layer Title", 10)
    arcpy.FeatureClassToFeatureClass_conversion(Polygons600, out_path, "Polygons600_subset" + str(i) + ".shp", "", "", "")

Other questions/answers on this site recommend using FeatureClassToFeatureClass_conversion or CopyFeatures_management to subset shapefiles, claiming they honor feature selections when they export. I have tried both in my loop (and I've also tried creating a .lyr version of my input .shp and using that as the selection layer) to no avail.

Does this problem have to do with how I've selected my subset polygons (the SelectRandomByPercent tool)? For instance, if I select a subset of my shapefile using a SQL statement in the FeatureClassToFeatureClass argument it exports just fine. How can I make FeatureClassToFeatureClass or CopyFeatures recognize my selections?

  • arcpy.SelectLayerByAttribute_management() requires a Feature Layer, you can't run a selection straight on a shapefile. It looks like you're trying to select the features in the shapefile directly. – Midavalo Oct 19 '16 at 0:11
  • 1
    Yes, I noticed this disconnect too: my process for selecting features calls on the layer in the ToC while arcpy.FeatureClassToFeatureClass_conversion calls on the original shapefile. I'm not sure how to modify the syntax of the arcpy.FeatureClassToFeatureClass_conversion to refer to the Feature Layer and not the original shapefile, however. – neirbom9 Oct 19 '16 at 0:32
  • 1
    Change your Polygons600 = r"C:\DataFolder\600Polygons.shp" to Polygons600 = arcpy.mapping.Layer(r"C:\DataFolder\600Polygons.shp") and you will be fine. – fatih_dur Oct 19 '16 at 1:35
  • 2
    @Midavalo, I have just tested with a shapefile (since I know it accepts GDB feature classes) to make sure and can confirm that it works. That's why I L O V E arcpy.mapping.Layer(). – fatih_dur Oct 19 '16 at 4:35
  • 1
    @Midivalo your answer works great, thanks! @fatih_dur I tried your method, but the output shapefiles still contain the full polygon set and not the subset. Maybe this is because the code still refers to the original 600Polygons.shp on disk and not the instance I have open in ArcMap that I'm selecting from? – neirbom9 Oct 19 '16 at 16:43
3

In your selection, you are selecting a layer in your MXD, but your Feature Class to Feature Class you are copying the actual shapefile, not the layer.

Change the shapefile reference in the arcpy.FeatureClassToFeatureClass_conversion() to instead reference the layer:

for i in range(1,100):
    SelectRandomByPercent("My Layer Title", 10)
    arcpy.FeatureClassToFeatureClass_conversion("My Layer Title", out_path, "Polygons600_subset" + str(i) + ".shp", "", "", "")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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