Lecture 12 Programming for automation of common data management tasks

Similar documents
Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Python Getting Started

Some material adapted from Upenn cmpe391 slides and other sources

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Python Getting Started

Python: Getting Started. Ben

Ways to Fail this Class. Welcome! Questions? Course Policies. CSC 9010: Natural Language Processing. Who / what / where / when / why / how 1/9/2014

Python Interpreted language: work with an evaluator for language expressions (like DrJava, but more flexible) Dynamically typed

Full Python Tutorial. Python. Language features. Dynamic typing the key difference. Why Python? Introduction to Programming Languages and Techniques

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Using Python with ArcGIS

Table of Contents EVALUATION COPY

And Parallelism. Parallelism in Prolog. OR Parallelism

ENGR 101 Engineering Design Workshop

Senthil Kumaran S

CSCE 110 Programming I

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Using Python with ArcGIS

The current topic: Python. Announcements. Python. Python

CIS192: Python Programming

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Using Python with ArcGIS

Getting started with programming For geospatial data analysis. Robert Hijmans UC Davis

Programming to Python

Basic Syntax - First Program 1

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Python Intro GIS Week 1. Jake K. Carr

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Introduction to Python

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introduction to Python - Part I CNV Lab

Beyond Blocks: Python Session #1

What Version Number to Install

S206E Lecture 19, 5/24/2016, Python an overview

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

Play with Python: An intro to Data Science

Getting Started with Python

Python for C programmers

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

A Brief Introduction to Python for those who know Java. (Last extensive revision: Daniel Moroz, fall 2015)

Algorithms and Programming I. Lecture#12 Spring 2015

Introduction to Python

CS Programming Languages: Python

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007

CIS192 Python Programming. Robert Rand. August 27, 2015

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Part III Appendices 165

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Python Programming, bridging course 2011

Introduction to Python for Plone developers

Fundamentals of Programming (Python) Getting Started with Programming

Statements 2. a operator= b a = a operator b

Flow Control: Branches and loops

CS1 Lecture 3 Jan. 18, 2019

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Python in 10 (50) minutes

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Variables, expressions and statements

CS1 Lecture 3 Jan. 22, 2018

Lecture 4. Defining Functions

Introduction to Python

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Python: common syntax

Python for Non-programmers

Building tools with Python

Chapter 2 Writing Simple Programs

Lecture 3. Functions & Modules

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Accelerating Information Technology Innovation

Introduction to Python, Cplex and Gurobi

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Introduction to Python

Using Python in ArcGIS Oli Helm May 2, 2013

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

Short, Unique and Mysterious

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

STSCI Python Introduction. Class URL

High Level Scripting. Gino Tosti University & INFN Perugia. 06/09/2010 SciNeGhe Data Analysis Tutorial

Accelerating Information Technology Innovation

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Not-So-Mini-Lecture 6. Modules & Scripts

Intro. Scheme Basics. scm> 5 5. scm>

Introduction to Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

ENGR 102 Engineering Lab I - Computation

MUTABLE LISTS AND DICTIONARIES 4

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

CS Summer 2013

Lists, loops and decisions

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Python Unit

Transcription:

Lecture 12 Programming for automation of common data management tasks Daniel P. Ames Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant EPS

Goals this Week To learn the basics of Python Scripting for automation of basic geoprocessing and database management tasks. Tuesday focus on intro to Python and ArcPy for mapping and geoprocessing tasks Thursday focus on PyODBC and using Python to automate database tasks

Useful Software to have on your laptop computer in class ArcGIS. http://www.esri.com/software/arcgis/arcgisfor-desktop/free-trial Python. www.python.org. PyODBC. http://code.google.com/p/pyodbc/. ArcMap 10.x comes with Python 2.6. It installs it in your C: folder under C:\Python\ArcGIS10\. PyODBC. http://code.google.com/p/pyodbc/downlo ads/detail?name=pyodbc-3.0.6.win32- py2.6.exe&can=2&q=

Slide Credits 2010 Mitch Marcus and Varun Aggarwala, University of Pennsylvania 2010 ESRI Conference Workshop: Python Essentials in ArcGIS-I 2002 LinuxWorld Tutorial: Introduction to Python by Guido van Rossum

Python Python is an open source scripting language. Developed by Guido van Rossum in the early 1990s Named after Monty Python Available on eniac Available for download from http://www.python.org CIS 530 - Intro to NLP 5

Why Python? Powerful but unobtrusive object system Every value is an object Classes guide but do not dominate object construction Powerful collection and iteration abstractions Dynamic typing makes generics easy

