Often times people write geoprocessing scripts that others try to incorporate in their work. This is done through modules or packages in Python. This is wonderful when one wishes to share their work, but it can also be bothersome if the module you are loading assumes that there is no geoprocessor loaded. This little script will help you safely load the geoprocessor object, either from an instantiation by the main program, or from scratch.
The method I use to load the geoprocessor, while verbose, ensures that if anyone loads the geoprocessor before they use any of my module functions, any of their settings remain intact. This is achieved as follows:
- Check if the arcgisscripting module was loaded.
- If it was not loaded, obviously the geoprocessor is not instantiated. Import, instantiate and return.
- If the arcgisscripting module was loaded, check the variables the user created in the main program.
- If a variable is found to be of type ‘geoprocessor object‘, then return that object.
- If no variable is found of type ‘geoprocessor object‘, create it and return it.
The code can be found below. Please review and use as you wish. If you have any comments or questions about it, please leave a comment.
"""
Loads the geoprocessor safely, ensuring that it is not loaded
twice (to avoid overwriting parameters).
Usage: load()
Returns: a geoprocessor object.
Note: Geoprocessor Version returned is 9.3
"""
import sys
def load(version=None):
# Check if the arcgisscripting module was loaded
if "arcgisscripting" not in sys.modules:
# The arcgisscripting module was never loaded Try to load it.
print "arcgisscripting not loaded"
print "Trying to load arcgisscripting"
try:
import arcgisscripting
except ImportError, errorDetail:
sys.exit("Error loading arcgisscriping. Is ArcGIS installed?\n" + errorDetail)
# Try to create a geoprocessor instantiation
print "Trying to create the geoprocessor instance"
try:
gp = arcgisscripting.create(9.3)
except:
sys.exit("Error creating geoprocessor object. Exiting\n" + str(sys.exc_info()))
# Return the geoprocessor instance
print "Done!"
return gp
else:
# arcgisscripting was loaded. Is the geoprocessor instantiated?
print "arcgisscripting was already loaded. Searching for geoprocessor..."
# Get the global variables
globalVars = vars(sys.modules["__main__"])
for item, value in globalVars.iteritems():
# Loop through global variables, ignore internals
if not item.startswith("__"):
# Check the type of the variable
if type(globalVars[item]).__name__ == 'geoprocessing object':
# If the variable is the geoprocessor, return it
print "Found geoprocessor instance as", item
return globalVars[item]
# If we are here, the module is loaded but no gp instantiated
# Try to create a geoprocessor instantiation
try:
print "arcgisscripting loaded but not instantiated. Instantiating now..."
gp = sys.modules["arcgisscripting"].create(9.3)
except:
sys.exit("Error creating geoprocessor object. Exiting\n" + str(sys.exc_info()))
# Return the geoprocessor instance
return gp
Related posts:

