0% found this document useful (0 votes)
2 views30 pages

Python-1

The document provides an overview of Python as a programming language, highlighting its simplicity, open-source nature, and versatility in scripting and programming. It discusses the use of Python in ArcGIS for automating GIS tasks, geoprocessing, and the differences between scripting and programming. Additionally, it covers Python basics, data types, expressions, statements, functions, and coding tips relevant to ArcGIS users.

Uploaded by

tarak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

Python-1

The document provides an overview of Python as a programming language, highlighting its simplicity, open-source nature, and versatility in scripting and programming. It discusses the use of Python in ArcGIS for automating GIS tasks, geoprocessing, and the differences between scripting and programming. Additionally, it covers Python basics, data types, expressions, statements, functions, and coding tips relevant to ArcGIS users.

Uploaded by

tarak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PYTHON FOR ARCGIS

Su Zhang, Ph.D., GISP, CMS-RS


WHAT IS PYTHON

• It is a programming language that is both


simple and powerful, and more importantly,
it is simple and easy to learn
• It is free and open source
• It is cross platform gisgeography.com

• Interpreted language, no special compilers


required
• Object Oriented Programming language

alphansotech.com
SCRIPTING VS PROGRAMMING

• Scripting – automating certain functionality


within another program
• Programming – developing more sophisticated
multifunctional applications
• Scripting is a programming task allows you to
connect various existing components to
accomplish a new, related task
• Scripting is the glue that allows you to put
various existing elements together
geeksforgeeks.org
• Programming allows you to build components
from scratch, as well the applications that Python is both a scripting and a
incorporate these components (system programming language!
language)
SCRIPTING IN ARCGIS

• ArcGIS 9 introduced scripting support for many


popular scripting languages, including, but not limited
to, Python, VBScript, JavaScript, JScript, PERL, C#,
Ruby, Scala, and Arcade
• Starting with ArcGIS 10, the Visual Basic for
Applications (VBA) development environment is no
longer installed by default, and ESRI is discouragingly
the continued use of VBA
• Python is included with ArcGIS for Desktop installation
• ArcToolBox contains tools created from python scripts
• Check your ArcGIS Installation files for python version
esri.com
SCRIPTING IN ARCGIS PRO
HOW TO WRITE PYTHON SCRIPTS?

• Python Command line


 All Programs > ArcGIS > Python x.x > Python (commnad
line) or Python Command Prompt or IDLE (Python GUI)
or Python Interactive Terminal or IPython
• Python Script Editor
 Integrated development environments (IDEs)
 Syntax formating and highlighting
 Jupyter Notebook
• Python window in ArcMap or ArcGIS Pro
• Other code editors – Notepad++, PythonWin, Visual Studio
Code, ...
https://wiki.python.org/moin/PythonEditors
SCRIPTING IN ARCGIS
IDLE (Python GUI) Jupyter Notebook

Python File Python window in ArcMap


PYTHON AND GEOPROCESSING

• Allows to perform spatial analysis,


modeling, and automating GIS tasks
• Supports the automation of workflows
by creating a sequence that combines
series of tools
• Geoprocessing framework comprises of
 Tools organized in toolboxes and toolsets
 Methods to find and execute tools
(standard tools, model builder, & python)
 Parameters and environment settings
 Results window that logs the tool
execution
TYPES AND CATEGORIES OF TOOLS

• Types
 Built-in tools
 Model tools
 Script tools
 Specialized tools
• Categories
 System tools - installed as part of ArcGIS
software
 Custom tools - script and model tools or other
third party Add-ins
desktop.arcgis.com
TOOL TYPES

desktop.arcgis.com
TOOL CATEGORIES
MODELBUILDER VS PYTHON

• ModelBuilder is a visual programming language


 Intuitive way to create tools/workflows
 String together sequences of geoprocessing tools
 Requires no programming experience or to learn syntax pro.arcgis.com

 Could be slow pro.arcgis.com

• Python is a text-based programming language


 Allows advanced programming logic
 Can be used with other software packages
 Can be run as a standalone script outside of ArcGIS
 Can be scheduled to run at a specific time
 Much faster
Build your model first! And
then build your code!
MODELBUILDER VS PYTHON

• Models can be converted


to scripts, but not vice
versa
• ModelBuilder has
limitations and cannot
perform more complex
geoprocessing operations
PYTHON WINDOW IN ARCGIS
DESKTOP
PYTHON WINDOW IN ARCGIS PRO

