0% found this document useful (0 votes)
11 views

Chapter 5_Lecture1-Programming in GIS

programming in GIS notes

Uploaded by

Prisca Molefe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter 5_Lecture1-Programming in GIS

programming in GIS notes

Uploaded by

Prisca Molefe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 105

CGB 224

PROGRAMMING FOR GEOMATICS

Chapter 4-5 - Lecture 2


Part 1: Python Modules & Packages
Part 2: Introduction to Geoprocessing
Part 3: Programming in GIS: ArcPy
Part 4: NumPy and SciPy
Prof. Ouma
UB
UB GEOMATICS GEOMATICS1
Contents
1. Modules in Python
2. Packages in Python
3. Incorporating Python in ArcGIS: ArcPy
4. Python Modules: NumPy and SciPy

UB GEOMATICS 2
5.0 Geoprocessing

UB GEOMATICS 3
5.0 Geoprocessing

UB GEOMATICS 4
5.0 Geoprocessing

UB GEOMATICS 5
5.0 Geoprocessing

UB GEOMATICS 6
5.0 Geoprocessing

UB GEOMATICS 7
5.0 Geoprocessing

UB GEOMATICS 8
5.0 Geoprocessing

UB GEOMATICS 9
5.0 Geoprocessing

UB GEOMATICS 10
5.0 Geoprocessing

UB GEOMATICS 11
5.0 Geoprocessing

UB GEOMATICS 12
5.0 Geoprocessing

UB GEOMATICS 13
5.0 Geoprocessing

UB GEOMATICS 14
5.0 Geoprocessing

UB GEOMATICS 15
5.0 Geoprocessing

UB GEOMATICS 16
5.0 Geoprocessing – Data

UB GEOMATICS 17
5.0 Geoprocessing – Data

UB GEOMATICS 18
5.0 Geoprocessing – Data

UB GEOMATICS 19
5.0 Geoprocessing – Data

UB GEOMATICS 20
5.0 Geoprocessing – Data

UB GEOMATICS 21
5.0 Geoprocessing – Data

UB GEOMATICS 22
5.0 Geoprocessing – Data

UB GEOMATICS 23
5.0 Geoprocessing – Data

UB GEOMATICS 24
5.0 Geoprocessing – Data

UB GEOMATICS 25
5.0 Geoprocessing – Data

UB GEOMATICS 26
5.1 Programming in GIS
(Geoprocessing using Python)
1. https://www.youtube.com/watch?v=lQFBuhtZD6M
2. https://www.youtube.com/watch?v=MdFB4psq42A
3. ESRI:
1. https://www.youtube.com/watch?v=iTZytnBcagQ
2. https://www.youtube.com/watch?v=r8CenylJ7c0
3. https://www.youtube.com/watch?v=nghtMu11BXM

UB GEOMATICS 27
5.1 Incorporating Python in GIS

UB GEOMATICS 28
UB GEOMATICS 29
UB GEOMATICS 30
UB GEOMATICS 31
5.1 Incorporating Python in GIS

UB GEOMATICS 32
5.1 Incorporating Python in GIS

UB GEOMATICS 33
5.1 Incorporating Python in GIS

UB GEOMATICS 34
5.1 ArcPy

UB GEOMATICS 35
5.2 arcpy

UB GEOMATICS 36
5.2 ArcPy

UB GEOMATICS 37
5.2 ArcPy

UB GEOMATICS 38
5.2 Incorporating Python in GIS

UB GEOMATICS 39
5.2 ArcPy Interfaces – Python Window

UB GEOMATICS 40
5.2 ArcPy Interfaces – Script Tools

UB GEOMATICS 41
5.2 ArcPy Interfaces – Command Line

UB GEOMATICS 42
UB GEOMATICS 43
5.3 Python and ModelBuilder in GIS
Programming

UB GEOMATICS 44
UB GEOMATICS 45
UB GEOMATICS 46
UB GEOMATICS 47
5.3 ModelBuilder

UB GEOMATICS 48
Python
program
Exported
from
ModelBuilder

UB GEOMATICS 49
5.3 Example: Hot Spot Analysis Model

UB GEOMATICS 50
5.3 Example: Hot Spot Analysis Model

UB GEOMATICS 51
5.3 Example: Hot Spot Analysis Model

UB GEOMATICS 52
5.4 Scripting in GIS using ArcPy

UB GEOMATICS 53
5.4 ArcPy Scripting

UB GEOMATICS 54
5.4 ArcPy

UB GEOMATICS 55
5.4.1 ArcPy – setting the workspace

UB GEOMATICS 56
Videos: Example ArcPy Scripting
1. Creating Point Feature Classes Using Arcpy Scripting
• https://www.youtube.com/watch?v=NPEvq0jXyJA

2. Creating Line Feature Classes Using Arcpy Scripting


• https://www.youtube.com/watch?v=UtPKjYOv2mg

3. Creating Polygon Feature Classes Using Arcpy


Scripting
• https://www.youtube.com/watch?v=csyVBgern4U

UB GEOMATICS 57
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 58
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

Import arcpy within ArcGIS

Set the working Environment (directory)


