Introduction to GIS PythonScript CGIS-NURIntroduction to ArcGIS II
Lesson 6 overview How do I create a new script? Introduction to GIS Lesson 6 overview How do I create a new script? How do I write/change a script? How do I run a tool from a script? How do I set an environment setting in a script? What syntax do I use? What functionality is available? How do I run a script from an ArcGIS application? How do I fix my errors? CGIS-NURIntroduction to ArcGIS II
Why write scripts? Automate workflow Introduction to GIS Why write scripts? Automate workflow Copy all incoming data into a geodatabase Perform project, clip, buffer operations on multiple data sets Run code at specific dates and times Windows AT command Windows scheduler Easily distribute code A script is a self-contained, single file CGIS-NURIntroduction to ArcGIS II
Introduction to GIS Points of interest Scripting ArcObjects do not replace standard ArcObjects Customize or interact with the interface Developers (Desktop, Engine, Server) Can use many scripting/programming languages VBScript, JScript, Perl, Python, VBA, VB, C++, etc. Anything that supports COM Users do not have to learn a proprietary language ESRI primarily supports Python Installed with ArcGIS 9 Samples, documentation, help CGIS-NURIntroduction to ArcGIS II
The Python scripting language Introduction to GIS The Python scripting language What is Python? An open-source, object-oriented, scripting language Can view and modify source code Support for large projects Easy to use Why use Python? Offers IDEs with debugging tools Modular – can be broken apart Cross platform Ability to compile scripts Installed with ArcGIS 9, ESRI samples provided PythonWin CGIS-NURIntroduction to ArcGIS II
The Basics of Python Introduction to GIS CGIS-NURIntroduction to ArcGIS II
Lesson overview Where to write code Commenting code Introduction to GIS Lesson overview Where to write code Commenting code Strings, numbers, and lists Line continuation Functions, modules, and statements Decision making and looping Case sensitive rules CGIS-NURIntroduction to ArcGIS II
Where to write code There are many places to write Python code Introduction to GIS Where to write code There are many places to write Python code Text editor: Notepad, Wordpad, etc. Python command prompt IDE: PythonWin, IDLE, and so on IDEs allow you to perform all jobs from one location Write, save, run, and debug code from one location Command prompt and both IDEs installed with ArcGIS Lectures and exercises use PythonWin CGIS-NURIntroduction to ArcGIS II
PythonWin interface Menus, toolbars, and context menus Script window Introduction to GIS PythonWin interface Menus, toolbars, and context menus Script window Write and save code Interactive window Test lines of code Report messages Benefits Windows look and feel All in one application CGIS-NURIntroduction to ArcGIS II
Comments Comment: A non-executable line of code Introduction to GIS Comments Comment: A non-executable line of code One number sign (#) for green and italicized Two number signs (##) for grey Can comment and uncomment blocks of code Highlight code and use script window's context menu CGIS-NURIntroduction to ArcGIS II
Variables in Python Variables are dynamically typed Introduction to GIS Variables in Python Variables are dynamically typed No declaration keyword No type assignment fc = "C:/Student/PYTH/Database/rwanda.shp" Variables are case sensitive scale = 10000 Scale = 20000 Variables can hold different data types Strings, numbers, lists, tuples, dictionaries, files Two different variables! CGIS-NURIntroduction to ArcGIS II
Strings Variables can hold strings Introduction to GIS Strings Variables can hold strings folder = "C:/Student" whereClause = "[STREET_NAM] = 'CATALINA'" Strings surrounded in double (") or single (‘) quotes Can embed one string in another Pathnames use two back (\\) or one forward (/) slash Strings can be combined together gdbPath = "C:\\malaria.mdb" fc = “roads" fullPath = gdbPath + "\\" + fc Strings are indexed fc = “roads.shp" newFC = fc[:-4] CGIS-NURIntroduction to ArcGIS II
Numbers and lists Variables can hold numbers and expressions Introduction to GIS Numbers and lists Variables can hold numbers and expressions num1 = 1.2 num2 = 3 + 5 Variables can hold lists numList = [1, 2, 3] fcList = ["Roads", "Streets", "Parcels", "Zipcodes"] Lists are indexed fc1 = fcList[1] fc2 = fcList[0:2] fc3 = fcList[0:-1] fc4 = fcList[2:] CGIS-NURIntroduction to ArcGIS II
Variable naming conventions Introduction to GIS Variable naming conventions Uppercase versus lowercase First word lowercase, capitalize each successive word outputFieldName = "Rwanda" Acronym at the beginning, use lowercase letters gdbPath = "C:\\malaria.mdb" fc = "roads.shp" Acronym in the middle or at the end, use uppercase letters inputFC = "roads.shp" Avoid special characters (e.g. / \ & * !) Use descriptive variable names CGIS-NURIntroduction to ArcGIS II
Line continuation Line continuation characters Introduction to GIS Line continuation Line continuation characters Parentheses ( ), brackets [ ], and braces { } Backslash \ Indentation is automatic CGIS-NURIntroduction to ArcGIS II
Decision making syntax Introduction to GIS Decision making syntax Testing conditions Colons used at end of each condition Indentation defines what executes for each condition Python automatically indents when you press Enter Use tabs or spaces, must be consistent Two equal signs for conditions, one for assignment CGIS-NURIntroduction to ArcGIS II
Looping syntax While loops, counted loops, and list loops Introduction to GIS Looping syntax While loops, counted loops, and list loops Colons used at end of each statement Indentation defines what executes for the loop CGIS-NURIntroduction to ArcGIS II
Demo Used Syntax Examples Adapting scripts for own use Introduction to GIS Demo Used Syntax Examples Adapting scripts for own use CGIS-NURIntroduction to ArcGIS II