Geography 481 Intro to GIS
Project Ten: Application Customization


Introduction

Application programming and customization is used to combine a sequence of GIS program commands into one integrated procedure, to modify the interface or to create complete applications.

The goal of this project is to provide you with a first-hand look at how GIS tasks can be executed through programming. You will write a simple geoprocessing program, called a script, using Python programming language and you will run the geoprocessing operation without even launching ArcMap.

But first we will briefly explore the Python window in ArcMap.


Setup

Launch ArcMap


Using Python

Congratulations, you have executed the traditional "first program" that almost every beginner starts with.

Now,

I know that wasn't especially exciting, but a necessary right of passage. Moving on...

 

Copy the Project 4 files from Data on Geogsrv\Geog481-Carroll\Proj4\ into c:\temp\. Yes, Proj4. It is good to recycle. This was the flood hazard / overlay exercise. Make sure the files are extracted from the zip archive.

Open Notepad

Type the following lines in Notepad

import arcpy
print "fun"
arcpy.env.workspace = "c:/temp"
infc = "floods.shp"
clipfc = "oc.shp"
outfc = "ClippedFloods.shp"
arcpy.Clip_analysis(infc, clipfc, outfc)

Python is case sensitive so make sure you type these lines exactly as they appear.

Save your file as c:\temp\clipscript.py

Take a look at the script you typed and see if you can figure out what each line does.

Close Notepad

In the Windows File Explorer, browse to c:\temp and double-click clipscript.py

It will take a few seconds (about as long as it takes the Clip tool to run in ArcMap), but you should eventually see a new shapefile appear in c:\temp

Open ArcMap and take a look.


That's all there is to it. You have just created and executed your first ArcGIS script... and you didn't even have to open ArcMap to do it.

 

Feel the power!


Using ModelBuilder

Coding not your thing? There is another way to automate your GIS work using the visual programming capability of ModelBuilder.

Open the Proj4.mxd in ArcMap. It should look familiar.

You have now set the Geoprocessing environment to read and write from the C:\temp directory.

Models are a type of tool and need to reside in a toolbox. Time to start building the model.

When you added the layers, the ovals were automatically given a blue fill and the layer name was assigned. However, when the Clip tool was added, its rectangular symbol remained hollow because the tool's parameters have not been specified yet. If you recall, the Clip tool required an input layer(s) and a clip layer. In addition, the Clip tool produces an output feature class or shapefile, so this data variable is automatically added to the model, even though it is not pointing to an output feature class or shapefile yet.

We will use the Connect tool to connect the Floodhaz and County layers to the Clip tool

Once you have set the parameters, the Clip box should fill with yellow and the output oval should be green and assigned the name FLOODS_clip.shp

It is good practice to regularly use the Auto Layout button to organize the model elements.

If you recall from Exercise 4, we used the clipped floodhaz layer and the Cities layer in our overlays. We will do the same here. Let's start with an Intersect.

Resize the Model window so you have some space to the right and below your current elements.

Notice the tool and output variable are both hollow (no color fill). We need some connectors.

Take a look at your model and see if it makes sense

Let's see if it works.

As each tool runs the tool rectangle will turn red and a dialog box will appear showing progress.

When the model is complete, click Close in the progress window.

By default, Model Builder does not automatically add output layers to ArcMap. They are assumed to be intermediate data.

Take a look in C:\temp to see if the intersect overlay was created. You should find two new a shapefiles named:

Try adding the Union overlay to your model.

You will need to add the appropriate connectors and don't forget to click the Auto Layout button when you are done.

When you run the model again, only the new parts will execute.

Feel the power!


Last modified: 11/30/2021