Python Interpreted language: works with an evaluator for language expressions Dynamically typed: variables do not have a predefined type Rich, built-in collection types: Lists Tuples Dictionaries (maps) Sets Concise

Language features Indentation instead of braces Newline separates statements Several sequence types Strings : made of characters, immutable Lists [ ]: made of anything, mutable Tuples ( ) : made of anything, immutable Powerful subscripting (slicing) Functions are independent entities (not all functions are methods) Exceptions

Basics

Dynamic typing Variables come into existence when first assigned a value A variable can refer to an object of any type

Playing with Python (1) >>> 2+3 5 >>> 2/3 0 >>> 2.0/3 0.66666666666666663 >>> x=4.5 >>> int(x) 4

Playing with Python (2) >>> x='abc' >>> x[0] 'a' >>> x[1:3] 'bc' >>> x[:2] 'ab >>> x[1]='d' Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> x[1]='d' TypeError: 'str' object does not support item assignment

Playing with Python (3) >>> x=['a','b','c'] >>> x[1] 'b' >>> x[1:] ['b', 'c'] >>> x[1]='d' >>> x ['a', 'd', 'c']

Playing with Python (4) >>> def p(x): if len(x) < 2: return True else: return x[0] == x[-1] and p(x[1:-1]) >>> p('abc') False >>> p('aba') True >>> p([1,2,3]) False >>> p([1, a, a,1]) True >>> p((false,2,2,false)) True >>> p(( a,1,1)) False

The Python Command Line Interpreter Interactive interface to Python % python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> Python interpreter evaluates inputs: >>> 3*(7+2) 27

The IDLE GUI Environment (Windows) Shell for interactive evaluation. Text editor with color-coding and smart indenting for creating Python files. Menu commands for changing system settings and running files. Installed with ArcMap

A Code Sample (in IDLE) x = 34-23 # A comment. y = Hello # Another one. z = 3.45 if z == 3.45 or y == Hello : x = x + 1 y = y + World # String concat. print x print y

Enough to Understand the Code Indentation matters to the meaning of the code: Block structure indicated by indentation The first assignment to a variable creates it. Variable types don t need to be declared. Python figures out the variable types on its own. Assignment uses = and comparison uses ==. For numbers + - * / % are as expected. Special use of + for string concatenation. Special use of % for string formatting (as with printf in C) Logical operators are words (and, or, not) not symbols Simple printing can be done with print.

Basic Datatypes Integers (default for numbers) z = 5 / 2 Floats # Answer is 2, integer division. x = 3.456 Strings Can use or to specify. abc abc (Same thing.) Unmatched can occur within the string. matt s Use triple double-quotes for multi-line strings or strings than contain both and inside of them: a b c

Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines. Use a newline to end a line of code. Use \ when must go to next line prematurely. No braces { } to mark blocks of code in Python Use consistent indentation instead. The first line with less indentation is outside of the block. The first line with more indentation starts a nested block Often a colon appears at the start of a new block. (E.g. for function and class definitions.)

Comments Start comments with # the rest of line is ignored. Can include a documentation string as the first line of any new function or class that you define. The development environment, debugger, and other tools use it: it s good style to include one. def my_function(x, y): This is the docstring. This function does blah blah blah. # The code would go here...

Naming Rules Names are case sensitive and cannot start with a number. They can contain letters, numbers, and underscores. bob Bob _bob _2_bob_ bob_2 BoB There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while

The + Operator The + operator produces a new tuple, list, or string whose value is the concatenation of its arguments. >>> (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) >>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> Hello + + World Hello World

Lists >>> li = [ abc, 23, 4.34, 23] >>> li[1] = 45 >>> li [ abc, 45, 4.34, 23] We can change lists in place. Name li still points to the same memory reference when we re done.

Operations on Lists Only 1 >>> li = [1, 11, 3, 4, 5] >>> li.append( a ) # Note the method syntax >>> li [1, 11, 3, 4, 5, a ] >>> li.insert(2, i ) >>>li [1, 11, i, 3, 4, 5, a ]

Functions

Defining Functions Function definition begins with def Function name and its arguments. def get_final_answer(filename): Documentation String line1 line2 return total_counter Colon. The indentation matters First line with less indentation is considered to be The keyword return indicates the outside of the function definition. value to be sent back to the caller. No header file or declaration of types of function or arguments. CIS 530 - Intro to NLP

