ArcObjects_Pyton

51
www.geonorth.com Value Through Integration and Innovation GIS and Engineering • Mark Pearson, PE (civil) • GeoNorth, LLC – GIS, Database, Internet Consulting – Anchorage, Portland – 13 Years Old (91 DOGS Years) – App Development, Planning, Conversion – Huge to Tiny Projects/Organizations

Transcript of ArcObjects_Pyton

Page 1: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

GIS and Engineering

• Mark Pearson, PE (civil)

• GeoNorth, LLC– GIS, Database, Internet Consulting– Anchorage, Portland– 13 Years Old (91 DOGS Years)– App Development, Planning, Conversion– Huge to Tiny Projects/Organizations

Page 2: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

The Plan

• ArcObjects– Concepts– Examples

• Python– Concepts– Examples

• Compare/Contrast

• Discussion

Page 3: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Multiple Ways to Skin the Cat

Page 4: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects: Serious Programming

• AML– Throw commands into text file and you’re a

programmer

• Avenue– Proprietary Language

Page 5: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Page 6: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects

• Not a Language• Building Blocks Upon

which ArcGIS was built• COM objects • Most commonly used

with VisualBasic or C++

Page 7: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects: First Rule

Whining

Page 8: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

VisualBasic

Page 9: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Modeling the Real World

• Objects

Page 10: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Object Oriented

Map Car

Property MapUnits Horsepower

Method AddLayer Accelerate

Event SelectionChanged Brakes are pressed

Page 11: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Object Orientation

• Properties– Characteristics (nouns)

• Methods– Actions object knows how to perform (verbs)

• Events– Occurrences that Object can respond to

Page 12: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Objects Vs. Classes

Page 13: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Objects Vs. Classes

• Class is the “Blueprint”

• Object is the “House”– An instantiation

Page 14: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Interface vs. Objects

Page 15: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Interfaces vs. Objects

Dim pFeatureLayer As IFeatureLayerDim pAttributeTable as IAttributeTableSet pFeatureLayer = new FeatureLayerSet pAttributeTable = pFeatureLayer ‘Query Interface

Page 16: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Tips

• Learn all of the on-line resources and use them

arcobjectsonline.esri.com

Page 17: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Tips

• Use Samples– Lots of Them– Almost Never Start from Scratch

Page 18: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Tips

• Training is Worth It for quick jump start

• We have an ArcObjects course in March (hint)

Page 19: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Tips

• Spend Time on Error Handling

Page 20: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects

• For hard-core programmers = Nirvana

• For casual programmers = Purgatory

• Non programmers = Hell

Page 21: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects: Before & After

Before ArcObjects After ArcObjects

Page 22: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects

• Up Side: Granularity of Control

• Down Side: Nothing is Easy*

* For the casual user

Page 23: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Common Implementations

• Quick & Dirty Automation using VBA within ArcMap

• Separate stand-alone applications

• ‘Extensions’ to ArcGIS framework– Compiled DLL(s)

Page 24: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Example in ArcObjects

• Example: Programmatically Turn a Layer Off

Page 25: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Private Sub TurnOffLayer() Dim pDoc As IMxDocument Dim pMap As IMap Dim pLayer As ILayer Dim pActiveView As IActiveView

Set pDoc = ThisDocument Set pMap = pDoc.FocusMap Set pLayer =

Page 26: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Private Function FindLayerByName(strLayerName As String, pMap As IMap) As ILayer Dim i As Integer For i = 0 To pMap.LayerCount - 1 If UCase(pMap.Layer(i).Name) = UCase(strLayerName) Then Set FindLayerByName = pMap.Layer(i) Exit Function End If Next i Set FindLayerByName = Null

End Function

Page 27: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Private Sub TurnOffLayer() Dim pDoc As IMxDocument Dim pMap As IMap Dim pLayer As ILayer Dim pActiveView As IActiveView

Set pDoc = ThisDocument Set pMap = pDoc.FocusMap Set pLayer = FindLayerByName("parcels", pMap) pLayer.Visible = False

End Sub

Set pActiveView = pMap ‘this is a query interface pActiveView.Refresh pActiveView.ContentsChanged

Page 28: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects 8.x to 9.0

• Good News: not much pain

• Binary Compatibility for most core-level libraries

• Not like Avenue versus ArcObjects

Page 29: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Type Libraries: Diced Up

• Esricore.olb was one huge library

• Now slit into functional groups

Page 30: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Type Libraries: Diced Up

Declarations

Dim pEnv as esriCore.IEnvelope Dim pEnv as esriGeometry.IEnvelope

ZoomToPoint (inPt as esricore.IPoint) ZoomToPoint(inPt As esriGeometry.IPoint)

Instantiations

Set pPt = new esriCore.Point set pPt = New esriGeometry.Point

Implementations

Implements esricore.ITool Implements esriFramework.ITool

ArcObjects 8.x ArcObjects 9.0

Page 31: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcGIS 9.0 Type LibrariesArcGIS Engine UI •esriSystemUI•esriDisplayUI•esriOutputUI•esriGeoDatabaseUI•esriDataSourcesRasterUI•esriLocationUI•esriCartoUI •esriCadUI•esriIMSUI •esriTinUI•esriCatalogUI ArcGIS Application assemblies•esriArcMapUI•esriArcMap•esriArcCatalogUI•esriArcCatalog•esriEditor•esriEditorExt•esriGeoDatabaseDistributedUI•esriGeoReferenceUI

ArcGIS EngineesriSystemesriGeometryesriDisplayesriOutputesriGeoDatabaseesriDataSourcesFileesriDataSourcesGDBesriDataSourcesOleDBesriDataSourcesRasteresriGeoDatabaseDistributedesriLocationesriCarto esriCadesriIMS esriTinesriWorker esriFramework

Page 32: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Code Converter Utility

• VB6 Add-In• Removes ESRI Object Library from Project

reference• Replaces ‘esriCore.’ with appropriate name in:

• Declarations• Instantiations• Implementations• ProgIDs

• Adds appropriate new references according code that is found

Page 33: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Tips for Healthy ArcObjects Use

• Never over promise amount of time

• Always plagiarize Code• ArcObjects Online / Samples• Knowledgebase• Your own code

• Keep a ‘Toolbox’ of common routines

• Make subs/functions generic whenever you can

Page 34: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

•Python/Scripting

Page 35: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Scripting in ArcGIS

• Python

• VBScript

• Perl

• Jscript

• Any scripting language that is COM compliant

• Python is ESRI-prefered

Page 36: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Why Python?

• Easy to learn, clean, clear syntax

• Supports Object Oriented programming

• Works well with complicated data structures

• Simple to integrate with C++ and Fortram

• Can be seamlessly integrated with Java

• Free, Open Source, Widely Used

Page 37: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Python

• Not invented by ESRI

• Interpreted, interactive, object-oriented

• Invented in 1991 by Guido von Rossum, Netherlands

• Named after Monty Python

• www.python.org

• Platforms: Windows, all UNIX, OS/2, Mac, Amiga – even Nokia cell phones

Page 38: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Geoprocessing with Python

• Geoprocessor Programming Model

Page 39: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Scripts in ArcToolBox

Page 40: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Importing Modules

• Python scripts are Modules• Use the ‘import’ statement to load and use

modules• All geoprocessing scripts use the ‘win32com’

module– Supports communciation between Python and

the COM IDispatch interface, which is used by the geoprocessor

• Other modules to math, file I/O, OS stuff, etc.

Page 41: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Standard Stuff at in a Python Script

import win32com.client, sys, os– sys is python system, user input, etc.

– Os does OS-specific file manipulation

#Create the Geoprocessor object

GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")– Declares and creates geoprocessing object that you will use later

#Set the input workspace

GP.workspace = “c:\data\anchorage\shapes”

GP.workspace = sys.argv[1]

Page 42: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Indentation and Spaces

• They are relevant!

try:

#get a list of featureclasses in workspace

fcs = GP.ListFeatureClasses()

Python is also case sensitive !!!

gp is not the same as GP

Page 43: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Batch Processing

• ListDatasets (Wild Card, Dataset Type)• ListFeatureClasses (Wild Card, Feature Type)• ListFields (Input Value, Wild Card, Field Type)• ListIndexes (Input Value, Wild Card)• ListRasters (Wild Card, Raster Type)• ListTables (Wild Card, Table Type)• ListWorkspaces (Wild Card, Workspace Type)

• All return an Enumeration (list with unknown count)

Page 44: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Batch Example

# Import COM Dispatch module

from win32com.client import Dispatch

# Create the geoprocessor object

GP = Dispatch("esriGeoprocessing.GPDispatch.1")

# Set the workspace. List all of the feature classes that start with 'G'

GP.Workspace = “c:/data/anchorage/anch.mdb”

fcs = GP.ListFeatureClasses("G*")

# or for a type: fcs = GP.ListFeatureClasses("G*","polygon")

Page 45: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Batch Example

# Reset the enumeration to make sure the first object is returnedfcs.reset()# Get the first feature class namefc = fcs.next()while fc: # While the feature class name is not None # Copy the features from the workspace to a folder GP.Copy(fc,“c:\data\anchorage\dataout\” + fc) # Get the next feature class name fc = fcs.next()

Page 46: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Licensing

• When a tool is executed, either thru the ArcToolbox or thru Script, an ArcGIS Desktop license is required

• Extension Licenses, too

Page 47: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Extending Python with Wrappers

• Vector Formats– pyshapelib (shapelib.maptools.org)

• Access to individual vertices of the shape• Dbf file• Indexing

– OGR (gdal.maptools.org/ogr)• Other vector formats like MapInfo, Coverage,

PostGIS, Oracle Spatial, TIGER, SDTS, OpeNDAP, DGN, Microstation DGN

Page 48: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Extending Python with Wrappers

• Grids– Geodata Abstration Library GDAL

(gdal.maptools.org)• JPEG2000, BSP, USGS DEM, Military• Elevation Data, ECW, GRASS, TIFF/GeoTIFF,

NetCDF, Imagine, and SDTS

Page 49: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Extending Python with Wrappers

• Projections– pyProjection

(http://hobu.biz/index_html/software/pyprojection)• For projecting coordinates from one to another• Uses EPSG code system and/or you can roll your own

• SDE– PySDE (hobu.stat.aistate.edu/pysde)

• Wrapper for SDE C API and has corresponding Python methods

Page 50: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

ArcObjects or Python?

• Rearrange the ArcGIS User Interface?

• Batch project a bunch of feature classes?

• Create new tool to edit special Widget feature classes?

• Create an ‘Extension’ for ArcGIS?

• Automated Map Book Generation

Page 51: ArcObjects_Pyton

www.geonorth.comValue Through Integration and Innovation

Thanks