Python script to rename files based on a shapefile attribute — example application

I thought it might be helpful to post not only scripts that I’ve created, but why and how I use them. As written, these scripts tend to be fairly short and simple, but also very specific to some task I do repeatedly at work, or that I suspect I may have to redo multiple times with slight changes, and which would be tedious to do manually. In short, I thought an example might provide a helpful context for the scripts themselves.

This example uses the script from the post titled “Python script to rename files based on a shapefile attribute”.

At work, I deal with aerial photography. This imagery may come to us in tiles, a grid of geotiffs, for example. Sometimes customers desire that we deliver the imagery to them cut into a different tiling scheme in order to keep consistency with their imagery from previous years, or with some other data layers or boundaries. Sometimes, when compressing an area to .ecw or .sid we may need to break up an area into some arbitrary tiling scheme to balance file size with image quality. In those cases, I find the script from my post “Python script to rename files based on a shapefile attribute” to be useful. I could also see this as possibly be useful with, say, a NED dataset from the USGS with a metadata shapefile.

Perhaps in another post, I’ll go into more detail about how to create a tile index for the imagery — for now I’ll just say in brief that it can be accomplished in a few ways — it can be done using ER Mapper (create a mosaic using the Mosaic Wizard, then create a Mosaic Tile Index Grid using the mosaic) , Global Mapper (select layers in the Control Center, right click, and choose Create Area Features from Selected Layer Bounds), and I think with a little creativity there is some way to do it ArcMap 10 as well.

Anyway…here are some steps and screenshots of how I might use this script:

First, let’s start with a raster dataset–I’ll use imagery –and an index shapefile of some sort.

The attribute table currently has a field called “TIFF” that contains the number of the tiff that fills the extent of the corresponding feature. Now, I want to replace this incomplete, sequential, bottom left to top right numbering scheme with one that is more convenient for our customers using rows and columns. I begin by adding three fields: “TILE”, “Row”, and “Column”.

I use the Select Features tool to select all features in a row, then use the Field Calculator to populate the “Row” field with “A” for the selected features. Rinse and repeat until all records have a value for row and column. Manually selecting rows and columns can be a little unwieldy for a large number of rows and/or columns — I may try to write something to automate this step eventually, but so far I haven’t found it necessary.

Once I have the rows and columns done, I use the Field Calculator to fill the “TILE” column. In this example I simply use the row and column separated by an underscore.

Now, if I change the label field to “TILE” instead of “TIFF” for the index shapefile, the new naming scheme is displayed.

Now I just have to get the names of the tiffs to match! First, from the Attribute Table for the index, I export to a text file. The way I have the script set up, this table has to be in the same folder as the tiles to be renamed.

Now I open the script in IDLE, and make sure the path, text file, and the fields corresponding to the old name and the desired new name are set.

Run the script, and the files are renamed to match the “TILE” field in the index shapefile.

At this point I’d probably load the newly renamed tiffs into ArcMap along with the index and make sure everything is good, then delete the “TIFF” field from the shapefile and LookupTable.txt from the folder to avoid confusing the customer. And, we’re done!

2 thoughts on “Python script to rename files based on a shapefile attribute — example application

  1. Great post. I’m trying to do someting very similar and can’t quite get you script to work. I think it has to do with cleaning up the uneccessary characters. My table looks like this…

    2BMLTCCYOIMG_000010.jpg IMG_000010.jpg

    New name followed by a space then the old name. Any suggestions on how to reformat your script.

  2. So, you’re trying to add “2BMLTCCYO” to the front of a file named “IMG_000010.jpg” to get “2BMLTCCYOIMG_000010.jpg”, but you’re getting “2BMLTCCYOIMG_000010.jpg IMG_000010.jpg”? Interesting. I know this script is clumsy in how it strips away those characters, so that does seem a likely weak spot. I’m sure there is probably an easier way to do that step using a search cursor to read directly from the feature’s fields so that middle step of exporting to a text file is unnecessary.

    In the attribute table, the file names are showing correctly as they are and as you want them? If so, then somewhere it seems like it’s concatenating the desired name with the current one. Is there a space instead of a comma somewhere?

    Unfortunately, there’s no “undo” button, so you don’t want to get too crazy renaming your files unless you have copies, probably. I’d try commenting out the actual rename part of the script, and stick print statements in there after every step parsing the names or creating the new file name, and just see what’s going on. But if you’re working in ArcMap, take a look at the search cursor, you can probably have it read it directly from the shapefile. I think I have another script on here somewhere I turned into a script tool for renaming tiles by adding or removing prefixes and suffixes, and I think I used a cursor there in ArcGIS 9.3. It went from arcgisscripting to arcpy, and now search cursors are in arcpy.da I believe (?), so it might be a little dated.

Leave a Reply

Your email address will not be published.