Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

Intro to Python Welcome to the Wonderful world of GIS programing!
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Python & ModelBuilder. Overview Python/ModelBuilder Concepts – The Geoprocessor – Checking some environment variables – Providing feedback from your model/script.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to C Programming
Geography 465 Assignments, Conditionals, and Loops.
Geography 465 GIS Database Programming Getting Started with GIS Database Programming.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
Python Programming Fundamentals
Python & ModelBuilder. Continuing Education Python and ModelBuilder Overview Python/ModelBuilder Concepts –The Geoprocessor –Checking some environment.
Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.
2006 GIS Jam: ArcGIS Python Scripting
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Python: An Introduction
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Functions Reading/writing files Catching exceptions
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
If statements while loop for loop
Module 6: Geoprocessing Scripts. Processing loops and decisions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
1 JavaScript in Context. Server-Side Programming.
Python Let’s get started!.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Controlling Program Flow with Decision Structures.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Linux Administration Working with the BASH Shell.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Introduction to GIS PythonScript CGIS-NURIntroduction to ArcGIS II.
Chapter 6 JavaScript: Introduction to Scripting
Python Let’s get started!.
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
PYTHON: AN INTRODUCTION
The Selection Structure
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Lecture 9 Using Python for Geoprocessing
Lecture 10 Accessing tools and environment setting in Scripts
An Introduction to Python
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Programming
Geography 465 GIS Database Programming
Introduction to Programming
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Geography 465 Analytic Cartography: Getting Started with Python Scripting

What is Geoprocessing? The application of GIS operations on a set of inputs for generating new information (or new data as appropriate to situation) All geoprocessing tools are accessed from ArcToolbox A tool can be a dialog, a script, or a model.

Why write scripts? Scripts supply the added benefit of decision making logic and looping functionality Automate a work flow, for example: –Copy all incoming data into a geodatabase –Perform a project, clip, buffer operation on multiple data sets (iterate) Easily distribute code –A script is a self-contained, single file

Why use Python for Scripting? An open-source, object-oriented, scripting language Offers IDE ( integrated development environment ) with debugging tools Modular, can be broken apart Ability to compile scripts Installed with ArcGIS 9 and ESRI samples provided

Writing code in Python Where to write Python code? –Python command line –IDE (integrated development environment): PythonWin IDE allows you to perform all jobs from one location –Write, –Save, –Run, and –Debug code

PythonWin Interface Script window –Write and save code Interactive window –Test lines of code –Report messages Menus and Toolbars –standard and debugging

