Lecture 8: Python - Learngroup

Lecture 8: Python - Learngroup

Lecture 8: Python Writing Scripts in GIS Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information Assiut University April 10, 2016 INF424: GIS. Dr. Taysir Hassan A. Soliman Outline The following free online books might be helpful: Think Python Dive Into Python Python Scientific Lecture Notes Google's Python Class

INF424: GIS. Dr. Taysir Hassan A. Soliman Outline Module 1: Basic Python programs, functions Module 2: expressions, variables, for loops Module 3: File processing Module 4: Geprocessing with python INF424: GIS. Dr. Taysir Hassan A. Soliman Open source general-purpose language. Object Oriented, Procedural, Functional Easy to interface with C/Java

Downloads: http://www.python.org Documentation: http://www.python.org/doc/ INF424: GIS. Dr. Taysir Hassan A. Soliman Python interpreter evaluates inputs: >>> 3*(7+2) 27 To exit Python: CTRL-D INF424: GIS. Dr. Taysir Hassan A. Soliman INF424: GIS. Dr. Taysir Hassan A. Soliman INF424: GIS. Dr. Taysir Hassan A. Soliman Module 1: Python! basic Python programs, defining functions Created in 1991 by Guido van Rossum (now at Google)

Named for Monty Python Useful as a scripting language script: A small program meant for one-time use Targeted towards small to medium sized projects Used by: Google, Yahoo!, Youtube Many Linux distributions Games and apps (e.g. Eve Online) Writing GIS Scripts INF424: GIS. Dr. Taysir Hassan A. Soliman What is a script? INF424: GIS. Dr. Taysir Hassan A. Soliman INF424: GIS. Dr. Taysir Hassan A. Soliman

Who uses Python? Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers -Cuong Do, Software Architect, YouTube.com INF424: GIS. Dr. Taysir Hassan A. Soliman Installing Python Windows: Download Python from http://www.python.org Install Python. Run Idle from the Start Menu.

Mac OS X: Python is already installed. Open a terminal and run python or run Idle from Finder. Linux: Chances are you already have Python installed. To check, run python from the terminal. If not, install from your distribution's package system. Note: For step by step installation instructions, see the course web site. INF424: GIS. Dr. Taysir Hassan A. Soliman The Python Interpreter

Allows you to type commands one-at-a-time and see results A great way to explore Python's syntax Repeat previous command: Alt+P INF424: GIS. Dr. Taysir Hassan A. Soliman Console output: System.out.println Methods: public static void name() { ... Hello2.java 1 2 3 4 5 6 7

8 9 public class Hello2 { public static void main(String[] args) { hello(); } } public static void hello() { System.out.println("Hello, world!"); } INF424: GIS. Dr. Taysir Hassan A. Soliman Our First Python Program Python does not have a main method like Java

The program's main code is just written directly in the file Python statements do not end with semicolons hello.py 1 print("Hello, world!) INF424: GIS. Dr. Taysir Hassan A. Soliman A Brief Review INF424: GIS. Dr. Taysir Hassan A. Soliman The print Statement print("text)

print() (a blank line) Escape sequences such as \" are the same as in Java Strings can also start/end with ' swallows.py 1 2 3 4 print(Hello, world!) print() print("Suppose two swallows \"carry\" it together.) print('African or "European" swallows?) INF424: GIS. Dr. Taysir Hassan A. Soliman Comments Syntax:

# comment text (one line) swallows2.py 1 2 3 4 5 6 # Suzy Student, CSE 142, Fall 2097 # This program prints important messages. print("Hello, world!) print() # blank line print(Suppose two swallows \"carry\" it together.) print('African or "European" swallows?) INF424: GIS. Dr. Taysir Hassan A. Soliman

Functions Function: Equivalent to a static method in Java. Syntax: def name(): statement statement ... statement hello2.py 1 2 3 4 5 6 7

