What is a Function?
- A function is a piece of code that is called (initiated) from somewhere else in the program. It is an ideal way to create efficient reusable code that is very organized and easy to read.
- Local vs. Global Variables:
- Variables created in a function are local variable that can not be referenced outside that function unless returned.
- Global variables created outside of a function can be referenced outside and insider any function.
- To create a global variable, the command global must be used in the function!
- Example of a Function:
- def myFun(x,y):
- result = x+y
- return result
- print myFun(3,4) # prints 7
- All Geoprocessing Tools (ArcToolbox) can be called directly from Python.
- To identify the proper syntax, parameters, and options, right-click a tool from ArcToolbox and select 'Help'.
- It is also very helpful to see an example of proper syntax by examining the processing dialog text when the geoprocessing tool is run from ArcMap directly.
- Example of a buffer scripting syntax:
- Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field, line_side, line_end_type, dissolve_option, dissolve_field)
- This example in action:
- arcpy.Buffer_analysis (inshape.shp, outshape, "5 Miles")
Please note that all references to actual directories on your computer use the forward slash /. This is opposite what Windows uses.
Random Integers:
- import random (best used at top of script with import statements)
- for integers :: random.randint(lower bound, upper bound)
- for floats :: random.uniform(lower bound, upper bound)
Round Values:
- round(value, number of decimal places)
Delete if Files Exist:
- if arcpy.Exists(file):
- arcpy.Delete_management(file)
- import os
- if os.path.exists(file):
os.remove(file)
- import os, glob
- files = glob.glob(directory/*)
- for f in files:
- os.remove(f)
- import os
- os.path.dirname(file) # returns path
- oc.path.basename(file) # returns name
- string.replace(old characters, new characters)
- import sys
- sys.argv[1] is first parameter; sys.argv[2] is second parameter; etc.
TOGETHER (Don't Peek!)
Exercise #1: Write a script that will create a new shapefile on the desktop using a function. Hard code the path and file name.
Exercise #2: Add a function that will delete the shapefile if it already exists
Exercise #3: Add a function that will randomly create a polygon or polyline or point shapefile
Exercise #4: Add a command that will add a DOUBLE field called mydbl (Does not need to be another function)
Exercise #5: Create ArcGIS Geoprocessing form and add form control to allow user to specify (1) the shapefile path and name and (2) the DOUBLE field name. The script should then concatenate the geometry type (point, polygon, polyline) to the name provided in the script.
INDIVIDUAL (shapefile)
Create a program that will (1) Add a field named CALC to the provided shapefile, and (2) multiply the provided median household income data in the income field by the % over 65 population in the p_over65 field. Use an ArcMap Geoprocessing Form.
No comments:
Post a Comment