Basics of Python Comment : A non-executable line of code –One number sign (#) for green and italicized –Two number signs (##) for gray # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer a feature class import win32com.client gp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”) ## gp.Workspace = “C:\\Python_Data\\SanDiego.mdb” Gp.Buffer_analysis (“Freeways”, “BuffFreeway”, 1000) Can comment and uncomment blocks of code

Variables in Python Variables are dynamically typed –No declaration required –No type assignment required fc = “C:\\ProjectData\\SanDiego.mdb\\Freeways.shp” Variables are case sensitive fc = “Freeways.shp” Fc = Variables can hold different data types –strings, numbers, lists, files Two different variables

Strings Variables can hold strings folder = “c:/Student” Strings are surrounded in double (“) or single (‘) quotes Pathnames use two back (\\) or one forward (/) slash One backslash (\) is a reserved escape character and a line continuation character

Strings Strings can be combined together gdbPath = “c:\\SanDiego.mdb” fc = “Roads” fullPath = gdbPath + “\\” + fc Strings are indexed –strings are zero-based from the left and one-based from the right fc = “Streets.shp” fc[0] ---> “S”# S is in the 0 position fc[1:3] ---> “tr”# start at 1st, up to not including 3rd fc[:-4]---> “Streets” # get rid of the last 4 charaters C:\SanDiego.mdb\Roads”

Numbers and lists Variables can hold numbers and expressions num1 = 1.2 num2 = Variables can hold lists numList = [1, 2, 3] fcList = [“Roads”, “Streets”, “Parcels”, “Zipcodes”] Lists are indexed fc1 = fcList[1] fc2 = fcList[0:2] ---> “Roads”, “Streets” fc3 = fcList[0:-1] ---> “Roads”, “Streets”, “Parcels” fc4 = fcList[2:] ---> “Parcels”, “Zipcodes” ---> “Streets”

Variable naming conventions Upper case versus lower case –First word lower case, capitalize each successive word tableFieldName = “Street” - Acronym at the beginning, use lower case letters gdbPath = “C:\\SanDiego.mdb” -Acronym in the middle or at the end, use upper case letters inputFC = “Streets.shp” Avoid special characters (for example / \ & % # !) Use descriptive variable names

Line continuation Line continuation characters –Parentheses ( ), brackets [ ], and braces { } –Backslash \ Indentation is automatic fcList = [“Roads”, “Climate”, “Streams”, “Zipcodes”, “Coastlines”] distanceValues = 100, 200, 300, 400, 500, \ 1000, 1500 gp.Buffer_analysis(fcList[2], BuffStreams1000, distanceValues[5])

Built-in functions Python has many built-in functions Built-in means that Python automatically makes these functions available – they don’t have to be imported like most other functions You can see the list of built-in functions by typing dir(__builtins__) into the interactive window of PythonWin

Examples of Built-in functions len ( ) – returns the length fc = “Streams.shp” len (fc) ---> 11 max ( ) – returns the maximum value xExtent = ( , ) max (xExtent) ---> open ( ) – opens a file coord = open(“C:\\Mydata.txt”, “r”).read( ) ---> holds the contents of the Mydata.txt file round ( ) – Rounds a number xCoord = round(xCoord) --->

Accessing modules Most functions are imported from modules –The math module import math math.sqrt(64) ---> 8.0 math.pi ---> –The string module Import string string.split(“ ”) ---> [’75.7’, ‘-45.3’] string.upper(“c:\\student”) ---> ‘C:\\STUDENT’

Accessing modules –The os.path module import os.path os.path.basename(“C:\\SanDiego.mdb\\Streets,shp” ---> ‘Streets.shp’ os.path.dirname((“C:\\SanDiego\\Streets.shp” ---> ‘C:\\SanDiego’

Defining your own functions Function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. def NAME( LIST OF PARAMETERS ): STATEMENTS

Defining your own functions - example def newLine(): print print "First Line." newLine() print "Second Line." def threeLines(): newLine() print "First Line." threeLines() print "Second Line."

Defining your own functions with parameters- example def printTwice(n): print n, n printTwice(“Geog465 project!”) ---> ? n = “Geog465 project!” printTwice(n)

Boolean Expressions A boolean expression is an expression that is either true or false. In Python an expression that is true has the value 1, and an expression that is false has the value 0. In this case the equal comparison (not assignment) operator is as follows. >>> 5 == 5 1 >>> 5 == 6 0

Boolean Expressions The == operator is one of the comparison operators; the others are: x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y

Logical Operators There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.

Decision Statement Syntax Conditional statement: if…elif…else if x == 1: print “x is 1” elif x == 2: print “x is 2” else: print “x is not 1 or 2” Colons are used at the end of each condition Indentation defines what executes for each condition

Looping syntax While loop x = 1 while x < 10: print x x = x + 1 Colons used at end of each statement Indentation defines what executes for the loop

Looping syntax Counted loop for x in range(1, 5): print x Counted loops increment and test a variable on each iteration of the loop The last value is not executed

Looping syntax List loop x = [1, 2, 3] For a in x: print a List loops iterate over each value in a list The loop will execute once for each value in the list

Case sensitive rules Case sensitive –Functions and statements Correct: max len open print import if Incorrect:Max LEN OpEn imporT IF -Variable names Not case sensitive –Path names “C:\\DATA” = “c:\\Data” Geoprocessing properties and methds gp.BUFFER = gp.buffer

Exposing Geoprocessing Functions An ArcObjects component, the geoprocessor object, is the main vehicle for exposing the geoprocessing functions to scripting in Python It is an object that provides a single access point and environment for the execution of any geoprocessing tool in ArcGIS, including extensions.

Exposing Geoprocessing Functions GpDispatch is a COM wrapper for the geoprocessor making it possible for Python to access more than 450 available tools Example: import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # use Clip_Analysis tool gp.Clip_analysis ("C:\\World\\Cities.shp", "C:\\World.mdb\\Yemen\\Yemen", "C:\\World.mdb\\Yemen\\ClipCitiesScript")

Using COM Objects with Python To use COM objects: import win32com.client o = win32com.client.Dispatch(“Object.Name") Example: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible = 1 o.Workbooks.Add() o.Cells(1,1).Value = "Hello"

Interacting with Geoprocessor Geoprocessor, like any ArcObject, has properties and methods –Property: characteristic of an object (an adjective) –Method: something the object knows how to do (a verb) Interact with Geoprocessor through properties and methods –to buffer use Buffer method –to clip use Clip methods –to delete feature class use Delete method

Accessing the Geoprocessor from Python Tell Python you want to use COM objects import win32com.client Create the instance of Geoprocessor gp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”)

Syntax for Properties and Methods To assign a value to property Object.Property = value gp.Workspace = “C:\\Data” To get a value of a property Object.Property print “The name of the workspace is “ + gp.Workspace To use a method Object.Method(argument, argument, …) gp.Buffer_analysis (“Freeways”, “FreewaysBuffer”, 100) - parentheses around arguments - arguments separated by commas

Running Scripts in Python Use of ArcGIS help system to find: –Usage, command syntax, and scripting examples of standard ArcToolbox tools, –Usage and syntax of Geoprocessor properties and methods, which are only accessible through scripting

Running Python Scripts Three Modes 1) Running scripts in PythonWin 2) Running scripts as script tools in ArcGIS 3) Running scripts as embedded model components in ArcGIS

Running Scripts in Python Problem with the schema lock –Cannot manipulate with the same file open in ArcGIS and in PythonWin at the same time

Running scripts in PythonWin Example of a simple script to buffer a feature class # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer a feature class import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1“ gp.Workspace = "C:\\Python_Data\\SanDiego.mdb” gp.Buffer_analysis ("Freeways", "BufferedFreeways", 1000)

Debugging the Code Test code in PythonWin –A check for syntax errors Do not test a script from ArcToolbox Test code in PythonWin, then add it to ArcToolbox

Running scripts in PythonWin Run the script from the script window of PythonWin Check the message in the lower left corner of the interactive window in PythonWin: –While the script is running you will see the message “running script.py” -The indication of the successful run is the message “Script returned the exit code 0” Display the result in ArcCatalog

Running scripts in PythonWin Example of a simple script to clip a feature class # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To clip a feature class import win32com.client gp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.Workspace = "C:\\Python_Data\\SanDiego.mdb" gp.Clip_analysis ("MajorAttractions", "SDdowntown", "SDdowntownAttractions")

Running scripts in PythonWin Example of a simple script to buffer and clip a feature class # Name Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer SD freeways first and then to clip the downtown section # of freeways import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.Workspace = "C:\\Python_Data\\SanDiego.mdb" if gp.Exists(gp.Workspace + "\\bufferedFreeways"): gp.Delete_management(gp.Workspace + "\\bufferedFreeways") gp.Buffer_analysis ("Freeways", "bufferedFreeways", "500") gp.Clip_analysis ("bufferedFreeways", "SDdowntown", "downtownFreeways")

Gene will get you started with hands-on Python in lab session