Leave a comment

Good Geoprocessing practices


Something that I learned as good practice is working with copies of my shapefiles and not the originals.  When doing this you do create several temporary tables and/or shapefiles and these need to be cleaned up.


## System arguments
 # Input Streets shapefile
 Streets = sys.argv[1]
 # Output Streets shapefile
 Streets_output = sys.argv[2]
 # Output Streets shapefile
 Streets_output2 = sys.argv[3]
 # facility streets layer
 facilstr = sys.argv[5]

# Set the workspace for the processing files
 gp.workspace = os.path.dirname(sys.argv[2])

# Make copies so that processing doesn't take place on the original.
 gp.AddMessage("\nCreating processing files...")
 gp.Copy_management(Streets, "Streets_process.shp")
 gp.Copy_management(facilstr, "facilstreets.shp")
 gp.AddMessage ("Copies created")

This creates copies of the input shapefiles which I then use for processing.  Because there are these temp files, in the end of the script I need to delete those temp files.  For this you need to import the “os” and “glob” functions such as import os, glob in the beginning of the script.  Then at the end of the script we use these functions to clean up the temp files.  I also cleanup the xml files that are created when working with temporary tables.


gp.AddMessage("Cleanup...")
 # Delete "process" files

for file in glob.glob(gp.workspace+"/Streets_process*"):
 os.remove(file)
 for file in glob.glob(gp.workspace+"/facilstreets*"):
 os.remove(file)
 for file in glob.glob(gp.workspace+"/*.xml"):
 os.remove(file)
 gp.AddMessage("\nDone.\n")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: