07.05.2013 Views

Using Python with ArcGIS

Using Python with ArcGIS

Using Python with ArcGIS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> <strong>Python</strong> <strong>with</strong> <strong>ArcGIS</strong><br />

Drew Flater, Nobbir Ahmed<br />

Offering 184


Agenda<br />

<strong>Python</strong> essentials<br />

Arcpy, functions & classes<br />

Script geoprocessing workflows<br />

Automate map management & production<br />

Customize Desktop <strong>with</strong> <strong>Python</strong> Add-ins<br />

Analyze rasters <strong>with</strong> map algebra<br />

Extend <strong>ArcGIS</strong> <strong>with</strong> <strong>Python</strong>


<strong>Python</strong> Essentials<br />

Getting started <strong>with</strong> <strong>Python</strong><br />

in <strong>ArcGIS</strong>


What is <strong>Python</strong>?<br />

“<strong>Python</strong> is an easy to learn, powerful language…<br />

(<strong>with</strong>) high-level data structures and a simple but<br />

effective approach to object-oriented<br />

programming. <strong>Python</strong>’s elegant syntax and<br />

dynamic typing…make it an ideal language for<br />

scripting…in many areas and on most platforms.”<br />

–python.org<br />

Scripting language of <strong>ArcGIS</strong><br />

Free, cross-platform, easy to learn, widely<br />

useful, great community


<strong>Python</strong> 101<br />

Where do I write <strong>Python</strong> code?<br />

<strong>Python</strong> file is text <strong>with</strong> .py extension<br />

Edit in IDE like PyScripter, Wing IDE<br />

Logic for testing conditions<br />

If, else statement<br />

Operators like >,


ArcPy<br />

Site package included <strong>with</strong> <strong>ArcGIS</strong> that enables interaction<br />

<strong>with</strong> <strong>Python</strong><br />

The access point to 900+ geoprocessing tools<br />

A package of functions, classes and modules<br />

Helper functions that perform useful tasks and enable automation<br />

(ListFeatureClasses, Describe, SearchCursor)<br />

Classes that can be used to create complex objects<br />

(SpatialReference, Geometry, FieldMap)<br />

Modules that provide specialized functionality<br />

(mapping, SpatialAnalyst, data access)<br />

Enhancement of arcgisscripting module (pre-10.0)<br />

Your old scripts will work


Why use <strong>Python</strong> and <strong>ArcGIS</strong>?<br />

Automate repetitive tasks<br />

Batch processing<br />

Develop workflows that<br />

leverage hundreds of tools<br />

and functions<br />

Add geoprocessing services<br />

to your application<br />

Customize Desktop apps <strong>with</strong><br />

a language Esri is committed<br />

to support<br />

Extend the capabilities of<br />

<strong>ArcGIS</strong>


Script<br />

Geoprocessing<br />

Workflows<br />

Calling geoprocessing tools<br />

through ArcPy and stringing<br />

them together


Geoprocessing and <strong>Python</strong><br />

import arcpy<br />

Follow tool syntax<br />

arcpy.toolname_toolboxalias()<br />

Enter input and output parameters<br />

How do I use a specific tool?<br />

Tool help page<br />

Copy as <strong>Python</strong> Snippet<br />

help(arcpy.Buffer_analysis)<br />

Use geoprocessing environments as<br />

global parameters<br />

Accessed from arcpy.env<br />

Productivity / code cleanup tool<br />

workspace, extent, cellsize, mask


Demo<br />

Script<br />

Geoprocessing<br />

Workflows<br />

Perform batch processing, and<br />

string together multiple tools to<br />

perform spatial analysis


Tips & Tricks<br />

Use the result object of geoprocessing tools<br />

Returned by all tools<br />

Maintains messages, parameters, and outputs<br />

result = arcpy.Buffer_analysis(…)<br />

Write intermediate tool output to in_memory workspace<br />

Automatic cleanup<br />

Faster read-write


Automate Map<br />

Management &<br />

Production<br />

Working <strong>with</strong> maps and layers<br />

using arcpy.mapping


ArcPy Mapping module<br />

arcpy.mapping<br />

Module that contains functions and classes used to<br />

automate mapping tasks<br />

Manage map documents, layers, and data<br />

Find and fix broken data sources<br />

