Chapter 5_Lecture1-Programming in GIS
Chapter 5_Lecture1-Programming in GIS
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
UB GEOMATICS 57
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)
UB GEOMATICS 58
5.4.2 ArcPy: Tools - ArcToolbox (ArcGIS)
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)
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
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.
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
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
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.
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.
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
• 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.
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> )
UB GEOMATICS 91
Understanding Python Syntax
• Both methods are correct, and the approach you use is a
matter of preference and coding habits.
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.
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} )
UB GEOMATICS 97
Geoprocessing Tools Syntax
• The syntax of geoprocessing tools typically follows the
same general pattern, as follows:
• 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.
UB GEOMATICS 99
Buffer Tool Example
• A code example of the Buffer tool is as follows:
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.
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