In this post, we’ll go over how you can use a Python script to get ArcGIS to do something it doesn’t currently do.

This post comes courtesy of the suffering of a postgrad (like most things in the academic world.)   As part of a class project, the postgrad in question, Daniel, wanted to do something that, on the surface of it, sounded quite simple.  But it ended up being a task that ArcGIS wasn’t set up to do.  Without going into too much detail, he was doing some spatial analysis to identify potential landslide areas in the Manawatu-Wanganui region.

landslide

Part of that involved calculating a simple ratio of the area of each particular landslide to the total area of landslides for the region.  Sounds simple, eh?  Something that should easily be done as a table calculation?  In theory, yes, but to do this, there has to be an attribute in the table that holds the total area of all the landslides but what he had was only the area covered by each individual landslide.  Here’s a similar table (landslides from Northland courtesy of GNS):

Data

Note how there’s an attribute for area (in square meters) for each landslide.  So what Daniel needed to do was to sum up all these areas, add a new attribute, and then add in the total landslide area into every record.  Then he could do a simple field calculation to get the ratio of each landslide area to the total area.  Again, sounds simple, but not that easy to do in reality; especially as he was wanting to do this as part of an automated ModelBuilder model that would save him the trouble of doing multiple runs of the same analysis on different input layers.  So Daniel, being an industrious lad, turned to the internet and, as is often the case, found someone who had a similar problem.  That person ended up writing a little program, using the Python scripting language, that does just that.  This takes us into higher end use of ArcGIS; when it isn’t set up to do what you need it to do, you can write a script that will make it jump through your spatial hoops.  It’s akin to getting an Excel macro, or a VB script, though ArcGIS has a major preference for Python when writing scripts so there’s a lot of built in functionality and support.  Anyway, here are the gory details of this particular script which he copied and pasted straight out of his browser:

Script1

Short and (ahem) simple from a Python point of view.  To dissect this script (but not too much), it adds a field called “sum_Shape” to an existing table, and then searches through the table to sum up all the area values and add that sum to the new field.  When the script runs, it will ask the user for specific details in a pop up window, such as the name of the table (known as “intable” to the script), the specific field with the landslide areas (“field”), the summed value (“output”) and the name of the new field.

So, all well and good, but what can you do with this?  How can you turn it in to something useful when you’ve got to have your analysis done this afternoon?  As Daniel discovered, you can attach this script to a tool and then run it whenever you need it.  There are a few steps involved, but here’s the general how-to:

  • Create a new toolbox:  There are a variety of ways you can do this, from ArcToolbox, or from the Catalog window (usually at the right of your screen.)  Open up ArcCatalog and then ArcToolbox.  Then right-click on the “ArcCatalog” label at the top level and select “Add Toolbox”
  • Navigate to where you want the Toolbox to live an click the New Toolbox button:

NewToolbox

  • Give it a new name and you’ll see it added to the tree of Toolboxes.  Here I’ve called it “MyTools”

NewInArcToolbox

  • Next, right-click on the new toolbox and go to Add > Script…
  • You’ll need to give the tool a name (no spaces) and a label which will be it’s display name (can have spaces) in ArcCatalog.  It’s usually a good idea (in general) to store relative path names.  Click Next.

AddScript1

  • In the next window, point the tool towards the script (it must end in .py or it won’t be recognised.)  Tick the “Run Python script in process”, then Next

AddScript2

  • In the next Window, any parameters that the script needs can be set, but let’s not worry about that for now.  Click Finish and you should have a new tool in the toolbox.

NewTool

Now that you have a new tool connected to the script, it can be executed like any other tool by double-clicking on it.  Additionally, the tool can be used in a ModelBuilder model which is exactly what Daniel did:

DanielModel

The “Sum_Add_Field” tool is the tool that he created by pointing it to the script he copied from the web.  This is one line from a much bigger overall model.  You can see, for instance, that this is the sixth time he’s used his new tool in the overall model.

It should be said that the script could also be run directly from inside ArcMap using the Python window.  It should also be said that there’s probably a way that this could be done as a field calculation and some Python script, but the benefits of having done it this way are that 1) he now has a reusable script that he can use at another time and, 2) it’s tied to a tool so can be used with ModelBuilder, and therefore also be a part of an automated process.

Once you start looking for them, there is a plethora of scripts (and I’m struggling to know if plethora is singular or plural…) available via some judicious web searches.  The not-faint-of-heart amongst us might be interested to note that, with a bit of background, anyone can write their own scripts (and post them on the internet for all to see/use) but again, that’s taking us to higher level end use of ArcGIS.  Once you know what you’re doing though, it’s a powerful way to drive your spatial analysis and data management.

C