Python and Types Python determines the data types of variable bindings in a program automatically. Dynamic Typing But Python s not casual about types, it enforces the types of objects. Strong Typing So, for example, you can t just append an integer to a string. You must first convert the integer to a string itself. x = the answer is # Decides x is bound to a string. y = 23 print x + y # Decides y is bound to an integer. # Python will complain about this.

Calling a Function The syntax for a function call is: >>> def myfun(x, y): return x * y >>> myfun(3, 4) 12

Functions without returns All functions in Python have a return value even if no return line inside the code. Functions without a return return the special value None. None is a special constant in the language. None is used like NULL, void, or nil in other languages. None is also logically equivalent to False. The interpreter doesn t print None

Logical Expressions

True and False True and False are constants in Python. Other values equivalent to True and False: False: zero, None, empty container or object True: non-zero numbers, non-empty objects Comparison operators: ==,!=, <, <=, etc. X and Y have same value: X == Y Compare with X is Y : X and Y are two variables that refer to the identical same object.

Boolean Logic Expressions You can also combine Boolean expressions. True if a is True and b is True: a and b True if a is True or b is True: True if a is False: a or b not a Use parentheses as needed to disambiguate complex Boolean expressions.

Special Properties of and and or Actually and and or don t return True or False. They return the value of one of their sub-expressions (which may be a non-boolean value). X and Y and Z If all are true, returns value of Z. Otherwise, returns value of first false sub-expression. X or Y or Z If all are false, returns value of Z. Otherwise, returns value of first true sub-expression. And and or use lazy evaluation, so no further expressions are evaluated

Control of Flow

if Statements if x == 3: print X equals 3. elif x == 2: print X equals 2. else: print X equals something else. print This is outside the if. Be careful! The keyword if is also used in the syntax of filtered list comprehensions. Note: Use of indentation for blocks Colon (:) after boolean expression

while Loops >>> x = 3 >>> while x < 5: print x, "still in the loop" x = x + 1 3 still in the loop 4 still in the loop >>> x = 6 >>> while x < 5: print x, "still in the loop" >>>

For Loops

For Loops 1 For-each is Python s only for construction A for loop steps through each of the items in a collection type, or any other type of object which is iterable for <item> in <collection>: <statements> If <collection> is a list or a tuple, then the loop steps through each element of the sequence. If <collection> is a string, then the loop steps through each character of the string. for somechar in Hello World : print somechar

For Loops 2 for <item> in <collection>: <statements> <item> can be more complex than a single variable name. When the elements of <collection> are themselves sequences, then <item> can match the structure of the elements. This multiple assignment can make it easier to access the individual parts of each element. for (x, y) in [(a,1), (b,2), (c,3), (d,4)]: print x

String Operations A number of methods for the string class perform useful formatting operations: >>> hello.upper() HELLO Check the Python documentation for many other handy string operations. Helpful hint: use <string>.strip() to strip off final newlines from lines read from files

Importing and Modules

Importing and Modules Use classes & functions defined in another file. A Python module is a single file with the same name (plus the.py extension) Where does Python look for module files? The list of directories where Python looks: sys.path To add a directory of your own to this list, append it to this list. sys.path.append( /my/new/path )

Commonly Used Modules Some useful modules to import, included with Python: Module: sys Maxint Module: os Module: os.path - Lots of handy stuff. - OS specific code. - Directory processing.

More Commonly Used Modules Module: math Exponents sqrt - Mathematical code. Module: Random - Random number code. Randrange Uniform Choice Shuffle To see what s in the standard library of modules, check out the Python Library Reference: http://docs.python.org/lib/lib.html Or O Reilly s Python in a Nutshell: http://proquest.safaribooksonline.com/0596100469 (URL works inside of UPenn, afaik, otherwise see the course web page)

Finally pass It does absolutely nothing. Just holds the place of where something should go syntactically. Programmers like to use it to waste time in some code, or to hold the place where they would like put some real code at a later time. for i in range(1000): pass

ArcPy Python Scripting Basics

Why use Python scripting in ArcMap? Python: a free, cross-platform, and easy to learn language Used to execute single tools or strings of tools Develop, execute, and share geoprocessing workflows Automation

ArcPy The access point to every geoprocessing tool A package of functions, classes and modules, all related to scripting in ArcGIS - Functions that enhance geoprocessing workflows (ListFeatureClasses, Describe, SearchCursor ) - Classes that can be used to create complex objects (SpatialReference and FieldMap objects) - Modules that provide additional functionality (Mapping and SpatialAnalyst modules) Builds on arcgisscripting module (pre-10.0)

