Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8: Python Writing Scripts in GIS

Similar presentations


Presentation on theme: "Lecture 8: Python Writing Scripts in GIS"— Presentation transcript:

1 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

2 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

3 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

4 INF424: GIS. Dr. Taysir Hassan A. Soliman
Open source general-purpose language. Object Oriented, Procedural, Functional Easy to interface with C/Java Downloads: Documentation: INF424: GIS. Dr. Taysir Hassan A. Soliman

5 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

6 INF424: GIS. Dr. Taysir Hassan A. Soliman

7 INF424: GIS. Dr. Taysir Hassan A. Soliman

8 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

9 INF424: GIS. Dr. Taysir Hassan A. Soliman
What is a script? INF424: GIS. Dr. Taysir Hassan A. Soliman

10 INF424: GIS. Dr. Taysir Hassan A. Soliman

11 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

12 INF424: GIS. Dr. Taysir Hassan A. Soliman
Installing Python Windows: Download Python from 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

13 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

14 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

15 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

16 INF424: GIS. Dr. Taysir Hassan A. Soliman
A Brief Review INF424: GIS. Dr. Taysir Hassan A. Soliman

17 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

18 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

19 INF424: GIS. Dr. Taysir Hassan A. Soliman
Functions Function: Equivalent to a static method in Java. Syntax: def name(): statement ... Must be declared above the 'main' code Statements inside the function must be indented hello2.py 1 2 3 4 5 6 7 # Prints a helpful message. def hello(): print("Hello, world!”) # main (calls hello twice) hello() INF424: GIS. Dr. Taysir Hassan A. Soliman

20 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() INF424: GIS. Dr. Taysir Hassan A. Soliman

21 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

22 INF424: GIS. Dr. Taysir Hassan A. Soliman
Exercise Solution def egg(): top() bottom() print def cup(): line() def stop(): print("| STOP |”) def hat(): def top(): print(" ______”) print(" / \\”) print("/ \\”) def bottom(): print("\\ /”) print(" \\______/”) def line(): print(" ”) # main egg() cup() stop() hat() INF424: GIS. Dr. Taysir Hassan A. Soliman

23 INF424: GIS. Dr. Taysir Hassan A. Soliman
Module 2 expressions, variables, for loops INF424: GIS. Dr. Taysir Hassan A. Soliman

24 INF424: GIS. Dr. Taysir Hassan A. Soliman
Expressions Arithmetic is very similar to Java Operators: + - * / % (plus ** for exponentiation) Precedence: () before ** before * / % before + - Integers vs. real numbers You may use // for integer division >>> 1 + 1 2 >>> * 4 - 2 11 >>> 7 // 2 3 >>> 7 / 2 3.5 >>> 7.0 / 2 INF424: GIS. Dr. Taysir Hassan A. Soliman

25 INF424: GIS. Dr. Taysir Hassan A. Soliman
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 = x * 8; double d = 3.2; d = d / 2; System.out.println(d); x = 2 x = x + 1 print(x) x = x * 8 d = 3.2 d = d / 2 print(d) INF424: GIS. Dr. Taysir Hassan A. Soliman

26 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

27 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 3.14 double float "ni!" String str INF424: GIS. Dr. Taysir Hassan A. Soliman

28 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

29 INF424: GIS. Dr. Taysir Hassan A. Soliman
>>fruit = “banana” >> letter = fruit[1] >> Print(letter) >> a INF424: GIS. Dr. Taysir Hassan A. Soliman

30 INF424: GIS. Dr. Taysir Hassan A. Soliman

31 INF424: GIS. Dr. Taysir Hassan A. Soliman
Control Structures INF424: GIS. Dr. Taysir Hassan A. Soliman

32 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) 1 2 3 4 INF424: GIS. Dr. Taysir Hassan A. Soliman

33 INF424: GIS. Dr. Taysir Hassan A. Soliman
for Loop Variations for name in range(min, max): statements for name in range(min, max, step): 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): 15 10 INF424: GIS. Dr. Taysir Hassan A. Soliman

