2

So I've been assigned to clean up someone else's model.

Here's the situation. The model is supposed to iterate over a few hundred folders containing feature classes and convert the contents into shapefiles. The model fails if a file called "arc" does not exist in the folder, so I need to write some python to check if it is there, and if it does not, then skip the current iteration and continue onto the next folder.

My problem is that with the way modelbuilder is set up, there is no way to skip an iteration within an iterator tool. Any ideas? I'm halfway thinking of just rewriting the whole tool in python.

Here's a screenshot of the iterator

enter image description here

It has been suggested that I use calculate value to skip an iteration. The code should be fairly simple, however how do I use the true/false output to skip an iteration?

  • Go python! Model builder has limitations and a python tool can be used in a toolbox exactly the same as a model, except when a toolbox gets corrupted (yes, it happens) you've still got the script but all the other models are broken and inaccessible. If you're not brave enough to start from scratch you can export the model to a script as a starting point. – Michael Stimson May 19 '15 at 22:54
  • 1
    In model you can try help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/… and/or help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/… to check if that directory is exists. – SIslam May 20 '15 at 5:47
  • Would help if you had uploaded a screen shot of the model as I think what @SIslam is suggesting should work especially if you make the output a precondition to any downstream processing. – Hornbydd May 20 '15 at 14:19
  • @Hornbydd I posted a screenshot of the iterator where the error occurs, the code to check if a directory exists should be pretty simple, but where should I add the calc. value tool and how do I use its output to skip an iteration? – purelyp93 May 20 '15 at 16:28
1

I would've just commented but my rep is too low.

As Stimson has suggested, go Python. I would think that a simple if statement with the conversion process nested inside of it would work. So:

for fc in arcpy.ListFeatureClasses():
    if arcpy.Exists('arc'):
        SelectData_mb (in_dataelement, {out_dataelement})
        arcpy.CopyFeatures_management(#files to copy, outpath)
1

Ok folks, I figured it out.

First, the problem was not that some file called 'arc' was missing. The problem was that the select data tool would put an empty folder in the 'Output Values' list that would then cause problems in the Featureclass to Shapefile conversion tool as it was trying to convert null values. As was suggested throughout this thread, I coded a solution in python encapsulating both the data selection and conversion processes.

import arcpy, os, re
#the line below is a regex that will be used to iterate only through feature datasets marked as dempoints
r = re.compile(r".*\\dempoints")

#get directory holding feature classes and the directory where the shapefiles will be stored
inputDir = arcpy.GetParameterAsText(0)
outputDir = arcpy.GetParameterAsText(1)

#the files are stored recursively in my system so os.walk will iterate through everything
for path, dirs, files in os.walk(inputDir):
  holder = path
  #When os.walk comes across the feature dataset containing the feature classes, it will set ArcGis' workspace variable to be that folder.
  if r.match(holder):
    arcpy.env.workspace = holder
    for fc in arcpy.ListFeatureClasses():
        arcpy.FeatureClassToShapefile_conversion(fc, outputDir)

So in the end, my code just goes through everything and creates a shapefile on a file-by-file basis rather than storing them in a list structure and iterating through them later.

0

This is how I would do it in model builder:

Your sub-model takes a geodatabase and collects the contents into a list

The sub-model

The Master model calls the sub-model but this has a precondition on it which is the calculate tool. This has a tiny piece of python code to test if the input geodatabase has a feature class called arc. You could adapt this for folders.

Master model

The calculate value tool is set up as below, note how the GeoDatabase element of the model is passed into the isOK function using inline substitution:

Calculate Values tool

Alternatively you could write all this as python as suggested by others.

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.