Home arrow GIS Data & Resources arrow Scripts and Code arrow Python arrow PYTHON! call an external GIS workflow
PYTHON! call an external GIS workflow PDF Print E-mail

Written by Kevin Bell,

Here are two python scripts that talk to each other.  The first script, called "myFunctions.py" simply defines a geoprocessing workflow as a python function.  This is called by script #2 called "callOutsideFunction.py" which does exactly that.  The function "getSurroundingStates" could just as well been named "findAjacent" or similiar if the code was more general, subsituting variable for the hardcoded paths and names.

The idea here is that for each of your repetitive tasks that you must perform, make it a function and call it from a master script.  As you have more time due to your automation, you'll get new tasks that you can code and plug in to your master script.  Each of these scripts can be called conditionally, based on dates, the attribute of a feature, if a new file is added to a folder... anything you fancy!

Let me know if you get any mileage out of this one.  I think altering this sample could pretty much run your whole shop!

 


#--------------------------------------------------------------------------------------
# myFunctions.py
# Authour: Kevin Bell
# Date: 20070216

def getSurroundingStates(stateName):
    '''given a state name, creates a shp with those surrounding it '''
    import arcgisscripting
    gp = arcgisscripting.create()
    gp.overwriteoutput = 1
    gp.workspace = (r"C:\ArcGIS\ArcTutor\Using_ArcGIS_Desktop")
   
    try:
        gp.MakeFeatureLayer("USstates.shp","States")
        gp.SelectLayerByAttribute("States", "NEW_SELECTION", '"STATE_NAME"' + " =  " + "'" + stateName + "'")
        gp.SelectLayerByLocation("States", "BOUNDARY_TOUCHES", "States")
        gp.CopyFeatures("States", "c:/temp/statesSurrounding" + stateName + ".shp")
        print "success: getSurroundingStates of " + stateName

    except:
        print gp.GetMessages()
   
    del gp

#------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------
# callOutsideFunction.py
# Authour: Kevin Bell
# Date: 20070216

import os

#-------you must tell python were to look for your other script
os.sys.path.append(r"C:\scripts")

import myFunctions

myFunctions.getSurroundingStates("California")

#------- you can prompt for the input State in real time...
#myFunctions.getSurroundingStates(raw_input("Enter a State Name: "))

#-------"map" is a python built-in function will execute a function for each item in a list...
#stateList = ["Mississippi", "Oregon", "New York"]
#map(myFunctions.getSurroundingStates, stateList)

print "done calling imported function"


Users' Comments  
 

No comment posted

Add your comment

30, Jan. 2008
Last Updated ( 31, Jan. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for