Update layer symbology across many maps<br />

Export and print map documents<br />

Automate map production / map series


ArcPy Mapping module<br />

MapDocument object is essential<br />

References .mxd on disk; has methods and properties<br />

Needed to perform most mapping tasks<br />

MapDocument as input to function<br />

Functions called from MapDocument<br />

md = arcpy.mapping.MapDocument("…/NtlParks.mxd")<br />

# Set map document properties<br />

md.description = "Map of National Parks"<br />

# List layers in the map<br />

mapLayers = arcpy.mapping.ListLayers(md)<br />

# Fix Data Sources<br />

md.replaceWorkspaces(…)<br />

# Export the map to PDF<br />

arcpy.mapping.ExportToPDF(md, "…/NtlParks.pdf")


Mapping module resources<br />

Download sample tools


Demo<br />

Automate Map<br />

Management &<br />

Production<br />

Make a multi-page map book<br />

using arcpy.mapping and data<br />

driven pages


Customize <strong>ArcGIS</strong><br />

Desktop <strong>with</strong><br />

<strong>Python</strong> Add-ins<br />

Run <strong>Python</strong> code in response to<br />

button clicks and application<br />

events


<strong>ArcGIS</strong> Desktop Add-ins<br />

Add-in framework provided to customize and extend <strong>ArcGIS</strong><br />

Desktop applications<br />

Easy to build, install and share<br />

Secured through digital signing<br />

Supports C#, VB.NET, Java, and <strong>Python</strong><br />

<strong>Python</strong> makes add-ins easier!<br />

No dlls, compiling, or ArcObjects, and less code


<strong>Python</strong> Add-in Types<br />

Button<br />

Tool<br />

Toolbar<br />

Tool Pallet<br />

Combo Box<br />

Menu<br />

Extension<br />

Dockable windows are not supported<br />

No custom UI support


<strong>Python</strong> Add-In Classes and Methods<br />

Button<br />

Tool<br />

onClick()<br />

onCircle(), onLine(), onRectangle()<br />

onMouseDown(), onMouseDownMap()<br />

Application Extension<br />

startup()<br />

newDocument(), openDocument(), closeDocument()<br />

contentsChanged(), itemAdded(), itemDeleted()<br />

Full documentation of all functions and classes


<strong>Python</strong> Add-In Wizard<br />

Add-ins are built using the <strong>Python</strong> Add-in wizard<br />

The wizard generates fully stubbed out add-in projects<br />

including the config.xml, folders, and the <strong>Python</strong> script<br />

Download the <strong>Python</strong> Wizard from arcgis.com<br />

Extract the contents of the .zip<br />

Launch the addin_assistant.exe from the bin folder


Demo<br />

Customize <strong>ArcGIS</strong><br />

Desktop <strong>with</strong><br />

<strong>Python</strong> Add-ins<br />

<strong>Python</strong> Add-in toolbar <strong>with</strong><br />

buttons to select and zoom to<br />

next or previous feature


Analyze Rasters<br />

<strong>with</strong> Map Algebra<br />

<strong>Using</strong> arcpy.sa (Spatial Analyst)<br />

to efficiently work <strong>with</strong> rasters


Spatial Analyst Module<br />

from arcpy.sa import *<br />

arcpy.CheckOutExtension("Spatial")<br />

Includes all Spatial Analyst tools<br />

Helper classes that can be used to support complex<br />

parameter in scripting<br />

Integrates Map Algebra into <strong>Python</strong><br />

Defines geographic analysis as algebraic expressions<br />

Supports mathematical, relational, other operators<br />

Output on the left-side<br />

slopeRas = Slope("elevRas") * 100.0


Raster Class<br />

Reference to raster on disk, created in two ways:<br />

Returned output from arcpy.sa functions<br />

Cast using arcpy.Raster() function<br />

Necessary for map algebra expressions using operators<br />

Temporary raster dataset that can be saved<br />

Has properties and method<br />

raster.minimum, raster.format, raster.extent<br />

raster.save()<br />

dem = arcpy.Raster("Elevation_Meters.tif"))<br />

demFt = dem * 3.28<br />

# Rescale elevation to 0 – 1 scale<br />

rescale = (dem - dem.minimum)/(dem.maximum-dem.minimum)<br />