34 INF424: GIS. Dr. Taysir Hassan A. Soliman
Nested Loops Nested loops are often replaced by string * and + ....1 ...2 ..3 .4 5 Java 1 2 3 4 5 6 for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (5 - line); j++) { System.out.print("."); } System.out.println(line); Python 1 2 for line in range(1, 6): print((5 - line) * "." + str(line)) INF424: GIS. Dr. Taysir Hassan A. Soliman

35 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 2 3 4 5 6 7 8 9 10 11 12 13 MAX_VALUE = 3 def printTop(): for i in range(MAX_VALUE): for j in range(i): print(j) print() def printBottom(): for i in range(MAX_VALUE, 0, -1): for j in range(i, 0, -1): print(MAX_VALUE) INF424: GIS. Dr. Taysir Hassan A. Soliman

36 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

37 INF424: GIS. Dr. Taysir Hassan A. Soliman
Exercise INF424: GIS. Dr. Taysir Hassan A. Soliman

38 INF424: GIS. Dr. Taysir Hassan A. Soliman
Exercise Solution SIZE = 4 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, -1): # main bar() top() bottom() INF424: GIS. Dr. Taysir Hassan A. Soliman

39 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) 1 2 3 10 9 8 INF424: GIS. Dr. Taysir Hassan A. Soliman

40 INF424: GIS. Dr. Taysir Hassan A. Soliman
Exercise Solution 2 SIZE = 4 def bar(): print "#" + 4 * SIZE * "=" + "#" def mirror(): for line in list(range(1, SIZE + 1)) + list(range(SIZE, 0, -1)): print("|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|“) # main bar() mirror() INF424: GIS. Dr. Taysir Hassan A. Soliman

41 INF424: GIS. Dr. Taysir Hassan A. Soliman
Strings index 1 2 3 4 5 6 7 or -8 -7 -6 -5 -4 -3 -2 -1 character P . D i d y 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] >>> name[3:6] 'Did' >>> name[3:] 'Diddy' >>> name[:-2] 'P. Did' INF424: GIS. Dr. Taysir Hassan A. Soliman

42 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

43 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 y a h INF424: GIS. Dr. Taysir Hassan A. Soliman

44 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

45 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

46 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

47 INF424: GIS. Dr. Taysir Hassan A. Soliman
if/else if condition: statements elif condition: else: 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!“) print("Your application is denied.“) INF424: GIS. Dr. Taysir Hassan A. Soliman

48 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 48

49 Logical Operators Operator Meaning Example Result == equals 1 + 1 == 2
True != does not equal 3.2 != 2.5 < less than 10 < 5 False > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 Operator Example Result and (2 == 3) and (-1 < 5) False or (2 == 3) or (-1 < 5) True not not (2 == 3) INF424: GIS. Dr. Taysir Hassan A. Soliman

50 INF424: GIS. Dr. Taysir Hassan A. Soliman
while Loops while test: statements >>> n = 91 >>> factor = # find first factor of n >>> while n % factor != 0: factor += 1 ... >>> factor 7 INF424: GIS. Dr. Taysir Hassan A. Soliman

51 INF424: GIS. Dr. Taysir Hassan A. Soliman
while / else while test: statements else: 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

52 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 the sequence can be a range, a string, ... >>> from random import * >>> randint(1, 5) 2 5 >>> choice(range(4, 20, 2)) 16 >>> choice("hello") 'e' INF424: GIS. Dr. Taysir Hassan A. Soliman

53 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

54 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

55 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

56 GeoProcessing with Python
1. Every script using Arc functions must start with import arcpy import arcpy 2. Get user inputs with ‘GetParameterAsText’ 1 2 3 4 5 6 '''script.py ''' import arcpy # get input feature class inputFeatureClass = arcpy.GetParameterAsText(0) This gets the first user entered parameter as text. INF424: GIS. Dr. Taysir Hassan A. Soliman

57 INF424: GIS. Dr. Taysir Hassan A. Soliman

58 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

59 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

60 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

61 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

62 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

63 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

64 INF424: GIS. Dr. Taysir Hassan A. Soliman
References INF424: GIS. Dr. Taysir Hassan A. Soliman

65 INF424: GIS. Dr. Taysir Hassan A. Soliman
End of Lecture INF424: GIS. Dr. Taysir Hassan A. Soliman


Download ppt "Lecture 8: Python Writing Scripts in GIS"

Similar presentations


Ads by Google