writing geoprocessing scripts with arcgis

61
Writing Geoprocessing Scripts With ArcGIS Lecture 10

Upload: duongtuyen

Post on 06-Jan-2017

241 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Writing Geoprocessing Scripts With ArcGIS

Writing Geoprocessing Scripts

With ArcGIS

Lecture 10

Page 2: Writing Geoprocessing Scripts With ArcGIS

GIS System

Page 3: Writing Geoprocessing Scripts With ArcGIS

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
Page 5: Writing Geoprocessing Scripts With ArcGIS
Page 6: Writing Geoprocessing Scripts With ArcGIS
Page 7: Writing Geoprocessing Scripts With ArcGIS
Page 8: Writing Geoprocessing Scripts With ArcGIS

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

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

arcpy.Clip_arc() arcpy.Clip_analysis()

Always assign names

to custom toolboxes

Page 11: Writing Geoprocessing Scripts With ArcGIS

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
Page 13: Writing Geoprocessing Scripts With ArcGIS

Python Basics

Page 14: Writing Geoprocessing Scripts With ArcGIS
Page 15: Writing Geoprocessing Scripts With ArcGIS

gdbPath = "C:/SouthAfrica.mdb"

fc = "Roads"

fullPath = gdbPath + "/" + fc

Page 16: Writing Geoprocessing Scripts With ArcGIS
Page 17: Writing Geoprocessing Scripts With ArcGIS

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

"C:/Stockholm.mdb"

Page 19: Writing Geoprocessing Scripts With ArcGIS

arcpy.Buffer_analysis

Page 20: Writing Geoprocessing Scripts With ArcGIS

"C:/XY.txt"

Page 21: Writing Geoprocessing Scripts With ArcGIS

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
Page 23: Writing Geoprocessing Scripts With ArcGIS
Page 24: Writing Geoprocessing Scripts With ArcGIS
Page 25: Writing Geoprocessing Scripts With ArcGIS

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

arcpy.BUFFER = arcpy.buffer

Page 26: Writing Geoprocessing Scripts With ArcGIS

Introduction to

Python and ArcGIS for

Geoprocessing Scripts

Lecture 10

Page 27: Writing Geoprocessing Scripts With ArcGIS

Python: An open-source programming language

Page 28: Writing Geoprocessing Scripts With ArcGIS

Many places for help, not just Python.org

Page 29: Writing Geoprocessing Scripts With ArcGIS

Support comes in many languages

Page 30: Writing Geoprocessing Scripts With ArcGIS

Search, and you will find many guides

Page 31: Writing Geoprocessing Scripts With ArcGIS

Python as a first language

Page 32: Writing Geoprocessing Scripts With ArcGIS

Python for Programmers

Page 33: Writing Geoprocessing Scripts With ArcGIS

As with any language: “Read the freaking manual”

Page 34: Writing Geoprocessing Scripts With ArcGIS

ArcGIS 10 installs Python 2.6.5

Page 35: Writing Geoprocessing Scripts With ArcGIS

Only use ArcGIS 10 help / tutorials / examples

Version 9.3 is significantly different

Page 36: Writing Geoprocessing Scripts With ArcGIS

Only use Python 2.6.5 help / tutorials / examples

Upgrading might break link with ArcGIS

Page 37: Writing Geoprocessing Scripts With ArcGIS

ESRI created Python routines to automate GIS

Page 38: Writing Geoprocessing Scripts With ArcGIS

Pick any ArcGIS tool - help is not helpful

Page 39: Writing Geoprocessing Scripts With ArcGIS

Tool Help is much better

Page 40: Writing Geoprocessing Scripts With ArcGIS

Scroll to bottom for sample Python script

Page 41: Writing Geoprocessing Scripts With ArcGIS

Python scripts are text files with “.py” extension

Page 42: Writing Geoprocessing Scripts With ArcGIS

Create new document, copy / paste text

Page 43: Writing Geoprocessing Scripts With ArcGIS

For existing scripts, right click and edit with IDLE

Page 44: Writing Geoprocessing Scripts With ArcGIS

IDLE displays color-coded script

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

Page 45: Writing Geoprocessing Scripts With ArcGIS

Example

Get GIS data

Page 46: Writing Geoprocessing Scripts With ArcGIS

Example

Downloaded, processed, and symbolized layers

Page 47: Writing Geoprocessing Scripts With ArcGIS

Gulf Of Mexico outline, plus monthly Chlorophyll

concentration from Terra, SeaWiFS, and Aqua

Page 48: Writing Geoprocessing Scripts With ArcGIS

Edit script to match data location and names

Page 49: Writing Geoprocessing Scripts With ArcGIS

Setup

Run Python from Windows Command Prompt

Page 50: Writing Geoprocessing Scripts With ArcGIS

Setup

Easier to create a customized shortcut

Page 51: Writing Geoprocessing Scripts With ArcGIS

Setup

Start Command Prompt in same folder as script

Page 52: Writing Geoprocessing Scripts With ArcGIS

Desktop and Command Prompt ready

Page 53: Writing Geoprocessing Scripts With ArcGIS

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

Page 54: Writing Geoprocessing Scripts With ArcGIS

Script created “Mean” datafiles

Page 55: Writing Geoprocessing Scripts With ArcGIS

Open in ArcMap... unsymbolized

Page 56: Writing Geoprocessing Scripts With ArcGIS

ArcPython does not like to overwrite files

Page 57: Writing Geoprocessing Scripts With ArcGIS

Add status messages, create symbolize layer

Page 58: Writing Geoprocessing Scripts With ArcGIS

Run again... feedback shows

Page 59: Writing Geoprocessing Scripts With ArcGIS

Script created “Mean” datafiles and layer

Page 60: Writing Geoprocessing Scripts With ArcGIS

Open in ArcMap... symbolized

Page 61: Writing Geoprocessing Scripts With ArcGIS

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