rescale.save("Elevation_Rescale_0_1.tif")


Demo<br />

Analyze Rasters<br />

<strong>with</strong> Map Algebra<br />

Use raster processing <strong>with</strong><br />

<strong>Python</strong> to perform probabilitybased<br />

site suitability


Extend <strong>ArcGIS</strong><br />

<strong>with</strong> <strong>Python</strong><br />

<strong>Using</strong> <strong>Python</strong> to do stuff that's<br />

not in the <strong>ArcGIS</strong> box


<strong>Python</strong> packages<br />

<strong>ArcGIS</strong> includes several 3 rd -party <strong>Python</strong> packages and<br />

modules<br />

No separate install or config required, just import<br />

NumPy Statistical computation, powerful array object<br />

Matplotlib Data presentation and graphing<br />

Xlrd, Xlwt Excel spreadsheet read/write (new at 10.2)<br />

PyPI - official <strong>Python</strong> Package Index<br />

Vast collection of 3 rd -party packages <strong>with</strong> easy browse/search<br />

Windows binaries<br />

UC-Irvine maintains unofficial package installs for Windows<br />

"A simple approach for including 3 rd party <strong>Python</strong> libraries <strong>with</strong> your scripts"


NumPy<br />

NumPy is useful for mathematical &<br />

statistical computation<br />

Feature, table, and raster data can be<br />

converted to NumPy arrays<br />

1 3<br />

2 4<br />

1 3<br />

2 4<br />

3<br />

4<br />

Numpy Array<br />

To NumPy From <strong>ArcGIS</strong> To <strong>ArcGIS</strong> From NumPy<br />

3<br />

4<br />

Raster<br />

arcpy.da.FeatureClassToNumPyArray arcpy.da.NumPyArrayToFeatureClass<br />

arcpy.da.TableToNumPyArray arcpy.da.NumPyArrayToTable<br />

arcpy.RasterToNumPyArray arcpy.NumPyArrayToRaster


<strong>ArcGIS</strong>, <strong>Python</strong>, and Beyond<br />

Use <strong>Python</strong> as a communication language between <strong>ArcGIS</strong><br />

and other APIs, packages, or applications<br />

"Extend <strong>ArcGIS</strong> <strong>with</strong> R"<br />

"Call a dll from a script tool using ctypes"<br />

<strong>Python</strong>…<br />

Retrieves and organizes parameters/arguments from <strong>ArcGIS</strong><br />

Converts data as needed (shapefiles, IMG, NetCDF, etc.)<br />

Constructs strings and uses operating system or class for execution<br />

After execution, applies symbology, coordinate system, and creates<br />

reports


Demo<br />

Extend <strong>ArcGIS</strong><br />

<strong>with</strong> 3 rd -Party<br />

<strong>Python</strong> Libraries<br />

<strong>ArcGIS</strong> and NumPy roundtrip<br />

and using 3 rd -party packages


Resources<br />

resources.<strong>ArcGIS</strong>.com<br />

<strong>Python</strong> community<br />

arcpy.wordpress.com<br />

GIS Stack Exchange, Stack Overflow<br />

<strong>Python</strong> References<br />

Learning <strong>Python</strong> by Lutz<br />

The <strong>Python</strong> Standard Library by<br />

Example by Hellmann<br />

<strong>Python</strong> Scripting for <strong>ArcGIS</strong><br />

by Esri press<br />

diveintopython.org<br />

python.org<br />

Working <strong>with</strong> the ArcPy DA Module<br />

Wed 1:00pm – Smoketree A-E<br />

Creating Tools in a <strong>Python</strong> Toolbox<br />

Wed 2:30pm – Mesquite GH<br />

Creating Geoprocessing Services<br />

Tue 1:00pm – Smoketree A-E<br />

<strong>Python</strong> Scripting for Map Automation<br />

Wed 1:00 pm – Mojave Learning Ctr<br />

Developing <strong>Python</strong> Add-ins<br />

Tue 4:00pm – Primrose A<br />

Working <strong>with</strong> Raster Data <strong>Using</strong> Py<br />

Thu 8:30am – Primrose A<br />

Integrating Open Source Stats Pkgs<br />

Tue 1:30pm – Demo Theater 1, Oasis 1<br />

Offering 184

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!