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:

  1. Check if the arcgisscripting module was loaded.
    1. If it was not loaded, obviously the geoprocessor is not instantiated. Import, instantiate and return.
  2. If the arcgisscripting module was loaded, check the variables the user created in the main program.
    1. If a variable is found to be of type ‘geoprocessor object‘, then return that object.
  3. 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
 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3.00 out of 5)
Loading ... Loading ...

Related posts:

  1. Understanding the Geoprocessor Programming Model part 1
  2. Understanding the Geoprocessor Programming Model part 3
  3. 7 wishes for the new Geoprocessor
  4. Accessing Geometries using the Geoprocessor (Updated)
  5. Understanding the Geoprocessor Programming Model part 2

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2010 Michalis Avraam Suffusion theme by Sayontan Sinha