Step 1:
Step 2:
Step 3:

Import functions from Toolbox

UB GEOMATICS 59
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 60
5.4.2 ArcPy – Example

UB GEOMATICS 61
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 62
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 63
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

Use of actual names

UB GEOMATICS 64
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 65
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 66
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 67
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)

UB GEOMATICS 68
5.4.3 ArcPy: Classes

UB GEOMATICS 69
5.4.3 ArcPy: Classes

UB GEOMATICS 70
5.4.3 ArcPy: Classes

Syntax for ArcPy classes

UB GEOMATICS 71
5.4.3 ArcPy: Classes

UB GEOMATICS 72
5.4.4 ArcPy: Functions

UB GEOMATICS 73
5.4.4 Functions

UB GEOMATICS 74
5.4.4 Functions

UB GEOMATICS 75
5.5 Creating Custom Tools with
ArcPy

UB GEOMATICS 76
5.5.1 Creating tools with Python
• Geoprocessing system tools (those installed with
ArcGIS) are designed to perform one small but
essential operation on geographic data.

• Using ModelBuilder or Python, you execute these


geoprocessing tools in a sequence, feeding the
output of one tool to the input of another.

• The script you create may be an essential part of your


workflow—a task you need to repeat again and again.

UB GEOMATICS 77
Creating tools with Python cont…
• Scripts that you create can be executed with one
of two basic methods:
• outside ArcGIS
• within ArcGIS

• Outside ArcGIS means that the script is


executed from the operating system
command prompt,
• within a development application,
such as PythonWin.
UB GEOMATICS 78
Executing a script

UB GEOMATICS 79
Python Tool Creation
• Geoprocessing tools can be created from Python
in two ways:
1. standard toolboxes, where you connect a Python script
tool to a toolbox using a wizard, and

2. Python toolboxes which are an ASCII file written


entirely with Python code.

UB GEOMATICS 80
What is a script tool?
• Creating a script tool allows you to turn your own
Python scripts and functionality into your own
geoprocessing tools—tools that look and act like
system geoprocessing tools in ArcGIS.

• Once created, a script tool provides many


advantages:

UB GEOMATICS 81
Python Script Tool Advantages
1. A script tool that you create is an integral part of geoprocessing,
just like a system tool—you can open it from
the Searchor Catalog window, use it in ModelBuilder and
the Python window, and call it from another script.

2. You can write messages to the Results window and progress


dialog box.

3. Using built-in documentation tools, you can provide


documentation.

4. When the script is run as a script tool, arcpy is fully aware of the
application (such as ArcMap) it was called from. Settings made
in the application, such as arcpy.env.overwriteOutput
and arcpy.env.scratchWorkspace, are available from ArcPy in
your script tool.

UB GEOMATICS 82
UB GEOMATICS 83
Creating script tools in a toolbox
• To create a script tool in a custom toolbox, you need three things:
• A script
• A custom toolbox
• A precise definition of the parameters of your script

• To create a script tool, right-click your toolbox, and


click Add > Script. This opens the Add Script wizard which
takes you step by step through the process of creating a script
tool.

• After completing the steps, your toolbox will contain a new script
tool. You can always modify the properties names and data types
of this script tool by right-clicking the script tool and
choosing Properties.

UB GEOMATICS 84
UB GEOMATICS 85
UB GEOMATICS 86
UB GEOMATICS 87
UB GEOMATICS 88
UB GEOMATICS 89
5.5.2 Accessing a tool in Python
code
• There are two ways to access a tool in a line of Python
code. The easiest way to call a tool is to call its
corresponding function.
• All tools are available as functions in ArcPy. An ArcPy
function is a defned bit of functionality that does a specifc
task.

• The syntax for calling a tool by its function is as follows:


• arcpy.<toolname_toolboxalias>( <parameters> )

• For example, the following code runs the Clip tool:


• import arcpy
• arcpy.env.workspace = "C:/Data"
• arcpy.Clip_analysis( "streams.shp", "study.shp", "result.shp" )

UB GEOMATICS 90
Alternative approach to
Accessing a tool in Python code
• Tools are also available in modules that match the toolbox
alias name. An alternative way to access a tool is to frst call
the toolbox as a module and then the tool as a function,
followed by the tool’s parameters. The syntax is as follows:

• arcpy.<toolboxalias>.<toolname>( <parameters> )

• Example - running the Clip tool:


• import arcpy
• arcpy.env.workspace = "C:/Data"
• arcpy.analysis.Clip( "streams.shp", "study.shp", "result.shp" )

UB GEOMATICS 91
Understanding Python Syntax
• Both methods are correct, and the approach you use is a
matter of preference and coding habits.

• Just a few quick reminders regarding Python syntax:


• Python is case sensitive, so Clip is correct, but clip is not.
• The use of spaces, or whitespace, in a line of code has no effect on
its execution. For example, workspace="C:/Data" is the same as
workspace = "C:/Data".
• Whitespace is recommended to improve readability but is not
required. However, do not include spaces between modules,
functions, classes, methods, and properties, so env.workspace is
correct, but env. workspace is not.

