Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The...

17
Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica. The first half of the tutorial introduces basic Python concepts while the second half shows how Python can be used with Geomatica. Additional information about Python, including complete lists of built-in functions, data types and operators can be found at https://docs.Python.org/2/library/functions.html. The scripts that are created in this tutorial can be found on PCI Geomatics’ Github page (https://github.com/PCIGeomatics). Python Basics Before beginning to script in Python for Geomatica, there are basic concepts that must be understood. Similar to any programming language Python has specific syntax. Indentation is important in Python and indicates when a certain line in the script will be run. If you are using notepad ++ go to Settings Preferences… Tab Settings Tab Size check mark Replace by space. In the following examples make sure to note how the lines are indented. Data Types and Operators Python includes the following data types that can be used within a script: Data Type Description Example Boolean True or False value that is produced from a certain operation. Integer Whole number which can be positive or negative. - to 5, -25, 2000, -124 Float Any number, including numbers with decimals. 1.2565, -10.597 String Collection of numbers or characters. “Hello”, “175 Broadway Ave” List A non-fixed number of elements in a collection. You can add (append) or remove elements from a list after it is defined. [1, 2, 3, 4] Tuple A fixed number of elements in a collection. You cannot add or remove values from a tuple after it is defined. (1, 2, 3, 4) These data types are used when defining variables in Python. Variables are defined in code to keep certain values in “memory”. These values can be called upon later by using the variable name. To practice defining variables and viewing the outcome of operations, open the Python command line (Start All Programs Python 2.7 Python (command line)). 1. In the command line enter A=25 and hit enter. You have just defined the variable A as 25. Page | 1 The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Transcript of Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The...

Page 1: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

The purpose of this tutorial is to provide an introduction to Python for use in Geomatica. The first half of the tutorial introduces basic Python concepts while the second half shows how Python can be used with Geomatica. Additional information about Python, including complete lists of built-in functions, data types and operators can be found at https://docs.Python.org/2/library/functions.html. The scripts that are created in this tutorial can be found on PCI Geomatics’ Github page (https://github.com/PCIGeomatics).

Python Basics Before beginning to script in Python for Geomatica, there are basic concepts that must be understood. Similar to any programming language Python has specific syntax. Indentation is important in Python and indicates when a certain line in the script will be run. If you are using notepad ++ go to Settings Preferences… Tab Settings Tab Size check mark Replace by space. In the following examples make sure to note how the lines are indented.

Data Types and Operators Python includes the following data types that can be used within a script:

Data Type Description Example

Boolean True or False value that is produced from a certain operation.

Integer Whole number which can be positive or negative. - ∞ to ∞ 5, -25, 2000, -124

Float Any number, including numbers with decimals. 1.2565, -10.597

String Collection of numbers or characters. “Hello”, “175 Broadway Ave”

List

A non-fixed number of elements in a collection.

You can add (append) or remove elements from a list after it is defined.

[1, 2, 3, 4]

Tuple A fixed number of elements in a collection.

You cannot add or remove values from a tuple after it is defined.

(1, 2, 3, 4)

These data types are used when defining variables in Python. Variables are defined in code to keep certain values in “memory”. These values can be called upon later by using the variable name. To practice defining variables and viewing the outcome of operations, open the Python command line (Start All Programs Python 2.7 Python (command line)).

1. In the command line enter A=25 and hit enter. You have just defined the variable A as 25.

Page | 1

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 2: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

1. Now type B=50 and hit enter. You have now defined two variables.

2. You can now add these two variables using the + operator. Enter A+B and hit enter.

There are various operators that can be used in Python scripts: Operator Description Example

+ Add two values 4+2 gives you 6

“Hello” + “ World” gives you “Hello World”

- Subtract two numbers 4-2 gives you 2

* Multiply two numbers 4*2 gives you 8

** Determines the exponential value 10**2 gives you 100

/

Divide two numbers. Performs differently with two integers vs float numbers. If two integers are divided

an integer with be produced. If one number is a floating number a floating number is produced.

10/3 gives you 3

10.0/3 gives you 3.3333

// Divides two numbers but will not display the decimal value 10.0//3 gives you 3.0

% Remainder of two numbers 10%3 gives you 1

= States that the value of the right side is assigned to the left side variable A= 1+2

Page | 2

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 3: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

== Compares two variables. A Boolean value is produced 1==1 gives you True

