3

I am trying to export individual shapefiles of all 2 pair combinations from a multi-feature polygon shapefile.

To explain this better as set of FID's 0, 1, 2, 3, 4 would be exported as individual shapefiles of FID pairs:

('0', '1'), ('0', '2'), ('0', '3'), ('0', '4'), ('1', '2'), ('1', '3'), ('1', '4'), ('2', '3'), ('2', '4'), ('3', '4')

I am not so good with Python so my attempts with itertools.combination etc have not worked out.

0

1 Answer 1

6

As @MikeToews suggests, using itertools.combinations you can get the list of combinations based on the FID, so you'd have a script that looks something like:

import arcpy
import itertools

arcpy.env.workspace = r"C:\path\to\workspace"

base_shapefile = "some_shapefile.shp"

oid_field = arcpy.Describe(base_shapefile).OIDFieldName
feature_count = int(arcpy.GetCount_management(base_shapefile).getOutput(0))

for (i, j) in itertools.combinations(range(feature_count), 2):
    arcpy.Select_analysis(base_shapefile , "combination_%i_%i.shp" % (i, j), '"%s" in (%i, %i)' % (oid_field, i, j))
0

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