Rinse and Repeat! Working with Modelbuilder Iterators


 

Modelbuilder is often an afterthought to most GIS analysts, however, it can be a powerful tool when it comes to building and replicating complex workflows, as well as automating those boring, tedious tasks.

Iterators are unique to models, and allow you to loop through a process on unique values, tables, layers in your map document, or even workspaces.

Let’s have a look at iterators, and how you can use them.

Iterators can be enabled through the context menu within the model itself. You have a few options available, as can be seen in the image below. For the moment, you can only use one iterator per model (more on this later).

Interators_ContextMenu

You can read about the individual capabilities of each here:
http://resources.arcgis.com/en/help/main/10.2/index.html#//00400000000n000000

The best way to understand this is to look at a simple scenario. Perhaps I have a dataset that I want to filter based on a unique value. For each unique value that I specify (in an excel table, for example), I want to select and export to a new feature class. In order to do this, I will use the Field Values Iterator.

Iterate
I can modify the options by double-clicking on the hexagon. I choose and Excel worksheet (PestSpeciesList$), and my “COM_NAME” field has all the unique values. I can also choose to skip null values or specify a value to ignore.

spreadsheet

Once I’ve done this, the iterator now has all the information to loop through the dataset and select features based on the unique values in my spreadsheet.fulliterator

Every time it loops through, it parses that unique value to a variable I can now use in other geoprocessing tools, as an In-Line Variable.

In-line variables are great because they allow you to automatically populate SQL queries, or automatically name file names based on the unique value chosen. All I need to do is input %Value% wherever I want the variable to be used. Remember that “Value” can be any name you give it, but it is the output of the iterator (green oval in image above).

In this case, I want to export those selected features to a new feature class, so I’m going to use the Feature Class to Feature Class tool. Any parameter that I would have had to individually specify, can now use the in-line variable.inlinevariables

 

 

Let’s recap – I started with a spreadsheet with 3 unique values. The iterator takes each of the three values using each entry under “COM_NAME”, and uses that unique value (%Value%) to select features and export to automatically named feature classes (“PestSurvey_Pricklyacacia”, “PestSurvey_Parthenium”, “PestSurvey_Lantana”).

Now this is a very simple example. I can build my complex workflows using modelbuilder, and rather than having to run the workflow repetitively, I can use an iterator as a loop. This is a really good resource for people like me, who don’t like to write scripts. If you need more than one iterator, you can employ nested models, but that is a whole other blog entry.

So go ahead and use those iterators, run that model, and grab a coffee while modelbuilder does the hard work for you.

Happy Easter!

Ebony

6 thoughts on “Rinse and Repeat! Working with Modelbuilder Iterators

  1. Beth

    Great blog post! Do you happen to have an example of using the While Iterator? I can’t seem to find any good demos/examples.

    Reply
    1. Ebony W Post author

      I think I’ll have to come up with an example – the help documentation just seems to give the parameters. Do you have a specific scenario in mind?

      Reply
      1. Beth

        That’s what I was finding, too.

        My specific scenario is that I sometimes get watershed polygons that I want to clean up- they should all be of a somewhat similar size, but what ends up happening is that, invariably, there are always tiny ones (some just one pixel from the raster that was used to create them).

        I’d like to be able to enter a numerical parameter value (area) to select all the little ones, then use the eliminate tool to incorporate them into their neighboring largest polygon.

        The only problem is- if there’s a group of little guys next to each other, I need to run though the process several times before they’ve all been eliminated. And of course how many times I have to run through it is dependent on the dataset, so I can’t just use a For Iterator, which at least I can find several examples of to work from (I’m new to iteration!).

        Thanks in advance for your help!

  2. Alizée Vanderveken

    I have a file showing for each disaster (one line = one disaster), which provinces were affected. The number of provinces affected varies and their names or associated code varies as well.

    Example, if a flood in 2000 affected 3 provinces called Sichuan, Henan and Yunnan. I need to select those 3 provinces from a shapefile that comprises all provinces of the world and create a new shapefile. Easy. But I need to do that for over 2000 disasters and the affected provinces always changes (in names and numbers of provinces affected).

    I would like to automate this with ModelBuilder but I don’t know how to change my SQL request for each disaster.

    Example, it will be “PROV_NAME”=”Sichuan” OR “Henan” OR “Yunnan”. But for another disaster, it might affect 10 provinces of different names.

    I would like to automatically generate a SQL query from an Excel file that shows in each line, a specific disaster, and in each column (same line), I would write the name of the affected provinces, and do the same for each disaster (so each line), but sometimes I would have 3 columns (for 3 affected provinces), sometimes 10 columns …

    Then if I have for each disaster, the required SQL query, I would like to find a way in ModelBuilder to do it automaticaly, one SQL request at a time, and create the corresponding shapefile with the 3 affected provinces and so on for the other disasters.

    Can you help me? Thank you!

    Reply
  3. Alizée Vanderveken

    Sorry, I don’t know if my previous message went trough:
    I summarized it:

    I have a file showing for each disaster, which provinces were affected. The number of provinces affected varies and their names or associated code varies as well.

    Example, if a flood in 2000 affected 3 provinces called Sichuan, Henan and Yunnan. I need to select those 3 provinces and create a new shapefile. Easy. But I need to do that for over 2000 disasters and the affected provinces always changes (in names and numbers of provinces affected).

    I would like to automate this with ModelBuilder but I don’t know how to change my SQL request for each disaster.

    Do you know if there is a way for ModelBuilder to see in an Excel file, which are the provinces that need to be selected and select them and create the corresponding shapefile?

    Thank you

    Reply

Got something to say?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s