Import * Indicates all items should be imported Import *

# Signifies that the line of code is a comment.

Comments are added to code to allow the reader to understand the purpose of the code.

# This is a comment and is not executed by Python

print Prints the value of what follows print 5 will give you 5

print 5+5 will give you 10

Lists Once the basics of Python are understood, more complicated scripts can be written. Understanding how lists work is important for creating scripts.

1. Open the Python command line. 2. Define a list: List1= [1, 2, 3, “a”, “b”, “happy”].

Lists are numbered starting at 0. In this example 1 is the 0th value, 2 is the 1st value and so on.

3. You can extract specific values from the list using List1 [value]. Ranges of values can also be

extracted using List1[n:m]. When ranges are used list items are extracted, starting at the nth item and continuing up to but not including the mth item. The range [2:4] will produce the 2nd and 3rd list items.

4. Add a value to the end of the list with List1.append(5). A 5 is added to the end of the list.

Page | 3

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 4: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Conditional Statements An important part of scripting is working with conditional statements and loops. An if statement is an example of a conditional statement. An if statement makes use of Boolean values to redirect the program to a specific part of the script.

1. In notepad++ type the example below. The type() function produces the data type of var1. In the example shown, if the data type of var1 is a string the if statement is true and the first line will be printed. If the if statement is false the else line will be printed. Note that the print statement is indented which means that it is part of the if statement. The first print statement is the result of the if statement and the second is the result of the else statement.

2. Save this file into a directory and name it WorkingInPython.py. 3. Open your command prompt window (Start type cmd). 4. Navigate to the directory where you stored WorkingInPython.py.

5. Click enter and on the next line type WorkingInPython.py. The line “Variable one is a string” will

appear. The script was executed and the if statement determined the variable type and chose the first option. If you change var1 to a number and run the script again the line “Variable one is not a string” will appear.

Page | 4

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 5: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Loops A loop allows a user to perform many commands automatically, saving both time and amount of code that must be written.

For Loops In the example below, the for statement goes through each item in the list and adds one to the count variable. It then prints out the count variable. Once all of the items in the list have been iterated through the program exits the loop.

1. Add the for loop to your file. Again you will notice that the count and print lines are tabbed. This means that they are executed within the loop. So for each value in letters the count and print lines are executed. Run the file in command line as you did with the if statement above and observe the output of the loop.

While Loops Another type of loop is a while loop. A while loop examines a statement. While the statement is true the loop will cycle. If the statement becomes false the loop will end. In the example below the loop continues until the length of the new list1 is no longer less than 4. On each iteration the nth value of letter is appended to list1. Each time the count increases, the next value in letter is added to list1. When all four values are added to list1 the list becomes longer than length 4 and the loop is exited. Once the loop is exited the program prints list1.

Page | 5

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 6: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Functions In Python functions are sections of code which take in arguments, perform a task and output information. Functions are often added into scripts to minimize the amount of coding. For example, if in your script you want to constantly perform a series of math problems and produce the final result you can create a function. Therefore every time you need to perform those math problems, you can call on the function you defined. The example below takes in three values, performs a math equation with those values and then produces the final result.

You can also define your own functions in Python. Functions must always begin with def function name (arguments): In the example below the function math has been defined. The arguments for math are x, y and z. When you want to run this function later within the script you have to give math three values (line 15 in the script). The math function executes four different math problems and then prints out the final result.

When you run math in command line the final result of math problems is printed.

In addition to defining your own functions, you can also use built-in functions. These work the same way as user-defined functions but you do not have to include the extra definition code in your script. The example shows the outputs of three built-in functions.

Page | 6

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 7: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Python in Geomatica Now that the basics of Python are understood you can start to create scripts that perform functions in Geomatica. The Geomatica Help provides various examples of how to call certain algorithms in your script. When looking up an algorithm in the Help simply select the Python environment option for examples.

You can open the Python files that correspond to each algorithm to see the full example script.

1. Navigate to C:\PCI Geomatics\Geomatica 2015\Examples\Python, right-click the fav.py file and choose edit in notepad ++. Make sure not to edit and save this file. Copy the code from this file into your working file to use it.

type() outputs the type of the given variable

len() outputs the length of the given variable. For a list the number of items is outputted. For a string the number of characters is outputted

min() outputs the minimum value of all inputted variables. For multiple strings the string that come s first alphabetically is outputted.

Page | 7

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 8: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

