This week’s lab will focus on building a foundation in scripting skills using python.
Before we can start programming we need to set up our development environment, the development environment consists of 3 main components: a programming language, a text editor, and libraries.
Our Programming language is Python 3, this has already been installed by default in your virtual machine.
Text editors range from simple (Note pad, gedit), code hinting (notepad++, visual studio code) all the way to Integrated Development Environments (IDEs) such as PyCharm. The choice of a text editor is based primarily on personal preference. For this course we will use Visual Studio Code.
And libraries, are pre-written pieces of code that we can use to perform common operations as opposed to writing all our own code every-time, packages are installed with a package manager such as PIP or Anaconda (Think of it like apt but for pieces of code instead of entire programs).
Lets write our first Python program, open visual studio code
go to file >>> new, and enter the following text into your document
Notice that this looks like normal text. Now save the file somewhere in your directory as hello.py. What happened to your text and why might this be useful?
You may notices some po-pups in the corner offering to install plug-in, go ahead and install they will add even more code highlighting specific to python, the same way your word processor underlines mistakes, visual studio code will highlight invalid code and help you to find typo’s faster.
If everything has gone according to plan you should be able to press [Ctrl] + [F5] and a terminal should show up and show the words ‘Hello World’. Note that this is a feature specific to this text editor (and most IDE’s), if you were using a simple text editor such as notepad or nano you would run your program with the following command.
Python allows us to set variables to remember information that we will use in loops.
We can assign variables by naming the variable and using a ‘=’ to assign something to it
text = "Hello World" print(text)
Will print Hello world to the console. We can also mutate (change) variables value as we process.
text = "Hello" text = text + " World" print(text)
The basic data type that you will see are
integer = 123 float = 1.23 text = "Text" character = 'a' boolean = True
All other variables are constructed from these basic types.
We can also define an array (list), using square brackets, for example
colours = ["Red", "Green", "Blue"]
And we can then look up individual values
This will print Green to the console.
Python is a zero-indexed language, what this means is that when we count we start at 0. Note that not all languages are 0 indexed, languages like Matlab for example are 1 indexed.
Python has a cool feature, if you what the last item in the list but don’t know how long the list is variable[-1] always returns the last value, and variable[-2] the 2nd last variable.
Packages and Imports
Lets start by adding some generic libraries useful for geospatial work: gdal, ogr, scipy and numpy.
pip3 install gdal ogr scipy numpy psycopg2
And then in visual studio code create a file in your GEOG-<username> folder and call it lab4.py
To gain access to the packages we just installed make the contents of your file be
""" Author: Date: Class: GEOG-413 Assignment: Lab4 """ import gdal # Raster base geospatial data import ogr # Vector based geospatial data import scipy # Scientific Tools import numpy # Tools for numerical analysis import psycopg2 # Interact with PostgreSQL Databases print("Import Complete! :)")
Notice in the above code that text inbetween tripple quotes is a paragraph comment, and text after a # is a single line comment. Always comment your code and explain what it is acomplishing. Future you with be greatful!
Now with a terminal cd’d to your GEOG413-<username> folder you can run the command:
If all of your packages are installed correctly you should see a message that says Import Complete, if you get warnings we will pause here and get everyones python going.
When working with raster data we thing of it as a multi dimensional array, a Colour photo is X pixels wide, Y pixels tall, and make of L layers (3-4 is typical R,G,B, and maybe Alpha, but in GIS this will vary greatly ie DEM has only one layer (grayscale), landsat data will have more (multi-spectral) This is done by using multiple sets of brackets.
To illustrate it, lets load some data. You should already have some geotiffs in ~/data/assignment1/originals. Lets open one of these photos
raster = gdal.Open( "/home/matt/data/assignment1/originals/513559864.tif" ) red = raster.GetRasterBand(1).ReadAsArray() green = raster.GetRasterBand(2).ReadAsArray() blue = raster.GetRasterBand(3).ReadAsArray() BW = red/3 + green/3 + blue/3 # Create Black and White Image # Average of bands print(numpy.average(red)) print(numpy.average(green)) print(numpy.average(blue)) print(numpy.average(BW))
At this point we can do any sort of match or statistics we want on the raster bands. in this case an RGB image is a lot more Remote Sensing than GIS. However what you may be more familiar with is classification rasters. This is the starting foundation for determining how much area is in a given class.
A website you will want to become familiar with is the GDAL/OGR Cookbook https://pcjericks.github.io/py-gdalogr-cookbook/index.html
Python gives us a big advantage over bash in that we can pick apart the data into smaller peices easier. For example if you wanted to extract the projection, instead of using a confusing awk command to search the metadata we could simply use the line:
projection = raster.GetProjection() print(projection)
Control structures are used to allow us to do more than one thing at a time, and come in the forms: if else, for, while, and do while.
We use if else blocks to decide if we want to execute code it
age = 19 if age >= 19 print("You may order a beer") else print("You may not order a beer")
Here is a simple line of code to check the age variable and respond accordingly.
Python uses TAB’s to group code together think of the first indented line being a (, and the last indented line at the same level as ).
If statements resolve as boolean values, some possible operations are >, <, >=, >=, and ==. Note the double ‘=’, one equal says set a to b, two equals says is a equal to b.
You can also mathematically resolve boolean statements where 0 is false, and non 0 is true.
The bang operator! In programming the exclamation point is known as a bang, and can be used to get the compliment (opposite) of boolean expressions. For example a != b, is true when a is not equal to b.
The else statement is optional.
A final not on this is that we can combine checks by using the logical operations: OR as well as AND