ArcGIS Python window Embedded, interactive Python window within ArcGIS Access to ArcPy, any Python functionality Great for experimenting with and learning Python

Executing a tool in Python ArcPy must be imported Follow syntax: arcpy.toolname_toolboxalias() Enter input and output parameters # Import ArcPy import arcpy # Set workspace environment arcpy.env.workspace = C:/Data # Execute Geoprocessing tool arcpy.buffer_analysis( roads.shp", roads_buffer.shp", 50 Meters )

Getting tool syntax Results window, Copy as Python Snippet Export Model to Python script Drag tool into Python window Tool documentation arcpy.usage( Buffer_analysis )

Setting environments in Python Accessed from arcpy.env Common environments: Workspace, coordinate system, extent Examples: arcpy.env.workspace arcpy.env.outputcoordinatesystem arcpy.env.extent

Live Demo Live demonstration of running geoprocessing functions in ArcMap

Geoprocessing messages Tools return three types of messages: Informative messages Warning messages Error messages Displayed in the ArcGIS Python window arcpy.getmessages() GetMessages(): All messages GetMessages(0): Only informative messages GetMessages(1): Only warning messages GetMessages(2): Only error messages

ArcPy functions: GetMessages # Execute Geoprocessing tool arcpy.buffer_analysis( roads.shp", roads_buffer.shp", 50 Meters ) # Print the execution messages print arcpy.getmessages() Executing: Buffer roads.shp roads_buffer.shp 50 Meters Start Time: Tue July 13 13:52:40 2010 Executing (Buffer) successfully. End Time: Tue July 13 13:52:45 2010(Elapsed Time: 5.00

Error handling basics Why do errors occur? Incorrect tool use Typos Syntax errors Python error handling Try Except try: # Do Something except: # A failure occurred, do something else

Try, Except Statement # Start Try block try: arcpy.buffer_analysis( roads.shp", roads_buffer.shp", 50 Meters ) # If an error occurs except: # Print that Buffer failed and why print "Buffer failed print arcpy.getmessages(2)

Live Demo Live demonstration of error handling in Python/ArcPy

Geoprocessing Tasks

ArcPy functions Perform useful scripting tasks Print geoprocessing messages (GetMessages) List data to aid batch processing (ListFeatureClasses, ListFields; 12 total List functions) Getting data properties (Describe) Allow automation of manual tasks

Batch processing Run a geoprocessing operation multiple times with some automation Example: Using the Clip tool to clip every feature class in a workspace to a boundary List functions used in Python to perform batch processing

ArcPy functions: ListFeatureClasses # Set the workspace arcpy.env.workspace = C:/Data/FileGDB.gdb/FDs" # Get a list of all polygon feature classes fclist = arcpy.listfeatureclasses("*", "polygon") # Print the list of feature classes one at a time for fc in fclist: print fc

ArcPy functions: ListFields # List all fields in a feature class fields = arcpy.listfields( C:/Data/roads.shp") # loop through all the fields for field in fields: # print the field s name and field type print field.name, field.type Field objects have properties name, aliasname, domain, editable, hasindex, isnullable, isunique, length, precision, scale, type

ArcPy functions: Describe Returns an object with properties Allows script to determine properties of data Data type (shapefile, coverage, network dataset, etc.) Shape type (point, polygon, line, etc.) Spatial reference Extent of features Path

ArcPy functions: Describe # Describe a feature class desc = arcpy.describe( C:/Data/roads.shp") # Branch script logic based on ShapeType property if desc.shapetype == "Polyline" : print "The fc is a line feature class elif desc.shapetype == "Polygon" : print "The fc is a polygon feature class else: print The fc is not a line or polygon."

Receiving arguments Arguments are inputs to a script - Makes script more flexible and portable - Values passed to script from user, instead of hard-coded Use GetParameterAsText to read arguments - Zero-based index Connect script to an ArcGIS script tool

Receiving arguments # Create variables from input arguments inputfc = arcpy.getparameterastext(0) outputfc = arcpy.getparameterastext(1) # First and third parameters come from arguments arcpy.clip_analysis(inputfc, C:/Data/boundary.shp", outputfc)

Python scripting resources ArcGIS Resource Centers http://resources.arcgis.com Online documentation Geoprocessing Resource Center: script gallery, blog, presentations Python Reference Books Learning Python by Mark Lutz & David Ascher Core Python Programming by Wesley J. Chun Python Organization http://www.python.org DiveIntoPython.org Free tutorial: http://diveintopython.org/#download