ArcGIS Model Builder: How to Create a Custom Toolbox and Export as a Python Script

How to use model builder and create custom toolboxes in ArcGIS

Model Builder in ArcGIS - Python Script
This example explains how to use model builder in ArcGIS. It includes a demonstration how to create custom toolboxes and export models as Python scripts.

Last Updated: Apr 21, 2018

What is Model Builder in ArcGIS Desktop and ArcGIS Pro?

Model Builder in ArcGIS automates your GIS workflows. For example, if you run a report every week that buffers, intersects and generates a pivot table… then you can create a single model and run it every week.

Instead of running each tool one-by-one, you create a model that takes your input, transforms it with a series of tools and then outputs it. In other words, model builder connects your input to geoprocessing tools and transforms it to your desired output.

The best part?

When you create a model in model builder, you can run it as part of your daily workflow. For example, you can reuse models and share them for other users to interact with.

Follow along step-by-step below with your own data and tools. Of course, if you have any questions, don’t be afraid to ask in the comment box below.

Model Builder Example – How To Set Up Your First Model

Let’s start with this simple example for how to create a model in model builder. Pretend you work for a company that plans out where pipelines will be built. In order to comply with environmental regulations, the pipeline can’t be 500 meters away from endangered bird nests.




As engineers modify the design of the pipeline often, they would like a spreadsheet every time they change the alignment of the pipeline. In this report, they want to know if the pipeline is within 500 meters of any bird nests.

A model usually consists of at least three elements:

  • Input Data (blue squares)
  • Geoprocessing Tools (yellow circles)
  • Output Data (green squares)

In our model builder example, here are the two inputs:

Input 1. Pipeline alignment (red and black-dashed line)
Input 2. Endangered bird nests

And here’s how these inputs look like on a map:

Model Builder - Pipeline and Bird Nests

Tool 1. Buffer pipeline 500 meters
Tool 2. Intersect bird nests with pipeline buffer
Tool 3. Generate report as spreadsheet

Output 1. Buffer, Bird Nests in Buffer, Report

Let’s start with this simple step-by-step example how to build a model in Model Builder.

Step 1 Open Model Builder

First, click the Model Builder icon to start creating a model. This will open the Model Builder window for you to start adding your geoprocessing tools.

Model Builder Icon

Step 2 Add tools to Model Builder

Drag and drop the buffer, intersect tool and Excel spreadsheet conversion tools from ArcToolbox.

Model Builder Geoprocessing Tools

Step 3 Enter tool parameters

Double-click each tool (rectangle) and enter in the following inputs and outputs:

BUFFER:
Input Features: Pipeline Realignment
Output Feature Class: C:\Temp\Pipeline_Buffer.shp (Make sure this directory exists or create a new folder if necessary)
Linear Distance: 500 meters

INTERSECT:
Input Features: Bird Nest and Pipeline_Buffer.shp
Output Feature Class: C:\Temp\BirdNest_Intersect.shp

TABLE TO EXCEL:
Input Table: BirdNest_Intersect.shp
Output Excel File: C:\Temp\Report.xls

After entering the inputs and outputs for each tool, the Model Builder window will look like the screenshot below. However, it looks disorganized and the auto-layout tool can help clean everything up.

Model Builder Auto Layout

Step 4 Auto layout

After you click the auto-layout button, the geoprocessing tools, inputs and outputs will rearrange themselves neatly in the model builder window.

Model Builder Final Model

Model Builder Example: Running the Model

Of course, we have only built the model up to this point. In order to create the report, we still have to run the model. You can run the model by clicking the ‘Play’ button.

Model Builder Run Model

After you run your model, you can add your output feature classes to ArcMap to verify it is correct. By double-clicking the report.xls, it lists all bird nests within 500-meters of the pipeline. In this case, it’s only the California Condor.

Model Builder Results

Another thing to note is that after you run your model, circles and squares in the model will have shadows. Check (C:\Temp\) to see each product it creates (buffer, intersect and report).

But really, the only output you need is just the report. The buffer and intersect are just temporary (intermediate) data sets. After running a model from the ModelBuilder window, it doesn’t delete all the intermediate outputs (buffer and intersect) that it creates.

So if you want to re-run the model, you can manually go into this folder (“C:\Temp\”) and delete these feature classes. Also, you may have to click the “checkmark” icon next to the “auto-layout” icon to validate the model.

