Friday, August 20, 2010

Python Tip: Iterating through a feature class

After you have mastered creating Python scripts to perform geoprocessing tasks on single feature classes, you will probably realize that you will often come across cases where you will want to perform the same tasks on all the features in a feature class, such as adding a field, adding a prefix or suffix to the name, etc.  Accomplishing this is very easy.  You ill need to iterate through a feature class using the ListFeatureClasses function.  I will give you an example that capitalizes the names of the feature classes in a feature class.  Start by importing the proper modules:

import sys, string, os, os.path, arcgisscripting

Next add the toolbox with the Rename tool:

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

Set the workspace to the feature class you wish to edit:

gp.workspace = "Database Connections\\GIS.sde\\Data"

Call the ListFeatureClasses function and move to the first feature class:

fcList = gp.ListFeatureClasses ("*", "all")
fc = fcList.Next()

Now loop through and rename each feature class:

while fc <> None:
     gp.Rename_management(gp.workspace + "\\" + fc, 
                          gp.workspace + "\\" + fc.upper() \
                          + "_temp", "FeatureClass")
     gp.Rename_management(gp.workspace + "\\" + fc.upper() \
                          + "_temp",  
                          gp.workspace + "\\" + fc.upper(),
                          "FeatureClass") 

     fc = fcList.Next()

Because ArcGIS is not case-sensitive, we had to temporarily rename the feature classes with "_temp" at the end, and then rename them again and capitalize them.  Also, remember to call the Next function to move to the next record or you will be stuck in an infinite loop.

In this case, we ran through every feature class but using the wild card parameter, you can select a subset of the feature classes.  Other similar functions exist that are also useful such as ListDatasets and ListTables.  I recommend that you read more about these functions here.

Friday, August 6, 2010

3 Quick Python Tips to Make Geoprocessing Easier


1. Ignoring escape characters
 
When you export a Python script from Model Builder, you have probably noticed that the “\” in the directory paths are modified in two ways, either “/” or “\\”.  This is because “\” is an escape character.  Basically, “\” tells Python to look at the following character and do something special.  For example “\n” tells Python to start a new line.  However, if you are like me, you usually copy and paste a file path and it is a hassle to have to change every occurrence of “\”.  Luckily there is away to tell Python to ignore the “\” in a string as an escape character.  You can do this by placing an “r” directly in front of the string.  So here are three ways to create the same string:
 
r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes"
"C:/Program Files/ArcGIS/ArcToolbox/Toolboxes"
"C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes"


2. Getting the directory location of the current script

When creating batch scripts that you will reuse frequently for files in various locations, it can be time consuming to modify the file directory before running the script every time.  One method of making this step easier is by placing the script in the same directory as the files you want to process.  To do this, you can have the script use the directory it is located in using the getcwd function.  This function returns the directory that the script is located in.  First import the os module and then call the getcwd function:

import os

directory = os.getcwd()


3. Create a data stamp

When processing file, you may want to create a date stamp on the end of the file name or write the date to a txt file.  Creating a date stamp is fairly easy First import the time module, then call the localtime function to return the current date and time on the system.  This example returns a string in the “MM_DD_YYYY format:

import time

t = time.localtime()

date = str(t[1]) + "_" + str(t[2]) + "_" + str(t[0])