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!