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 00:11arcpy.FeatureClassToFeatureClass_conversioncalls on the original shapefile. I'm not sure how to modify the syntax of thearcpy.FeatureClassToFeatureClass_conversionto refer to the Feature Layer and not the original shapefile, however. – neirbom9 Oct 19 '16 at 00:32Polygons600 = r"C:\DataFolder\600Polygons.shp"toPolygons600 = arcpy.mapping.Layer(r"C:\DataFolder\600Polygons.shp")and you will be fine. – fatih_dur Oct 19 '16 at 01:35arcpy.mapping.Layer().– fatih_dur Oct 19 '16 at 04:35600Polygons.shpon disk and not the instance I have open in ArcMap that I'm selecting from? – neirbom9 Oct 19 '16 at 16:43SelectRandomByPercent("My Layer Title", 10)to include reference toPolygons600parameter -SRandomByPercent(Polygons600, 10)which will make both the select and the Feature class to Feature class refer to the same layer – Midavalo Oct 19 '16 at 17:00