Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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')

Im not so good with python so my attempts with intertools.combination etc have not worked out. Any help would be appreciated. Thanks for any advice and help.

share|improve this question
2  
You tried list(itertools.combinations(range(5), 2))? –  Mike T Apr 12 '12 at 23:41
add comment

1 Answer 1

up vote 4 down vote accepted

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))
share|improve this answer
    
Note that this will only work for shapefiles, if it's in a geodatabase then it gets an OBJECTID which are not necessarily sequential (e.g. if you delete a feature). In that case you would need to create a list of all the OIDs and run that through combinations. –  blah238 Apr 13 '12 at 2:34
    
Thanks @om_henners this works great. I have learned a lot from this. To make it work in my environment I did add a arcpy.env.workspace = r"S:\folder_path" statement at the front of the script to specify where the new shapefiles can be stored. –  user6784 Apr 13 '12 at 17:27
    
No worries @user6784 - modified the script so that workspace is there. –  om_henners Apr 14 '12 at 10:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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