2. The example script for fav will open. These examples can be integrated into a larger script that follows

a specific workflow. When using algorithms in your scripts you must ensure that the variables are written in the exact same order as shown in the example scripts. All variables must be defined, even if you are choosing to leave them as the defaults. You can determine which values are required by looking in the help.

Setting Python Environment Variables If you have downloaded various versions of Python or different software that includes a Python instal you may wish to check environment variables to make sure you have the correct path. Geomatica requires Python 2.7 and if the environment path is switched to a different version you may have issues running the following scipts.

To check the environment variables open advanced system settings (Start right click Computer Properties on left hand panel click Advanced system settings). Open the environment variables by switching to the Advanced tab in the System Properties window. At the bottom will be a button for Environment Variables. In the system variables section scroll down to find the Path variable. Click on the Path variable and click Edit. In the Edit System Variable window there will be a Variable Value box that includes a long string of paths. Ensure that the path C:\Python27 is within this list. If this path is not present add it to the end of the list, making sure to separate it from the preceding path with a semi colon (;C:\Python27). Click OK on all open windows.

Page | 8

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 9: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Orthorectification Workflow This following example outlines how to complete an orthorectification workflow with a Python script. The steps to completing orthorecification are as follows:

Ingest Landsat 7 imagery

(CDLAND7)

Collect and refine GCPs (AUTOGCP2 & GCPREFIN)

Calculate model and orthorectify (SATMODEL & ORTHO2)

Import Libraries Before you can begin using the algorithms in your script you must first import them. In the example above the algorithm fav is imported from pci.fav. This line of code is accessing the fav.pyd file in the folder C:\PCI Geomatics\Geomatica 2015\exe\pci. The exceptions file must also be imported to ensure that the proper exceptions will appear when errors occur while running the script. For this tutorial we will be importing various algorithms to use in the script.

1. Using the form of from ___ import ___, import the algorithms cdland7, autogcp2, gcprefn, satmodel, and ortho2.

2. Add from pci.exception import * to ensure the exceptions are available.

Page | 9

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 10: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Set Data Paths When using files within Python scripts you want to define file variables at the beginning of your scripts. These variables hold the file path names so the entire pathname does not have to be written multiple times in the script. In this example four different files/folders will have to be specified: Input image, reference image, DEM file and output folder. The input file is required by cdland7 and the reference imagery and DEM file are inputs for AUTOGCP2. Output images from the algorithms are saved to the output folder.

1. Add a variable input_data and set it as the entire pathname for the raw header file (HDF) that is provided with the Landsat imagery: input_data= r”your header pathname”. Generally in Python a backslash (\) is an escape character which will move the rest of the string to the next line. Since we do not want this within our pathnames the r is added before the string. The r tells Python to ignore the backslashes.

2. Continue to add variables ref_image and dem_file in the same way as input_data. 3. Add output_dir and set it to the pathname of an output folder. This is the folder where the pix file

from cdland7 and the final ortho will be saved. Ensure to add a double \\ at the end of the pathname. This is added so that Python knows that there should be a \ added to the end of the folder name.

Page | 10

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 11: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Read Raw Landsat 7 Imagery The first step in the orthorecification process is to read in the required imagery. The ingested imagery will be saved to a PCIDSK file. The header file (HDF) will be read by CDLAND7.

1. The variable filehd is the input header file. Define this variable as input_data, defined in the data setup.

2. Define the variable in_data as output_dir + "ingest_landsat.pix". The + operator adds the new filename to the end of the output directory path. This file is the output of cdland7 but will be used as the input for future algorithms

3. Define each of the algorithm’s inputs as shown below. You can read more about algorithm input requirements in the Geomatica help. Ensure that all variables are defined, even if they are left as default values. You can also look at the Python example cdland7.py within the examples folder (C:\PCI Geomatics\Geomatica 2015\Examples\Python). The image below shows the organization and inputs.

4. Call the CDLAND7 Algorithm. Type cdland7(filehd, file, cdic, tex1) . This line executes the actual algorithm. The algorithm inputs must appear in this specific order.

Collect GCPs with AUTOGCP2 In order to orthorectify imagery, GCPs must be collected. AUTOGCP2 automatically collects GCPs for your imagery. These GCPs are saved to a new GCP segment