# Prints a helpful message. def hello(): print("Hello, world!) # main (calls hello twice) hello() hello() Must be declared above the 'main' code Statements inside the function must be indented INF424: GIS. Dr. Taysir Hassan A. Soliman Whitespace Significance Python uses indentation to indicate blocks, instead of {} Makes the code simpler and more readable In Java, indenting is optional. In Python, you must indent.

hello3.py 1 2 3 4 5 6 7 8 # Prints a helpful message. def hello(): print("Hello, world!) print("How are you?) # main (calls hello twice) hello() hello() INF424: GIS. Dr. Taysir Hassan A. Soliman

Exercise Rewrite the Figures lecture program in Python. Its output: ______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ /

\ / \ | STOP | \ / \______/ ______ / \ / \ +--------+ INF424: GIS. Dr. Taysir Hassan A. Soliman Exercise Solution def egg(): top()

bottom() print def top(): print(" ______) print(" / \\) print("/ \\) def cup(): bottom() line() print def bottom(): print("\\ /)

print(" \\______/) def stop(): top() print("| bottom() print def hat(): top() line() print def line(): print("+--------+) STOP |)

# main egg() cup() stop() hat() INF424: GIS. Dr. Taysir Hassan A. Soliman Module 2 expressions, variables, for loops INF424: GIS. Dr. Taysir Hassan A. Soliman Expressions Arithmetic is very similar to Java Operators: + - * / % (plus ** for exponentiation)

Precedence: () before ** before * / % before >>> 1 + 1 + 2 Integers vs. real numbers >>> 1 + 3 * 4 11 You may use // for integer division >>> 7 // 2 3 >>> 7 / 2 3.5 >>> 7.0 / 2 3.5 INF424: GIS. Dr. Taysir Hassan A. Soliman - 2 Variables Declaring

no type is written; same syntax as assignment Operators no ++ or -- operators (must manually adjust by 1) Java Python int x = 2; x++; System.out.println(x); x = 2 x = x + 1 print(x) x = x * 8; System.out.println(x);

x = x * 8 print(x) double d = 3.2; d = d / 2; System.out.println(d); d = 3.2 d = d / 2 print(d) INF424: GIS. Dr. Taysir Hassan A. Soliman Naming Rules Names are case sensitive and cannot start with a number. They can contain letters, numbers m and underscores: Bob Bob _bob _2bob_ bob_2 BoB

There are some reserved words: And, def, class INF424: GIS. Dr. Taysir Hassan A. Soliman Types Python is looser about types than Java Variables' types do not need to be declared Variables can change types as a program is running Value Java type Python type 42 int

int 3.14 double float "ni!" String str INF424: GIS. Dr. Taysir Hassan A. Soliman String Multiplication Python strings can be multiplied by an integer. The result is many copies of the string concatenated together.

>>> "hello" * 3 "hellohellohello" >>> print(10 * "yo ) yo yo yo yo yo yo yo yo yo yo INF424: GIS. Dr. Taysir Hassan A. Soliman >>fruit = banana >> letter = fruit[1] >> Print(letter) >> a INF424: GIS. Dr. Taysir Hassan A. Soliman

INF424: GIS. Dr. Taysir Hassan A. Soliman Control Structures INF424: GIS. Dr. Taysir Hassan A. Soliman The for Loop for name in range(max): statements Repeats for values 0 (inclusive) to max (exclusive) >>> for i in range(5): ... print(i) 0 1

2 3 4 INF424: GIS. Dr. Taysir Hassan A. Soliman for Loop Variations for name in range(min, max): statements for name in range(min, max, step): statements Can specify a minimum other than 0, and a step other than 1 >>> for i in range(2, 6): ... print(i)

2 3 4 5 >>> for i in range(15, 0, -5): ... INF424:print(i) GIS. Dr. Taysir Hassan A. Soliman 15 Nested Loops Nested loops are often replaced by string * and + Java ....1 ...2 ..3 .4

5 1 for (int line = 1; line <= 5; line++) 2 { for (int j = 1; j <= (5 - line); 3 j++) { 4 System.out.print("."); 5 } 6 System.out.println(line); } Python 1 for line in range(1, 6):

print((5 - line) * "." + str(line)) 2 INF424: GIS. Dr. Taysir Hassan A. Soliman Constants Python doesn't really have constants. Instead, declare a variable at the top of your code. All methods will be able to use this "constant" value. constant.py 1 MAX_VALUE = 3 2 3 def printTop(): 4 for i in range(MAX_VALUE): 5

for j in range(i): print(j) 6 print() 7 8 9 def printBottom(): 10 for i in range(MAX_VALUE, 0, -1): 11 for j in range(i, 0, -1): 12 print(MAX_VALUE) 13 print() INF424: GIS. Dr. Taysir Hassan A. Soliman Exercise

Rewrite the Mirror lecture program in Python. Its output: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><>

| #================# Make the mirror resizable by using a "constant." INF424: GIS. Dr. Taysir Hassan A. Soliman Exercise INF424: GIS. Dr. Taysir Hassan A. Soliman SIZE = 4 Exercise Solution def bar(): print("#" + 4 * SIZE * "=" + "#) def top(): for line in range(1, SIZE + 1):

# split a long line by ending it with \ print("|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|) def bottom(): for line in range(SIZE, 0, print("|" + (-2 * line "<>" + (4 * line (-2 * line + 2 * # main bar() top() bottom() bar() -1): + 2 * SIZE) * " " + \ - 4) * "." + "<>" + \

SIZE) * " " + "|) INF424: GIS. Dr. Taysir Hassan A. Soliman Concatenating Ranges Ranges can be concatenated with + However, you must use the list() command Can be used to loop over a disjoint range of numbers >>> list(range(1, 5)) + list(range(10, 15)) [1, 2, 3, 4, 10, 11, 12, 13, 14] >>> for i in list(range(4)) + list(range(10, 7, 1)): ... print(i) 0 1 2 3

10 INF424: GIS. Dr. Taysir Hassan A. Soliman 9 Exercise Solution 2 SIZE = 4 def bar(): print "#" + 4 * SIZE * "=" + "#" def mirror(): for line in list(range(1, SIZE

list(range(SIZE, 0, -1)): print("|" + (-2 * line "<>" + (4 * line (-2 * line + 2 * + 1)) + + 2 * SIZE) * " " + \ - 4) * "." + "<>" + \ SIZE) * " " + "|) # main

bar() mirror() bar() INF424: GIS. Dr. Taysir Hassan A. Soliman Strings index 0 1 2 3 4

5 6 7 or -8 P -7 . -6 -5

D -4 i -3 d -2 d -1 y charact er Accessing character(s):

variable [ index ] variable [ index1:index2 ] index2 is exclusive index1 or index2 can be omitted (end of string) >>> name = "P. Diddy" >>> name[0] 'P' >>> name[7] 'y' >>> name[-1] 'y' >>> name[3:6] 'Did' >>> name[3:] 'Diddy' >>> name[:-2]

'P. Did' INF424: GIS. Dr. Taysir Hassan A. Soliman String Methods Java Python length len(str) startsWith, endsWith startswith, endswith toLowerCase, toUpperCase

upper, lower, isupper, islower, capitalize, swapcase indexOf find trim strip >>> name = Jordan Hiroshi Nakamura" >>> name.upper() 'JORDAN HIROSHI NAKAMURA' >>> name.lower().startswith(jordan") True >>> len(name)

23 INF424: GIS. Dr. Taysir Hassan A. Soliman for Loops and Strings A for loop can examine each character in a string in order. for name in string: statements >>> for c in "booyah": ... print(c) ... b o o y a h

INF424: GIS. Dr. Taysir Hassan A. Soliman input input : Reads a string from the user's keyboard. reads and returns an entire line of input >>> name = input("What's your name? ") What's your name? Omar Abdel Aziz >>> name Omar Abdel Aziz' INF424: GIS. Dr. Taysir Hassan A. Soliman input for numbers to read a number, cast the result of input to an int Only numbers can be cast as ints! Example: age = int(input("How old are you? "))

print("Your age is", age) print("You have", 65 - age, "years until retirement) Output: How old are you? 53 Your age is 53 You have 12 years until retirement INF424: GIS. Dr. Taysir Hassan A. Soliman if if condition: statements Example: gpa = input("What is your GPA? ") if gpa > 2.0: print("Your application is accepted.)

INF424: GIS. Dr. Taysir Hassan A. Soliman if/else if condition: statements elif condition: statements else: statements Example: gpa = input("What is your GPA? ") if gpa > 3.5: print("You have qualified for the honor roll.) elif gpa > 2.0: print("Welcome to Mars University!) else: print("Your application is denied.) INF424: GIS. Dr. Taysir Hassan A. Soliman

if ... in if value in sequence: statements The sequence can be a range, string, tuple, or list Examples: x = 3 if x in range(0, 10): print("x is between 0 and 9) name = input("What is your name? ") name = name.lower() if name[0] in "aeiou": print("Your name starts with a vowel!) INF424: GIS. Dr. Taysir Hassan A. Soliman Logical Operators Operator

Meaning Example Result == equals 1 + 1 == 2 True != does not equal

3.2 != 2.5 True < less than 10 < 5 False > greater than 10 > 5

True <= less than or equal to 126 <= 100 False >= greater than or equal to 5.0 >= 5.0 True

Operator and or not Example (2 == 3) and (-1 < 5) (2 == 3) or (-1 < 5) not (2 == 3) INF424: GIS. Dr. Taysir Hassan A. Soliman Result False True

True while Loops while test: statements >>> n = 91 >>> factor = 2 # find first factor of n >>> while n % factor != 0: ... factor += 1 ... >>> factor 7

INF424: GIS. Dr. Taysir Hassan A. Soliman while / else while test: statements else: statements Executes the else part if the loop does not enter There is also a similar for / else statement >>> n = 91 >>> while n % 2 == 1: ... n += 1 ... else: ... print n, "was even; no loop." ... 92 was even; no loop.

INF424: GIS. Dr. Taysir Hassan A. Soliman Random Numbers from random import * randint(min, max) returns a random integer in range [min, max] inclusive choice(sequence) returns a randomly chosen value from the given sequence >>> from random import * the sequence can be a 5) range, a string, ... >>> randint(1, 2

>>> randint(1, 5) 5 >>> choice(range(4, 20, 2)) 16 >>> choice("hello") 'e' INF424: GIS. Dr. Taysir Hassan A. Soliman Pass pass is a special keyword in Python that does nothing. It can be used to fill a control block or a function or a class definition. >> while True: # Do something pass INF424: GIS. Dr. Taysir Hassan A. Soliman

Importing import sys a = sys.maxint print a b = a*2 print b INF424: GIS. Dr. Taysir Hassan A. Soliman Lists a = [1, 2, 3] a = [1, 'a', [2, 'b']] a = [1, 2, 3] print a[0] print a[0:1] print a[-1] INF424: GIS. Dr. Taysir Hassan A. Soliman GeoProcessing with Python 1. Every script using Arc functions must start with import arcpy

import arcpy 2. Get user inputs with GetParameterAsText '''script.py 1 ''' 2 import arcpy 3 4 # get input feature class 5 inputFeatureClass = 6 arcpy.GetParameterAsText(0) This gets the first user entered parameter as text. INF424: GIS. Dr. Taysir Hassan A. Soliman INF424: GIS. Dr. Taysir Hassan A. Soliman

Matplotlib.pyplot matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc. In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes (please note that axes here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis). INF424: GIS. Dr. Taysir Hassan A. Soliman

import matplotlib.pyplot as plt plt.plot([1,2,3,4]) plt.ylabel('some numbers') plt.show() INF424: GIS. Dr. Taysir Hassan A. Soliman For every x, y pair of arguments, there is an optional third argument which is the format string that indicates the color and line type of the plot. The letters and symbols of the format string are from MATLAB, and you concatenate a color string with a line style string. The default format string is b-, which is a solid blue line. For example, to plot the above with red circles, you would issue INF424: GIS. Dr. Taysir Hassan A. Soliman

import matplotlib.pyplot as plt plt.plot([1,2,3,4], [1,4,9,16], 'ro') plt.axis([0, 6, 0, 20]) plt.show() INF424: GIS. Dr. Taysir Hassan A. Soliman You can add text() The text() command can be used to add text in an arbitrary location, and the xlabel(), ylabel() and title() are used to add text in the indicated locations INF424: GIS. Dr. Taysir Hassan A. Soliman import numpy as np import matplotlib.pyplot as plt mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)

# the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() INF424: GIS. Dr. Taysir Hassan A. Soliman References http:// pro.arcgis.com/en/pro-app/tool-reference/an alysis/clip.htm http:// pro.arcgis.com/en/pro-app/arcpy/geoprocessi

ng_and_python/using-functions-in-python.ht m https:// www.e-education.psu.edu/geog485/node/162 http://resources.arcgis.com/en/help/main/ INF424: GIS. Dr. Taysir Hassan A. Soliman End of Lecture INF424: GIS. Dr. Taysir Hassan A. Soliman

Recently Viewed Presentations

  • Lecture 10: Floating Point, Digital Design  Todays topics:

    Lecture 10: Floating Point, Digital Design Todays topics:

    1 1000 0001 01000…0000 -5.0 * Details The number "0" has a special code so that the implicit 1 does not get added: the code is all 0s (it may seem that this takes up the representation for 1.0, but...
  • Additional Title Text Enhancing Customer Collaboration With QADs

    Additional Title Text Enhancing Customer Collaboration With QADs

    Non-critical - mail alert. Critical - mobile alert. Communications channel. Monitored every 5 minutes from the outside. On failure is placed on retry. After a period routed through mobile alert. Service Security & Availability (II)
  • What is trauma and What is dissociation?

    What is trauma and What is dissociation?

    That is, the kinds of feelings and experiences the child can bring into interactions with the parent are shaped implicitly from the beginning of life in the parent's responses, or nonresponses, to the infant's uncomfortable, distressed, or frightened reactions, as...
  • MILLER/SPOOLMAN LIVING IN THE ENVIRONMENT 17TH Chapter 5

    MILLER/SPOOLMAN LIVING IN THE ENVIRONMENT 17TH Chapter 5

    This diagram shows the undisturbed secondary ecological succession of plant communities on an abandoned farm field in the U.S. state of North Carolina. It took 150-200 years after the farmland was abandoned for the area to become covered with a...
  • Introduction and Overview - University of California, Irvine

    Introduction and Overview - University of California, Irvine

    Z: zero flag (bit 6). Is 1 if the last operation result is zero. S: sign flag (bit 7). It is set to 1 if the most significant bit of the last operation is 1. T: trap flag (bit 8)....
  • NFHS Volleyball Rule Changes 2013-2014 Tina Spann Northeast

    NFHS Volleyball Rule Changes 2013-2014 Tina Spann Northeast

    NFHS Volleyball Rule Changes 2013-2014 Tina Spann Northeast Volleyball Officials' Association Rule Changes Rule 4-1-4: Deletes reference that a towel tucked in a player's uniform waistband is an automatic violation. Rationale: The wearing of a towel is not always a...
  • Monday Warm-up Copy the quote and write 2

    Monday Warm-up Copy the quote and write 2

    What has been your favorite activity/assignment/lesson we have done in AVID 1st quarter? How do you feel AVID has helped you become a better student? Do you feel like you've given your best effort toward school this quarter? Why or...
  • Logging in to Enhanced WebAssign Enhanced WebAssign Allows

    Logging in to Enhanced WebAssign Enhanced WebAssign Allows

    Visit the College Bookstore, or Visit www.CengageBrain.com Choose the format and price options that work best for you. CengageBrain.com offers Textbooks Value Bundles including EWA access Rental options Pay for Enhanced WebAssign access online directly from your course. If You...