1

I have two folders with shapefiles: r'E:\SO\Polygons' and r'E:\SO\Districts'

Within each folder I have 215 shapefiles named with similar naming conventions: poly_100 or dist_100. I need to clip the polygons by the districts if their ending digits are equal. I am new to python and at a loss:

    #folder for clipped Voronoi polygons
Voronoi_file = str(maindir) + "/" + "Voronoi_polygons"
if not os.path.exists(Voronio_file): os.makedirs(Voronoi_file)

env.workspace = r'E:\SO\Polygons'
outputworkspace= Voronoi_file
count=0
for fc in arcpy.ListFeatureClasses():
    end1 = str(fc[4:])
    env.workspace = r'E:\SO\Districs'
    for fc in arcpy.ListFeatureClasses():
            end2 = str(fc[4:])
            if end1==end2:
                out_name = str((outputworkspace) + "/" + "dis_poly" + str(count))  # Assemble the output point name and path
                arcpy.Clip_analysis(end1, end2, out_name)
                count = count + 1
  • It's "Voronoi" polygons :) – Paulo Raposo Nov 26 '15 at 3:53
0

This should do it. It takes advantage of your naming convention, and assumes there is a matching "dist_xxx.shp" for every "poly_xxx.shp".

import arcpy
import os

def getShapefiles(aDir):
    # Get all the files in the directory
    allfiles = os.listdir(aDir)
    # Isolate only the *.shp files
    shpOnly = [f for f in allfiles if (os.path.splitext(f)[1] == ".shp")]
    return shpOnly

def makeDictionary(aFCList):
    aDict = {}
    for fc in aFCList:
        idNumber = fc.split("_")[1]
        aDict[idNumber] = fc
    return aDict

# Input files
polysDir =  r'E:\SO\Polygons'
districtsDir = r'E:\SO\Districts' # Note spelling correction

# Isolate the .shp files in lists
polys = getShapefiles(polysDir)
districts = getShapefiles(districtsDir)

# Make a dictionary of districts files indexed by their numbers
districtsDictionary = makeDictionary(districts)

# Iterate through the clipping jobs
for p in polys:
    pFileName = os.path.split(p)[1]                   # In case there are underscores in the containing directory names
    idNumber = pFileName.split("_")[1]                # Get the digits after the "_".
    clippingFeatures = districtsDictionary[idNumber]  # Find the matching district.
    outName = os.path.splitext(p)[0] + "_clp.shp"     # Append "_clp"; assumes shapfiles, not gdb fc's.
    arcpy.Clip_analysis(p, clippingFeatures, outName) # Clip.
0

You create a list within the for line:

for fc in arcpy.ListFeatureClasses():

arcpy uses the workspace environment when return the list which you have changed. You have also used the same variable fc in both loops!

Try this:

arcpy.env.workspace = r'E:\SO\Polygons'
myList = arcpy.ListFeatureClasses()
for fc in MyList:
    end1 = str(fc[4:])
    arcpy.env.workspace = r'E:\SO\Districs'
    myList2 = arcpy.ListFeatureClasses()
    for fc2 in MyList2:
       # do clip
0

This is how I would do it. It's untested but should work. The code is iterating over the list of feature classes from both of your directories and looking for files found in both directories that have the same digits after the underscore:

import arcpy, os

clippeeDir = r'E:\SO\Polygons'
clipperDir =  r'E:\SO\Districs'
outDir = r'E:\SO\Voronio_file'

arcpy.env.worksapce = clippeeDir
for c1 in arcpy.ListFeatureClasses():
    print c1
    clippeeFile,clippeecCode = c1.split('_')
    arcpy.env.workspace = clipperDir
    for c2 in arcpy.ListFeatureClasses():
        clipperFile,clipperCode = c2.split('_')
        if clippeecCode == clipperCode:
            clippee = os.path.join(clippeeDir,clippeeFile)
            clipper = os.path.join(clipperDir,clipperFile)
            outFile = os.path.join(outDir,file+code+'_clip')
            arcpy.Clip_analysis(clippee,clipper,outFile)

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.