Select to view content in your preferred language

Using Recursive Iterator outputs as Inputs to Tools

2898
4
Jump to solution
07-11-2012 03:36 PM
BrianDudek
Regular Contributor
I couldnt get two iterators to work through modelbuilder, so I am trying Python.

Being a total newbie to Python, I was able to set up a loop that gets each shapefile in multiple directories using a wildcard (it is printing each shapefile with print command), but I don't know where to go from here.  I want to be able to use each shapefile to be used as an input feature.  And then going to do the same thing for other shapefiles based on a different wildcard and for the second tool input (e.g clip, erase, identify feature).  How will I go about passing along each file during the loop for the input feature and the other feature at concurrent times? eg. first returned shapefile in one loop will be input feature and first returned shapefile in second loop will be..say an erase feature

Anyways, for a first step, I was just testing the Buffer tool to see if one recursive loop would work, but it doesn't like file as the input parameter. Clearly that must be the wrong parameter.  What do I need to do here?  

import arcpy, os, fnmatch #for wildcards from arcpy import env  rootDir = r"FILEPATH_IS_HERE"  for dirPath, dirNames, fileNames in os.walk(rootDir):#, topdown=False):     for file in fileNames:       if fnmatch.fnmatch(file, "*100110.shp")== True:             print os.path.join(dirPath, file)             env.workspace = os.path.join(dirPath, file)             arcpy.Buffer_analysis(file, r"FILEPATHGOESHERE", 1000)    else:     print "No other matching 100110's"


Thanks for any help.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SolomonPulapkura
Frequent Contributor
Not sure if I understand you right but here is something you can try.
You will have to first create "outputFolder" either programmatically or manually.

import arcpy, os, fnmatch #for wildcards from arcpy import env  rootDir = r"FILEPATH_IS_HERE" ourDirName = "outputFolder" buffShape = "outputBuffer.shp"  for dirPath, dirNames, fileNames in os.walk(rootDir):#, topdown=False):     for file in fileNames:       if fnmatch.fnmatch(file, "*100110.shp")== True:             print os.path.join(dirPath, file)             env.workspace = os.path.join(dirPath, file)             arcpy.Buffer_analysis(file, rootDir + os.sep + outDirName + os.sep + buffShape, 1000)    else:     print "No other matching 100110's"

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Esri Alum
What's the error message it's returning? Have you tried it without the workspace tweaking?

for dirPath, dirNames, fileNames in os.walk(rootDir):#, topdown=False):
    for file in fileNames:
      if fnmatch.fnmatch(file, "*100110.shp"):
            shpname = os.path.join(dirPath, file)
            arcpy.Buffer_analysis(shpname, r"FILEPATHGOESHERE", 1000)
0 Kudos
BrianDudek
Regular Contributor
What's the error message it's returning? Have you tried it without the workspace tweaking?



Thanks.  I actually just had to remove file as a parameter from the env.workspace line.  So I got that settled now.

Curious how I would go about exporting the output files to a lower level directory (with a specific name-have to use another wildcard I presume) from where the input file exists.   Right now it exports the output feature in the directory of the input feature.  http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//004000000008000000.htm  This link indicates to use os.path.dirname(input), but how would I set it up with the existing filename loop I already have?
0 Kudos
SolomonPulapkura
Frequent Contributor
Not sure if I understand you right but here is something you can try.
You will have to first create "outputFolder" either programmatically or manually.

import arcpy, os, fnmatch #for wildcards from arcpy import env  rootDir = r"FILEPATH_IS_HERE" ourDirName = "outputFolder" buffShape = "outputBuffer.shp"  for dirPath, dirNames, fileNames in os.walk(rootDir):#, topdown=False):     for file in fileNames:       if fnmatch.fnmatch(file, "*100110.shp")== True:             print os.path.join(dirPath, file)             env.workspace = os.path.join(dirPath, file)             arcpy.Buffer_analysis(file, rootDir + os.sep + outDirName + os.sep + buffShape, 1000)    else:     print "No other matching 100110's"
0 Kudos
BrianDudek
Regular Contributor
Thank you it was os.sep that I was looking for. 🙂


Also can anyone give me an indication why "Parameters are not valid" on the line of arcpy.merge_managment(vt, featureOut)?

Just trying to do a merge of feature classes picked up from multiple directories with certain characters in the filename.  Clearly something is wrong with the list.

import arcpy, os, fnmatch #fnmatch for wildcards
from arcpy import env

rootDir = XXXXX
vt = arcpy.CreateObject("ValueTable")

for dirPath, dirNames, fileNames in os.walk(rootDir):
    for file in fileNames:
        if fnmatch.fnmatch(file, "*AddedNew*.shp")== True: #iterates for files with AddedNew in name
            env.workspace = os.path.join(dirPath)  #tells where to look for input feature
            shps = arcpy.ListFeatureClasses("*", "Polygon")
            print shps
            for shp in shps:
                vt.addRow(shp)
            featureOut = os.path.dirname(dirPath) + "Statewide_NewCoverage" # Finds area of beginning iteratiom + Name of outputfeatureclass
            arcpy.Merge_management(vt,featureOut)
else:
    print "Merge New Coverage files has failed"


EDIT: I got it working using another example without using arcpy.ListFeatureClasses, but by just populating filepaths into a [] with the append tool.  Then I feed that into the merge_management.  However, the problem is that it exports a shapefile for each merger.  For instance, it exports first file by itself, than the second shapefile merged with the first, then the third shapefile merged with first and second.
0 Kudos