UB GEOMATICS 92
Understanding Python Syntax cont..
• Also do not include spaces between functions and their
arguments, so
• <toolname>( <parameters> ) is correct, but
• <toolname> ( <parameters> ) is not.

• Quotation marks in Python are straight and not curly, so


use " " and not “ ”.

• When typing code using a Python editor, the correct style


of quotation marks is entered automatically. However,
incorrect quotation marks can occur when copying code
from other applications, such as a Microsoft Word
document or a PDF fle.

UB GEOMATICS 93
Key aspects of running
geoprocessing tools
• A key aspect of running geoprocessing tools is to get the syntax
right for the parameters.
• Every geoprocessing tool has parameters, required and
optional, that provide the tool with the information it needs for
execution.
• Common parameters are input datasets, output datasets, and
keywords that control the execution of the tool.
• Parameters themselves have properties such as the following:
• Name: a unique name for each tool parameter
• Type: the type of data expected, such as a feature class, integer, string, or
raster
• Direction: whether the parameter defines input or output values
• Required: whether a value must be provided for a parameter or is
optional

UB GEOMATICS 94
Tool Documentation
• The documentation of each tool describes its parameters
and properties.
• Once a valid set of parameters is provided, the tool is
ready to run.
• Most parameters are specified as a simple string. Strings
consist of text that identifies a parameter value, such as
a path to a dataset or a keyword.

UB GEOMATICS 95
Example: Clip tool’s documentation
• The Clip tool’s documentation, as shown in the figure,
describes its
• parameters.

UB GEOMATICS 96
Example: Clip Tool Parameters
• The Clip tool has four parameters, with the last one (
cluster_tolerance ) being optional.
• The syntax of the Clip tool is:
• Clip_analysis (in_features, clip_features, out_feature_class,
{cluster_tolerance} )

• The name of the Clip tool is followed by the tool’s


parameters in parentheses.
• Parameters are separated by a comma (,) surrounded
by curly brackets ( { } ).

UB GEOMATICS 97
Geoprocessing Tools Syntax
• The syntax of geoprocessing tools typically follows the
same general pattern, as follows:

• Required parameters come first, followed by optional parameters.

• The input datasets are usually the first parameter or parameters,


followed by the output dataset if there is one. Next are additional
required parameters, and finally, optional parameters.

• Parameter names for input datasets are prefxed by “in_” ( such as,
in_data, in_features, in_table, in_workspace ) and parameter
names for output datasets are prefxed by “out_” ( such as,
out_data, out_features, out_table ).

UB GEOMATICS 98
Order of Listing Code Parameters
• Listing required parameters first makes it easy to simply leave
out the optional parameters when they are not needed.
• Sometimes, however, some of the optional parameters need to
be set.
• Because parameters need to be specified in the order that they
are listed in the tool syntax, it can mean that some optional
parameters may need to be skipped.

• Consider, for example, the syntax of the Buffer tool:

• Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field,


{line_side}, {line_end_type}, {dissolve_option}, {dissolve_field}

UB GEOMATICS 99
Buffer Tool Example
• A code example of the Buffer tool is as follows:

• import arcpy arcpy.env.workspace = "C:/Data/study.gdb“


• arcpy.Buffer_analysis ("roads", "buffer", "100 METERS")

• Using this example, how would you specify the optional


dissolve_option parameter and skip the other optional
parameters that follow the required parameters?
• It can be accomplished in different ways, as follows:
• By setting the optional parameters using an empty string ( "" ) or
the number sign ( "#" )
• By specifying by name the parameter that needs to be set,
bypassing all others

UB GEOMATICS 100
Buffer Tool Example cont..
• The Buffer tool has three required parameters and four optional
parameters.
• To specify a dissolve option and the field to use in this dissolve, two
optional parameters need to be skipped.

• This can be done in three ways:

• arcpy.Buffer_analysis( "roads", "buffer", "100 METERS", "", "", "LIST", "Code" )

• arcpy.Buffer_analysis( "roads", "buffer", "100 METERS", "#", "#", "LIST", "Code" )

• arcpy.Buffer_analysis("roads", "buffer", "100 METERS", dissolve_option="LIST",


dissolve_field="Code" )

UB GEOMATICS 101
Buffer Tool Example cont..
• In the examples so far, the parameters of the tool use
the actual file name ( for example, “roads” ). This
means the file names are hard-coded.
• That is, the parameters are not set as variables, but
use the values directly.
• Although this syntax is correct and works fine, it is
often more useful to make your code flexible by using
variables for parameters instead of using file names.
• First, create a variable and assign it a value. Then you
can use the variable as a parameter. These variable
values are passed to the tool.

UB GEOMATICS 102
Clip Tool Example
• For example, in the case of the Clip tool, it would look
like this:

import arcpy
arcpy.env.workspace = "C:/Data"
infc = "streams.shp"
clipfc = "study.shp"
outfc = "result.shp"
arcpy.Clip_analysis( infc, clipfc, outfc )

UB GEOMATICS 103
END OF LECTURE
PROCEED TO REVIEW!

UB GEOMATICS 104
UB GEOMATICS CGB 221

You might also like