Model Builder Toolbox: Setting up Parameters/Variables

Let’s say another group in your company wants to use this model for the same purpose. But their input data and output data have completely different names.

Instead of static input and output, you need them to be dynamic as parameters. When you set the input data or output data as parameters, users can enter their own data and set their own output paths.

How do you set up model builder parameters?

Step 1 Set model parameters for inputs

Right-click your two input data sets (bird nests and pipeline) and click ‘Model Parameter’

Step 2 Set model parameter for output

Right-click your output data set (report) and click ‘Model Parameter’

Model Builder - Model Parameter

But there’s one more thing that we’ll have to change – how it handles intermediate data. ArcGIS Model Builder has a handy command called ‘in_memory’ to handle it.

For each temporary output (buffer and intersect), we can turn these into temporary datasets with ‘in_memory’. For example, double-click the green box (pipeline_buffer) and change it as follows:

Model Builder - InMemory

After this step, your model should have “P” next to each model parameter. Also, your temporary buffer and intersect output should have ‘in_memory’ for the output.

Model Builder - Parameters Variables

Delete all the files in (C:\Temp\). Now, when you click ‘Run Model’, it will only create the XLS report. And this is because you set the buffer and intersect as temporary data (in_memory).

How to Create a Toolbox in ArcGIS

Now that we’ve set the input and output as parameters, wouldn’t it be nice if this could be a toolbox on its own? Toolboxes are convenient to share. They look professional. And they are easy to create.

First, let’s empty the parameters so that when we share it, there isn’t a default path. Double-click each model parameter in model builder and empty it.

Model Builder - Empty Parameter

Once you’ve emptied each parameter, the model should look white like in the screenshot below:

Model Builder - Save Model

Now, you’ll want to save your model. But you’re going to need a toolbox to do so. After clicking “Save”, click the new toolbox icon in the top-right of the dialog box.

Model Builder - New Toolbox

You can name the new toolbox “Pipeline.tbx”. Now, double-click this new toolbox and save the model within the toolbox.

In ArcCatalog, locate where you just saved this toolbox. Expand the toolbox so you can see your new toolset.

Model Builder - Pipeline Toolbox

Double-click the new toolset that you have created. Now, that we have created parameters and emptied them, you can now dynamically enter in your inputs and outputs.

Model Builder - Pipeline Report Tool

How To Export a Model as a Python Script




Since ArcGIS 9.0, it has completely overhauled its programming interface from VBA to Python. Now that Python has been proven to be a great tool, where do you begin? Sure, you can start with python.org but this is a lot of information for you to start with.

Another way to save you time is to use ModelBuilder and create a model for your workflow. Once you get the model working, then export the model as a script (Model > Export > To Python Script). The output of this export provides a great starting point where you can alter the Python code and even include other Python packages.

Model Builder Export As Python Script

This is probably one of the best ways for you to begin learning Python scripting in ArcGIS. You can see how ModelBuilder breaks up the code with arcpy modules, script arguments, local variables and the geoprocessing tools.

These are the same code samples you see at the bottom of the ArcGIS Help Library. Each geoprocessing tool has a code sample to use in the Python Window or as a stand-alone script for that particular tool.

Model Builder Iterators

Iterators help repeat a process for a set of inputs in model builder. ArcGIS Model Builder has different types of iterators to repeat processes.

For example, if you want to set multiple distances for buffers at different increments, you can use the ‘FOR’ iterator. Using the buffer tool from our existing model, you can set ‘FROM’ and ‘TO’ variables at different increments.

Model Builder - For Iterator

In the Model Builder window above, we added an iterator to the model. This will dynamically set the buffer distance as described in the ‘FOR’ iterator.

Other iterators available are: ‘while’, ‘row selection’, ‘feature selection’, ‘multivalue’ and more.

Model Builder in ArcGIS Desktop

Because at your job, time is of the essence. You need to provide answers to real-life GIS problems at break-neck speeds.

Model Builder in ArcGIS automates your GIS workflows. When you create a model, it can repeatedly perform your analysis.

Instead of manually running each tool one at a time, try putting them in a model next time.

Model Builder is a robust way to analyze your GIS data and is a huge time saver.

3 Comments

Leave a Reply

Your email address will not be published.


*