Evolution of a Python Script into a Script Tool: Part 1

The script I’ll be using in this 3-part series of posts is the latest iteration of the script in my last post. It goes back a few posts, and some may find it informative to trace it back to my effective, if not so user-friendly, initial version which required me to export a text file and so on. Some may find it informative; however, others may find it tedious. So let’s pretend you don’t feel like going back through those, and let’s start fresh. I will issue a little disclaimer–the script you see below did not emerge fully formed, but went through several steps before getting here, including bouncing back and forth through the tool validation and attempts to run the tool in ArcMap and running chunks of it in IDLE…

So here’s the script I said you’d see below, but I think maybe I should go back further. All the way back. I’ll do so after this little image:

If you’ve read the earlier posts, skip to the next paragraph. For those who haven’t, here’s the abstract: Where I work, we deal with imagery that is often tiled into a grid of tiffs, sids, or ecws. We have (or we create) an index shapefile that shows where each tile is in the grid. Sometimes I have wanted to rename the tiles for a variety of reasons. I figured that just as a field in the attribute table contains the current name of each tile, I could add a field and populate the names I want them to have. Then, using a Python script, I could read the values in these two fields for each record, and rename the files in a given folder from the existing name to the desired one.

So, the bare bones version of this script is in my last post. Basically, it takes four parameters: the index shapefile, the field name corresponding to the current file names, the field corresponding to the desired one, and the folder containing the files. Then the script creates a search cursor, and then goes through the records getting the values from each of the designated fields, and renames the files from the current name to the desired one.

Easy peasy, right? It does require the fields to have exactly what you want the names to be. Now, sometimes there’s a file extension present in the attribute table, sometimes not, so I would either have to customize the script by tacking on ‘+”.tif”‘ where the old and new names are, or simply add them into the attribute table with the Field Calculator. Not much trouble, but I thought if I make it a script tool, I could maybe give some options for dealing with those file extensions, and also with prefixes like “2011_UTM_LosAngeles_”, or like “M:\11_UTM_LA_tiffs\”, that sometimes precede the actual simple name of the tile. So I added a couple fields to my test tile index so I could play around with this a bit:

Now, before I really start fleshing out the bare bones, I want to make sure the bare bones work as a script tool, right? So I right-click in the ArcToolbox window, add my custom toolbox, and then right-click the toolbox and add a script. I give it a name, a label, and a short description, and I like to check “relative paths”. I hit “Next”, and navigate to my script. I hit “Next”, and then here’s where it can start to get a little complicated, depending on the script and the parameters. Keeping the script open in IDLE for reference, I fill in the parameters. I give them the following Display Names: “Index Shapefile”, “Field Containing Old Name”, “Field Containing New Name”, and “Workspace”. I set the data type for the first of these to “Feature Class”. For the next two, I choose “Field”, and for the last I choose “Workspace”. So far, so good. I highlight “Field Containing Old Name”, and in the lower Parameter Properties window, I scroll down to “Obtained from” and choose “Index Shapefile”. Furthermore, since I want to use text strings, I click next to “Filter”, choose “Field”, and then uncheck all but the “Text” box. I repeat this for “Field Containing New Name”, hit “Finish”, and now the script tool appears in my toolbox.

When I double-click, it opens up a dialog box just like any other ArcMap tool. I fill in the fields:

Hit go, and after a few seconds I can see in the ArcCatalog window that my files have been renamed! Yay!

In Part 2, I’ll define a function to modify the names, test it in IDLE, incorporate it into the script, add the necessary parameters, and add a simple little line to the tool validation tab.

One thought on “Evolution of a Python Script into a Script Tool: Part 1

  1. If anyone actually gets this far and says, “Hey, where’s Part 2?”, well, sorry — never got to it. I’ll try, but I’m just so dang busy at the moment! Kids! Internship! Job! School!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>