+ All Categories
Home > Documents > Getting started with programming For geospatial data analysis

Getting started with programming For geospatial data analysis

Date post: 28-Mar-2022
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
34
Getting started with programming For geospatial data analysis Robert Hijmans UC Davis Crash course for the CSI Cali, 22 Sept 2017
Transcript
Slide 1Robert Hijmans UC Davis
- Scalability - Repeatability - Documentation - Innovation - Speed
Computer languages
• Multi-dimensional arrays (vector, matrix, …)
• Arithmetic
• Variables
int distkm;
CaliLon = -76.53;
CaliLat = 3.45;
BogLon = -74.07;
BogLat = 4.71;
C++
double latB, double r=6378137) {
double toRad = M_PI / 180;
* cos(latB) * cos(lonA - lonB)) * r;
}
toRad = math.pi / 180
lonA = lonA * toRad
lonB = lonB * toRad
latA = latA * toRad
latB = latB * toRad
distance = math.acos(math.sin(latA) * math.sin(latB) +
distkm = int(round(distkm))
Python
toRad <- pi / 180;
lonA <- lonA * toRad;
lonB <- lonB * toRad;
latA <- latA * toRad;
latB <- latB * toRad;
distkm
R
bio <- crop(bio, col)
biocol <- mask(bio, col)
– R – Data & model focused
http://www.rspatial.org/
Python
- General purpose - High level - Emphasizes code readability - Free and open source interpreter - Easy to learn (by design) - Widely used ($$$)
for stand alone programs, scripts, web-sites
Based on presentation from www.cis.upenn.edu/~cse391/cse391_2004/PythonIntro1.ppt
Some code ….
x = 34
y = “Hello” # Another one.
x = x + 1
print x
print y
Basic Datatypes
• Floats
Unmatched quotes can occur within the string. “matt’s”
Based on presentation from www.cis.upenn.edu/~cse391/cse391_2004/PythonIntro1.ppt
Whitespace
– Use a newline to end a line of code. (Use \ when must go to next line prematurely.)
– Use consistent indentation. The first line with a new indentation is considered outside of the block.
– Often a colon appears at the start of a new block.
Based on presentation from www.cis.upenn.edu/~cse391/cse391_2004/PythonIntro1.ppt
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 string.
y = 23 # Decides y is integer.
print x + y # Python will complain about this.
print x + str(y)
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
But use a good style...
• 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
Accessing Non-existent Name
File "<pyshell#16>", line 1, in -toplevel-
y
>>> y = 3
Multiple Assignment
a[1:4] "ell" # slicing
Based on Introduction to Python by Guido van Rossem: www.python.org/doc/essays/ppt/lwnyc2002/intro22.ppt
Based on presentation from www.cis.upenn.edu/~cse391/cse391_2004/PythonIntro1.ppt
Functions and Methods Function
Print
abc xyz 34
The % string operator in combination with the print command to format output text.
>>> print “%s xyz %d” % (“abc”, 34)
abc xyz 34
Same operators as for strings
a+b, a*3, a[0], a[-1], a[1:], len(a)
Item and slice assignment
print a
del a[-1]
>>> a = range(5) # [0,1,2,3,4]
– Compute the distance from Cali to Cartagena
– Write a program that can compute the distance from Cali to any other place
– Check for bad input data (impossible coordiantes)
– Read input from a file
– Write output to a file

Recommended