Spatial Computations 100 Lecture Two, Second Semester 2015
Dr David A. McMeekin
image: https://www.flickr.com/photos/sprengben/!
laugh.learn.love.live
SC 100 - Unit Learning Outcomes • Design solutions to complex spatial problems;
• Apply knowledge of structured programming concepts to computer program design;
• Create algorithms and flowcharts to document solutions for geometric and topological problems;
• Develop and implement programming solutions in structured code; and
• Evaluate the structure and logic of computer programs and apply debugging.
2!
laugh.learn.love.live
Binary Conversion Review
• Convert 12610 to a binary number:
• 126 ÷ 2 = 63, r 0 !• 63 ÷ 2 = 31, r 1 !• 31 ÷ 2 = 15, r 1 !• 15 ÷ 2 = 7, r 1 !• 7 ÷ 2 = 3, r 1 !• 3 ÷ 2 = 1, r 1 !• 1 ÷ 2 = 0, r 1 !!
• The binary is: 1111110 !
3!
laugh.learn.love.live
Our First Program - Pseudocode
• What needs to be done?
• display “Hello World”
• Algorithm:
START:
display Hello World
END.
6!
laugh.learn.love.live
Another Program - Pseudocode
• What needs to be done?
• sum 2 numbers together and print out the answer.
• Algorithm:
START:
sum the two numbers display the sum
END.
9!
laugh.learn.love.live
Another Program - Pseudocode
• What needs to be done?
• sum 2 numbers together and print out the answer.
• Algorithm:
START:
x = 5 y = 6
display x + y
END.
12!
laugh.learn.love.live
Our First Program - Python • The Python Code:
13!
def main(): !!x = 5 !
y = 6 ! print x + y !!main() !
• The Output:
11 !
laugh.learn.love.live
Programming Languages
• Computers understand binary
• ie. 0 & 1’s
• instructions need to be given in this machine form: machine code
• Programming in binary is difficult at best for humans!
• Compilers (and interpreters) are used to translate programmers' source code to machine format
15!
laugh.learn.love.live
Levels of Programming Languages
• 5th generation
• Problem solving / deductive
languages
• 4th generation
• natural and non-procedural
statements
• e.g. database languages such as
SQL
16!
• High level languages:
• English-like statements and arithmetic notation
• e.g. BASIC, COBOL, Fortran, C, Pascal, Java...
• Assembly languages
• symbolically coded instructions
• Machine languages
• machine code (binary)
laugh.learn.love.live
Making Code Machine Readable
• Programmers use a set of statements describing actions
• The program statements you write are called source code
• Source code must be converted to machine code in order to run
• Conversion is done either by an interpreter or a compiler
17!
laugh.learn.love.live
Compiler and Interpreter
• Interpreter:
• Converts source code line by line as the program runs
• Compiler:
• Source code statements are compiled, linked and stored as machine code
• giving an executable file
• Executables can run on a computer with no compiler
18!
laugh.learn.love.live
Programming and Languages
• Main streams, or types:
• Procedural
• Event driven
• Declarative
• e.g. SQL and Prolog
19!
laugh.learn.love.live
Procedural Programming
• Traditional style
• Each step is executed in sequence
• E.g. always from A->Z or line 1 to 100
• Sequences can be repeated
• Many scripts (e.g. ArcGIS, Matlab) use procedural programming!
• Single start point
• Reaches a defined end point
• May contain several possible paths!
• A bit like a cooking recipe
20!
laugh.learn.love.live
Event Driven Programming
• Windows-style programs - Using forms for user input
• Mouse click etc executes an event-procedure
• Multiple sequences of code execution
• Event-procedure to be executed depends on user;
• code still executes top to bottom within a procedure.
• Many start points - from A-->Z, but also from A,G,B,Z etc.
• An event driven program doesn't know what will happen next!
21!
laugh.learn.love.live
Object Oriented Programming
• Event driven programming is well suited to an object oriented (OO) environment
• Object
• Code that represents a 'thing', e.g. printer, map, numbat, GPS reading, …
• Each has properties (states or variables) and methods (actions that it can do)
• Class – datatype for an object
22!
laugh.learn.love.live
Software Development
23! Agile-Software-Development-Poster-En.pdf: The original uploader was Dbenson at English Wikipedia and VersionOne, Inc. *derivative work: Devon Fyson
laugh.learn.love.live
Agile Software Development
• Understand the problem that needs to be solved
• Break the problem down into small solvable problems
• Design and implement a solution to first problem
• Design and implement a solution to the next problem
• Keep going until everything is solved
24!https://www.flickr.com/photos/from_a_tester/!
laugh.learn.love.live
Software Development Step by Step
• Understand the problem
• Design a solution
• Test your solution
• Fix your solution
• Write and document the code
25!
https://www.flickr.com/photos/sybrenstuvel/!
laugh.learn.love.live
Python
• Developed in the late 1980’s (along with Duran Duran)
• Named after Monty Python’s Flying Circus
• It is an OpenSource development project
• Designed to make programs very readable
• Rich library to make things simple to do
26!
laugh.learn.love.live
Python 2 vs Python 3
• Currently 2 major versions of Python
• SC 100 uses Python 2.7.x
• The text book uses Python 3
• We will need to translate a little between the 2
• SC 100 uses Python 2.7.x as ArcGIS currently uses 2.7.x
27!
laugh.learn.love.live
Example • Design a process to find the straight-line distance between two points
• Break problem into smaller pieces:
• _________________;
• _________________;
• _________________;
• _________________;
• … 28!
laugh.learn.love.live
Algorithm Design
• Algorithms are key to solving the heart of the problem:
• simple problems, simple programs, simple algorithms
• Complex problems, complex solutions
• interface design
• data structure design and creation...
30!
laugh.learn.love.live
Solution Design & Document Tools
• Sketches and text descriptions
• Flowcharts:
• Overviews the complete solution
• Created early in the design
• Pseudocode:
• Simply step by step instructions in plain English
• Has more details than flowcharts
• More general than programming languages
• Keeps it simple and in plain English
33!
laugh.learn.love.live
Flowchart Example
• Read in the cake inputs
• Cook the cake
• Display the cake
35!
{
{ {
laugh.learn.love.live
Pseudocode
• Bridges between human and computer program language
• Code like statements
• For the human, not for the computer
• It is not source code
• Uses a small set of action words
• set, get, print, calculate etc.
• No standard way of writing it:
• be distinct from computer programming languages
• be consistent within the entire design
37!
laugh.learn.love.live
Pseudocode Example One • Problem: find the distance between two points:
• INPUTS: none
• OUTPUTS: displaying distance
• BEGIN READ eastingOne, northingOne !READ eastingTwo, northingTwo !eastingDifference = eastingTwo - eastingOne !northingDifference = northingTwo - northingOne !distance = eastingDifference2 + northingDifference2 !
distance = square Root of distance !
PRINT distance
• END 40!
Variables eastingOne, northingOne: point one coordinates
eastingTwo, northingTwo: point two coordinates
eastingDifference: difference in easting
northingDifference: difference in northing
distance: distance between the two points
laugh.learn.love.live
Pseudocode Example Two
• Problem: find the distance between two points:
• INPUTS: eastingTWo, northingOne, eastingOne, northingTwo
• OUTPUTS: distance
• BEGIN eastingDifference = eastingTwo - eastingOne !northingDifference = northingTwo - northingOne !distance = eastingDifference2 + northingDifference2 !distance = square Root of distance !
RETURN distance
• END
41!
Variables eastingDifference: difference in easting northingDifference: difference in northing
distance: distance between the two points
laugh.learn.love.live
Variables
• Hold values
• A variable name is a label that refers to a unique memory location
• Make your variable names:
• meaningful
• purpose should be obvious from name
• manageable
• not too long
• consistent
• sum, total, num
42!
laugh.learn.love.live
Naming Variables • A program with invalid variable names will not execute
• Variable names can start with underscores or letters
• Variable names can NOT start with anything else
• Valid variable names:
• _david, david, DAVID3, david_3
• Invalid variable names:
• 3david, david%, #$@% !!
• Variable names can NOT be reserved words such as:
• if, do, while... !43!
laugh.learn.love.live
SC100 Naming Convention
• PEP8 naming convention will be used.
• https://www.python.org/dev/peps/pep-0008/
• david_mcmeekin, min_temp, max_temp, low_speed !
44!
laugh.learn.love.live
Variables & Memory
• An assigned variable points to a region of memory
• Change it and it's gone!
• Be very careful when swapping values of two variables
• This action is used often, e.g. in sorting
str1 = "Hi" !
45!
laugh.learn.love.live
Swapping Values
• This does not work
x = 5, y = 10 !x = y !y = x !
46!
x y 5 10
x y temp 5 10
x y temp 10 5 5
• This works!
x = 5, y = 10 !temp = x !x = y !y = temp !
laugh.learn.love.live
Numbers in Python
• Integers: positive and negative whole numbers
• Floating point numbers: numbers with a fractional part
• Integers can be written using hexadecimal, octal and binary literals
• Integers have unlimited precision (until memory runs out)
47!
laugh.learn.love.live
Assigning Text or Numerical Values
• Use the assignment operator (=)
• statement = "Hello World" !• fahrenheit = 32 !!
• Can also use the concatenation operator to join strings (+):
• new_string = string1 + string2 !• new_string = string1 + " text" !
• Similar operator precedence rules to maths:
• If in doubt use parentheses: fahrenheit = celsius * 9/5 + 32 !fahrenheit = (celsius * 9/5) + 32!
48!
laugh.learn.love.live
Output to the User • Python uses the print command/function to display output to the user:
print “Hello World!” !Hello World! !
!print(“Hello World!”) !Hello World!!!print 3 + 5 !8 !!print “3 + 5” !3 + 5 !
49!
laugh.learn.love.live
Input from the User
• Python uses the raw_input() command to receive input from the user:
name = raw_input(“What is your name: ”) !What is your name: David !
!• The raw_input() function receives all input as a string.
distance = raw_input(“How far was it: ”) !How far was it: 55 !
!• The variable distance contains a string NOT a numeric value.
• Strings can NOT be used in numeric calculations, they will cause an error.
50!
laugh.learn.love.live
Convert String to Numeric Value • To convert a string to a numeric value the function int() or float()
must be used.
• int() converts the string to an integer value;
• float() converts the string to a double/floating point number.
distance = raw_input(“How far was it: ”) !How far was it: 55!numeric_distance = int(distance) !print numeric_distance !55 !
• numeric_distance now holds an integer.
• Using the float() function would convert it to a double. 51!
laugh.learn.love.live
Basic Maths Operators in Python
53!
Operator Description Example (assume that: a =10, b = 20)
+ Addition - Adds values on either side of the operator a + b will give 30
- Subtraction - Subtracts right hand operand from left hand operand a - b will give -10
* Multiplication - Multiplies values on either side of the operator a * b will give 200
/ Division - Divides left hand operand by right hand operand b / a will give 2
% Modulus - Divides left hand operand by right hand operand and returns remainder b % a will give 0
** Exponent - Performs exponential (power) calculation on operators a**b will give 10 to the power 20
// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.
9//2 is equal to 4 and 9.0//2.0 is equal to 4.0
laugh.learn.love.live
Maths Operator Precedence
54!
Operator Description
** Exponentiation (raise to the power)
~ + - Compliment, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= ** Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
laugh.learn.love.live
Math Library • Python has an extensive maths library.
• To use the math library it must be imported into your Python programs.
• import math is written at the top of your programs.
55!
math.ceil() math.atan() math.acosh()
math.fabs() math.atan2() math.asinh()
math.floor() math.cos() math.atanh()
math.pow() math.sin() math.cosh()
math.sqrt() math.tan() math.sinh()
math.acos() math.degrees() math.tanh()
math.asin() math.radians() math.pi()