61
Writing Geoprocessing Scripts With ArcGIS Lecture 10

Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

  • Upload
    ledat

  • View
    242

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Writing Geoprocessing Scripts

With ArcGIS

Lecture 10

Page 2: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

GIS System

Page 3: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

ArcObjects ArcObjects are the building blocks of ArcGIS. With

ArcObjects, you can create your own menus, tools, workflows, applications, and custom feature classes for use with ArcGIS.

ESRI ArcObjects is the development platform for the ArcGIS family of applications, such as ArcMap, ArcCatalog, ArcScene, ArcGIS Engine, and ArcGIS Server. The ArcObjects software components expose the full range of functionality available in ArcInfo, ArcEditor, and ArcView to software developers

Can use VBA, Python, C++, Java to program

Page 4: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 5: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 6: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 7: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 8: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

For ArcGIS 10 and Python 2.6.5:

import arcpy

Tells Python to import basic ArcGIS geoprocessing functionality

*****

from arcpy import env

Tells Python to import ability to control the ArcGIS Environment

*****

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

Example: Tells Python to import functionality from ArcGIS Spatial Analyst

and a second command to get/check the Spatial Analyst license

Page 9: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

env.workspace = "C:/Temp"

env.workspace

arcpy.Buffer_analysis ("C:/input/roads.tif", "C:/output/Output.gdb/buffer_output", 100)

Page 10: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

arcpy.Clip_arc() arcpy.Clip_analysis()

Always assign names

to custom toolboxes

Page 11: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

env.workspace = "c:/basedata/roads.gdb"

arcpy.Select_analysis("nfroads", "paved", '[ROAD_CLASS] = "PAVED"')

Python uses forward slashes, different than Windows using back slashes

Double quotes pass text strings

Single quotes contain text strings intended to pass variable names

Page 12: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 13: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Python Basics

Page 14: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 15: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

gdbPath = "C:/SouthAfrica.mdb"

fc = "Roads"

fullPath = gdbPath + "/" + fc

Page 16: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 17: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Variable index

Word =

Word[0]=‘H’

Word[2:4]=‘lp’

Word[:3]=‘Hel’

Word[-2:-4]=‘el’

Word[-3:]=‘lpA’

Page 18: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

"C:/Stockholm.mdb"

Page 19: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

arcpy.Buffer_analysis

Page 20: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

"C:/XY.txt"

Page 21: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Accessing Python modules

( "C:/student" )

( "C:/student/Streets.shp" ) returns "Streets.shp"

( "C:/student/Streets.shp" ) returns "C:/Student"

Page 22: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 23: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 24: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks
Page 25: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

"C:/STUDENT" = "c:/StUdEnT"

arcpy.BUFFER = arcpy.buffer

Page 26: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Introduction to

Python and ArcGIS for

Geoprocessing Scripts

Lecture 10

Page 27: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Python: An open-source programming language

Page 28: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Many places for help, not just Python.org

Page 29: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Support comes in many languages

Page 30: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Search, and you will find many guides

Page 31: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Python as a first language

Page 32: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Python for Programmers

Page 33: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

As with any language: “Read the freaking manual”

Page 34: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

ArcGIS 10 installs Python 2.6.5

Page 35: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Only use ArcGIS 10 help / tutorials / examples

Version 9.3 is significantly different

Page 36: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Only use Python 2.6.5 help / tutorials / examples

Upgrading might break link with ArcGIS

Page 37: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

ESRI created Python routines to automate GIS

Page 38: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Pick any ArcGIS tool - help is not helpful

Page 39: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Tool Help is much better

Page 40: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Scroll to bottom for sample Python script

Page 41: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Python scripts are text files with “.py” extension

Page 42: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Create new document, copy / paste text

Page 43: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

For existing scripts, right click and edit with IDLE

Page 44: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

IDLE displays color-coded script

(Don't need the Python Shell, close it)

Page 45: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Example

Get GIS data

Page 46: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Example

Downloaded, processed, and symbolized layers

Page 47: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Gulf Of Mexico outline, plus monthly Chlorophyll

concentration from Terra, SeaWiFS, and Aqua

Page 48: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Edit script to match data location and names

Page 49: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Setup

Run Python from Windows Command Prompt

Page 50: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Setup

Easier to create a customized shortcut

Page 51: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Setup

Start Command Prompt in same folder as script

Page 52: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Desktop and Command Prompt ready

Page 53: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Run... no feedback (maybe you like that)

Page 54: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Script created “Mean” datafiles

Page 55: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Open in ArcMap... unsymbolized

Page 56: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

ArcPython does not like to overwrite files

Page 57: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Add status messages, create symbolize layer

Page 58: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Run again... feedback shows

Page 59: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Script created “Mean” datafiles and layer

Page 60: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Open in ArcMap... symbolized

Page 61: Writing Geoprocessing Scripts With ArcGIS - UTSA - Python.pdf · Writing Geoprocessing Scripts With ArcGIS Lecture 10 . GIS System . ArcObjects ArcObjects are the building blocks

Do not be fooled by simple problems described

and solved by scripts in beginner textbooks

Try manually processing multiple GB of data with

dozens of processing steps, EVERY DAILY

Python and ArcGIS are powerful tools