15.8.10

Creating the Python Geoprocessor

I was recently hired to refactor about 20 geoprocessing services written in python. At the beginning of each script was about 5 lines of boiler plate to instantiate the geoprocessing object (gp), check out the appropriate extensions, and set the necessary environmental variables. Each script began with more or less:


import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = 1
gp.CheckOutExtension("Spatial")
gp.cellSize = "0.0041666667"


Since this code was being repeated throughout all of the gp services, I simply turned it into a function in my helpers class. This removed about 100 lines of boiler plate and gave the ability to fine-tune the geoprocessor for all of the services. There are some additional debugging methods inside createGeoprocessor which also reside in the helpers class. I will do a future post on those basic error handling functions soon.


def createGeoprocessor(self):
    '''
    Returns a 9.3/9.3.1 Geoprocessor whose
    overwrite property is set to true
    and Spatial Analyst extension checked-out
    '''
    import arcgisscripting
    gp = arcgisscripting.create(9.3)
    gp.overwriteoutput = 1
    try:
        if(gp.CheckExtension("Spatial") == "Available"):
        gp.CheckOutExtension("Spatial")
    else:
        raise "LicenseError"
    except "LicenseError":
        self.toScreen(gp, Messages.SPATIAL_ANALYST_ERROR)
    except:
        self.reportGPErrors(gp)
        self.reportSysErrors(gp)
    return gp

No comments:

Post a Comment