• The bottom session of the Python


window is called the prompt, where you
can type your code
• The top session of the Python window
is called the transcript, which is intially
blank. The transcript provides a record
of previously entered code and its
results.
• Right click on the codes and select
Clear Transcript to delete codes.
PYTHON WINDOW IN ARCGIS DESKTOP

• Single line code can be executed with ENTER


command at the end of each line
• Multiple line code can be executed with ENTER
command two times
• Result is printed to top session and bottom
session starts with a new prompt
• Multiline code uses secondary prompt to
complete the code. Secondary prompt is
automatically added when pressing the ENTER
key at the end of a line of code
• All geoprocessing tools can be accessed by
importing ArcPy site package

desktop.arcgis.com
PYTHON WINDOW IN ARCGIS DESKTOP

• Supports Autocomplete functionality


• Conditional execution can be
performed using if-then-else logic
• Iteration can be done with for and
while loops
• Python provides access to third party
modules for data manipulation
• Python code blocks written in the
window can be saved to a python or
text file
• Allows to load code from another file
PYTHON BASICS
Basics of Python
DATA TYPES

• Strings: texts such as “GIS”


• Lists: stores a sequence of items
inside brackets separated by
commas [1, 2, 4, 8, 16, 32] or
[“Ford”, “Chevy”, “Toyota”,
“Honda”, “Subaru”]
• Tuples: Similar to lists, uses
parentheses, but are immutable,
meaning they cannot be changed
1, 2, 4, 6, 8, 16, 32
DATA TYPES

• Boolean: True or False


• Dictionaries: stores pairs of items (key
and values) {“Austin”: “Texas”,
“Baltimore”: “Maryland”, “Cleveland”:
“Ohio”, “Denver”: “Colorado”}
• Numeric: 5, 1.3435926, 3+1j
• Set is an unordered collection of data
type that is iterable, mutable and has no
duplicate elements; The order of
elements in a set is undefined though it
may consist of various elements [1, 2,
'Geeks', 4, 'For', 6]
DATA TYPES

• Python scripts uses variables to


store information
• Python will know your variable’s
type by the value you assigned to
 If 17 then it is integer
 If “GIS” then it is string
 If 3.1415926 then it is float
• No need to declare a variable and
define its type before you can
actually use it
 Int age = 20 (variable type, variable
name, and then variable value)
EXPRESSIONS, STATEMENTS, FUNCTIONS,
METHODS

• An expression is a value
 Ex: 2 * 3 is an
expression returning the
value of 6
• Expressions can be built
using operators and
functions
• Expressions can contain
variables
EXPRESSIONS, STATEMENTS, FUNCTIONS,
METHODS

• A statement is an
instruction to perform
something
 Ex: x = 2 * 3
 Ex: Print, for, if-then-else,
...
• A statement do not return a
value (exception print)
EXPRESSIONS, STATEMENTS, FUNCTIONS,
METHODS

• A function is similar to a statement (to do


something) and returns a value
• A function is a small program used to
carry out a certain action.
 Ex: pow (2, 3)
• Python includes some built-in functions
with installation
• Many functions are available in python
than the built-in functions. Using them
requires modules
 Ex: import os, import math
EXPRESSIONS, STATEMENTS, FUNCTIONS,
METHODS
• Methods are similar to functions
• A method is a function that is closely coupled to
an object
• Methods are case sensitive
 Ex: <object>.<method>(<arguments>)
 >>> course = “Geographic Information
Systems Advanced”
 >>> course.count(“i”)
2
CODING TIPS
• Python scripting is case sensitive
• Variable names should be all lowercase and
contain only characters, digits, and the
underscore (_)
• Indentation:
 Use of four spaces is recommended to define
each indentation level
 never mix tabs and spaces
• Comments:
 Scripts should contain adequate commenting
esriuk.com
 Each script tool or function should have a
header that contains script name, a description
of how the script works, its requirements, who
wrote it and when
PYTHON SYNTAX IN ARCGIS PRO

• Clip
 Syntax: arcpy.analysis.Clip(in_features, clip_features, out_feature_class, {cluster_tolerance})

https://pro.arcgis.com/en/pro-app/latest/tool-
reference/analysis/clip.htm
PYTHON BOOKS
PYTHON BOOKS
PYTHON BOOKS

You might also like