Intro To Python For ArcGIS Basic OLD
Intro To Python For ArcGIS Basic OLD
Renzo Sanchez-Silva
Claudia Engel
Patricia Carbajales
http://bit.ly/pythonGIS
Does this describe you?
• Improves productivity
Python 101
• Where do I write Python code?
- IDE like PythonWin or PyScripter
- Python window in ArcGIS
var = "a"
if var == "a":
# Execute indented lines
print("variable is a")
else:
print("variable is not a")
Python 101
x=1
while x < 5:
print x
x=x+1
x = [1, 2, 3, 4]
for num in x:
print num
Python 101
• Case sensitivity
- Variables, functions, etc. are case sensitive
- name ‘X’ is not defined, function ‘X’ does not exist
• Building blocks
- Function: a defined piece of functionality that performs a specific task;
requires arguments ()
- Module: a Python file where functions live; imported
- Package: a collection of modules
- math.sqrt(100) … 10.0
- “There’s a module for that”
ArcPy
• The access point to geoprocessing tools
# Import ArcPy
import arcpy
• help(“arcpy.Buffer_analysis”)
Setting environments in Python
arcpy.env.workspace = "C:/Data"
arcpy.env.extent = "0 0 100 100"
Error handling
• arcpy.GetMessages()
- (): All messages
- (0): Only informative messages
- (1): Only warning messages
- (2): Only error messages
arcpy.GetMessages
>>>
Executing: Buffer Roads.shp Roads_buffer.shp '50 Meters'
Start Time: Tue July 12 08:52:40 2011
Executing (Buffer) successfully.
End Time: Tue July 12 03:52:45 2011(Elapsed Time: 5.00…
Python error handling
• Try…Except…
- "Try to do something, and if an error occurs, do
something else!"
# If an error occurs
except:
# Print that Buffer failed and why
print("Buffer failed")
print(arcpy.GetMessages(2))
ArcPy Functions
ArcPy functions
• Helper functions
• Perform useful scripting tasks
- Print messages (GetMessages)
- List data to perform batch processing
(ListFeatureClasses, 12 total List
functions)
- Getting data properties (Describe)
- Etc.
• Supports automation of
manual tasks
Batch processing
print(desc.shapeType)
>>> "Polyline"
Batch Processing
Exercise
Accessing R with python, rpy2
Demonstration by Claudia Engel
Demo
Python scripting resources
• ArcGIS Resource Center
- resources.arcgis.com
- Online documentation
- Geoprocessing: script gallery,
blog, tutorials, presentations
• python.org
• Python References
- Learning Python by Lutz, et al.
- The Python Standard Library by
Example by Hellmann
- diveintopython.org
Please complete our post-workshop
survey
http://bit.ly/gisworkshopsurvey
Thanks!