Select to view content in your preferred language

Creating geoprocessing scripts by using tools from toolbox

842
2
07-04-2011 07:43 PM
NeemaMohseni
Emerging Contributor
Part A: Create two geoprocessing scripts. Each should utilize at least two tools from ArcToolbox

Part B: Using one of two scripts created in Part A, create a script tool with at least one user input parameter.
Tags (2)
0 Kudos
2 Replies
ChristopherFricke1
Deactivated User
Two routes:

1) Use model builder and the "Export to Python" functionality (File / Export / Python) http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm


2) Check out the documentation on the support site for the tools you would like to string together.  This helped me a bunch http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_ref....
0 Kudos
StacyRendall1
Frequent Contributor
Two routes:

1) Use model builder and the "Export to Python" functionality (File / Export / Python) http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm


2) Check out the documentation on the support site for the tools you would like to string together.  This helped me a bunch http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_ref....


Except Option 1 sucks... Exported models usually don't work, and they are incredibly poorly written - which makes them unbelievably confusing!

Go with Option 2, but following are some Arcpy basics... It will also be handy to have done some basic Python tutorials before attempting to do too much.

1. Every script using Arc functions must start with import arcpy
'''script.py
'''
import arcpy


2. Get user inputs with 'GetParameterAsText'
'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

This gets the first user entered parameter as text. If you haven't specified any parameters when adding to Arc (see step 3) it will cause the script to fail.

3. Using the tool in Arc
Right click on a toolbox you can edit (if you haven't already, you will have to create one in "My Toolboxes" or within a folder), go down to 'Add' and select 'Script...', give it a name and label, select the script file, then add the parameters. For this example we are getting a Feature Class, so call it InputFeatureClass (or something meaningful) and set the Data Type to Feature Class. Click Finish. Double click on your new script tool to use it. At the moment it does nothing, but if you give it a feature class and click OK, it should run happily and say it was successful.

From now on we can just edit the script where it is and the changes will carry through, you won't need to import it again.

It is important to note that paths to feature classes are just strings, e.g. 'C:\\GIS Data\\NYC\\2007\\Networks.gdb\\BusLines'.

4. Adding messages to the display with 'AddMessage'
'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# inform user of selected feature class path
arcpy.AddMessage('  Input Feature Class: '+inputFeatureClass)

Save the script and run it again, and it will now print the feature class path before finishing happily.

5. Performing a geoprocessing operation
Let's do a buffer on the input features, and also get the user to input the buffer size for the operation; you will need to right click on the tool in Arc, go down to Properties, and in the parameters tab add the parameter BufferSize_m as a Long (integer). Buffer also requires an output feature class, so you will have to add that to the parameters as well; it's data type should be feature class, but you will need to set the Direction (under Parameter Properties) to Output. The script now becomes (with the message made a little more meaningful):
'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# get buffer size 
buffer = arcpy.GetParameterAsText(1)

# get output feature class
outputFeatureClass = arcpy.GetParameterAsText(2)

# inform user of selected feature class path
arcpy.AddMessage('  Buffering input Feature Class, '+inputFeatureClass+' by '+buffer+' Meters and writing to: '+outputFeatureClass)

buffer_corrected = buffer + ' Meters'

arcpy.Buffer_analysis(inputFeatureClass,  outputFeatureClass, buffer_corrected)


This should help you get started... Just remember that reading the documentation is absolutely essential!
0 Kudos