I have multiple shapefiles in a folder and I want to intersect all of them which overlap. So far I am making two lists and overlapping all of the possible file combinations (I am using a cartesian list in this example, although in hindsight I think I could have skipped that step).

I am using this code:

import arcpy, os, itertools

shapefiles=[os.path.join(arcpy.env.workspace, i) for i in arcpy.ListFeatureClasses('*.shp')]
shapefiles2=[os.path.join(arcpy.env.workspace, i) for i in arcpy.ListFeatureClasses('*.shp')] #same shapefiles but used to find the file combinations

if not os.path.isdir(out):

result = list(itertools.product(shapefiles,shapefiles2))

for files in result:

     for i in x:
         outname=x.split('\\')[-1].split('.')[0] + '_' + y.split('\\')[-1]
         arcpy.Intersect_analysis([x, y], os.path.join(out,outname))

print "Done"

This will intersect all the possible combinations, but since there are so many combinations it is extremely slow (the majority don't overlap), I am thinking there is a faster way to determine which ones instersect in the first place and then only instersect those ones. My code above also doesn't deal with the fact that more than 2 shapefiles can overlap.


You could test their extents first.

For example, if the maxX of one is less than the minX of another then they should not overlap as long as the feature class extents accurately reflect their contents.

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.