1. Define each of the variables for AUTOGCP2. In this example all the variables are left as their default values except mfile, dbic, filref, filedem and searchr. The outputted in_data from CDLAND7 is provided as the input file (mfile) for AUTOGCP2.

2. Define filref as the reference image (ref_image) and filedem as the dem file (dem_file). 3. In the example below you will notice that LASC = autogcp2(…). As mentioned above the GCPs are

saved to a new segment after being generated. This segment number is returned in list format by the autogcp2 algorithm. The LASC variable will be equal to the GCP segment number after the autogcp2 algorithm is run. LASC will be used in the next algorithm.

Page | 11

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 12: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

4. Note that the \ beside dbec in the code below is used as an escape characters and indicates that the two lines are connected. This is only added to the code for readability so that the autogcp2 line is not too long.

Refinement of GCPs Once all of the GCPs are collected by AUTOGCP2 they need to be refined with GCPREFN. The algorithm uses a computer model to remove ground control points that have high errors. The newly edited GCPs are then saved to a new GCP segment.

1. Set the input file (file) as in_data. 2. The next variable dbgc asks for the GCP segment value (LASC) for the newly generated GCPs. 3. Most of the variables are left as default. Set Modeltype = u”SAT” to choose a satellite model. 4. In this example reject is set to [5,0.5,0.5]. To learn more about this variable see the help for the

GCPREFN algorithm. 5. Writegcp is changed to No in this example since there are no checkpoints used.

Page | 12

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 13: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Calculate Model The GCPs are now ready to be used in model calculation and orthorectification. The model that is used in this example is a satellite model; therefore SATMODEL will be used for model calculations.

1. Satmodel, similar to gcprefn requires the GCP segment number. We want to provide satmodel with the refined GCPs produced by gcprefn. The segment produced by gcpref will be one greater than LASC. Add the line gcp_seg = [LASC[0] +1] . in this line of code the first value from the list LASC (produced by AUTOGCP2) is incremented by one. If LASC equals [4] than gcp_seg will equal [5].

2. Define the input file (mfile) as in_data 3. Set dbgc to our newly defined gcp_seg. 4. Leave the rest of the inputs as their defaults and call the satmodel algorithm.

Page | 13

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 14: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Orthorectification The final step in the process is to orthorectify the imagery using the GCPs. For this step the ORTHO2 algorithm is used.

1. Define the input image (mfile) as in_data. 2. Define the output ortho image (filo) as a new image “output_ortho.pix” in the output folder. 3. Set the horizontal (bxpxsz) and vertical (bypxsz) pixel sizes to 8 4. Set filedem to the input DEM file 5. Leave the other inputs as their default inputs. Run ortho2, ensuring that all of the variables are listed

in the correct order. This step may take some time to run.

Running the Python Script (Command Line) Once the entire script is created you can run the script in command line. Navigate to the folder with the python file and enter the file name, making sure to put .py at the end.

Page | 14

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 15: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

If the script runs into an issue it will stop and produce an error. The following error was produced because ingest_landsat.pix was already in the output folder. Delete ingest_landsat and rerun the script.

If no issues are encountered the script will run through each algorithm, printing out the “Complete” statements. The GCP reports from AUTOGCP2 and GCPRFN will be printed to the command line output. The ORTHO2 function is the most time consuming function which prints out lines similar to the following.

The script is completed when the final message “End of Workflow” is printed.

Page | 15

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 16: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Running the Script (Focus) Another option that can be used to run the script is the Focus Python Scripting window. Although you can run a script from within this window it is suggested that you write the script in another program (Notepad ++, IDLE, etc).

1. Open Focus then open the Python Scripting window from Tools Python Scripting. 2. Once the window is open you can open the python script that you created with the open file button

. The script will load into the window.

3. With the script open click the run button ( )to start the script 4. A separate window will open that will print out information from the script and finally print “End of

Workflow”.

The final output of the script execution is an orthorectified image. You can load ingest_landsat and output_ortho into Focus to see the result of the orthorectification process. Your final image should look similar to the image below.

Page | 16

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.

Page 17: Python Introduction Geomatica 2015 Tutorial · Python Introduction Geomatica 2015 Tutorial The purpose of this tutorial is to provide an introduction to Python for use in Geomatica.

Python Introduction

Geomatica 2015 Tutorial

Page | 17

The information in this document is subject to change without notice and should not be construed as a commitment by PCI Geomatics. PCI Geomatics assumes no responsibility for any errors that may appear in this document.