Using ArcObjects in Python. Mark Cederholm UniSource Energy Services



Similar documents
ArcGIS. Writing Geoprocessing Scripts With ArcGIS

python alarm system manual

ArcGIS Pro. James Tedrick, Esri

Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

ESRI Mobile GIS Solutions Overview. Shane Clarke ESRI

CAPIX Job Scheduler User Guide

Network Analysis with ArcGIS for Server

Customizing ArcPad solutions

Network Analysis with Python. Deelesh Mandloi

A Comparative Analysis of Programming Languages for GIS

ArcGIS Business Analyst - An Introduction. Jason Channin jchannin@esri.com

SUMMER SCHOOL ON ADVANCES IN GIS

The Courses. Covering complete breadth of GIS technology from ESRI including ArcGIS, ArcGIS Server and ArcGIS Engine.

Synchronizing Smallworld Data with ArcGIS

Visualization of LODES/OnTheMap Work Destination Data Using GIS and Statistical applications

GIS I Business Exr02 (av 9-10) - Expand Market Share (v3b, Jul 2013)

Introduction to the ArcGIS Data Model and Application Structure

ArcGIS Server Performance and Scalability Optimization and Testing. Andrew Sakowicz

Note: Hands On workshops are Bring Your Own Laptop (BYOL), unless otherwise noted. Some workshops are Bring Your Own Mobile Device(BYOD).

Installing ArcGIS Desktop 10.0: Student Evaluation Setup Guide. June 2014

ArcGIS Server Best Practices and Guidelines

ArcGIS 10.1 Geodatabase Administration. Gordon Sumerling & Christopher Brown

Getting Started with IVI-COM and Python for the Lambda Genesys Power Supply

ArcGIS. Server. A Complete and Integrated Server GIS

Data Integration for ArcGIS Users Data Interoperability. Charmel Menzel, ESRI Don Murray, Safe Software

GIS and Mapping Solutions for Developers. ESRI Developer Network (EDN SM)

How To Improve Gis Data Quality

INTRODUCTION TO ARCGIS SOFTWARE

Publishing Geoprocessing Services Tutorial

ArcGIS ArcMap: Printing, Exporting, and ArcPress

Software Installation Instructions for the 1-year Student License of ArcGIS Desktop 9.3.1

Managing Lidar (and other point cloud) Data. Lindsay Weitz Cody Benkelman

An Esri Technical Paper June 2010 ArcGIS 10 Enterprise Deployment

BLM Personnel & REA Collaborator Access. Accessing REA Data, Maps, & Models through SharePoint

Chapter 1: Introduction to ArcGIS Server

ArcGIS for Server Performance and Scalability-Testing and Monitoring Tools. Amr Wahba

Board also Supports MicroBridge

Automated Map Production Workflows. Aileen Buckley and David Watkins and ESRI, Inc., U.S.A.

Export Server Object Extension and Export Task Install guide. (V1.1) Author: Domenico Ciavarella ( )

An ESRI Technical Paper July 2008 ArcGIS 9.3 Enterprise Deployment

Developing Apps with the ArcGIS Runtime SDK for Android. Ben Ramseth Esri Inc. Instructor Technical Lead

Geodatabase Programming with SQL

r-one Python Setup 1 Setup 2 Using IDLE 3 Using IDLE with your Robot ENGI 128: Introduction to Engineering Systems Fall, 2014

TUTORIAL ECLIPSE CLASSIC VERSION: ON SETTING UP OPENERP 6.1 SOURCE CODE UNDER WINDOWS PLATFORM. by Pir Khurram Rashdi

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

ArcGIS 10.3 Enterprise Deployment. An Esri Technical Paper December 2014

Enterprise GIS Solutions to GIS Data Dissemination

BarTender s ActiveX Automation Interface. The World's Leading Software for Label, Barcode, RFID & Card Printing

Running and Scheduling QGIS Processing Jobs

ArcGIS 10.2 Enterprise Deployment. An Esri Technical Paper August 2013

ArcGIS Server and Geodatabase Administration for 10.2

BarTender s.net SDKs

Workflow improvement with FME in Skedsmo municipality

ArcGIS 10.1: The Installation and Authorization User Guide

An ESRI Technical Paper April 2009 ArcGIS Server in Practice Series: Large Batch Geocoding

ArcGIS for Server: Administrative Scripting and Automation

Python for Series 60 Platform

University of Arkansas Libraries ArcGIS Desktop Tutorial. Section 4: Preparing Data for Analysis

Personal Geodatabase 101

Installation Manual v2.0.0

NetBrain Enterprise Edition 6.0a NetBrain Server Backup and Failover Setup

Working with the Geodatabase Using SQL

ArcGIS. Tips and Shortcuts. for Desktop

Developing Web Apps with ArcGIS API for JavaScript TM. Student Edition

ArcGIS Business Analyst Premium* ~ Help Guide ~ Revised October 3, 2012

Quick Start Guide to. ArcGISSM. Online

ArcGIS for Desktop Best Practices in a Citrix XenApp Environment. Jeff DeWeese Sr. Technical Architect Esri January 4, 2013

Python and Cython. a dynamic language. installing Cython factorization again. working with numpy

HP AppPulse Mobile. Adding HP AppPulse Mobile to Your Android App

latest Release 0.2.6

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Database Servers Tutorial

Introduction to ArcGIS Network Analyst. Presenter: Matt Crowder ESRI Redlands, California

Getting your app together with Web AppBuilder for ArcGIS

Microsoft Visual Basic Scripting Edition and Microsoft Windows Script Host Essentials

Performance Testing in Virtualized Environments. Emily Apsey Product Engineer

Triple-E class Continuous Delivery

Tips & Tricks for Using Key Windows Vista Native APIs from Managed Code

NetCDF and HDF Data in ArcGIS

Basic Android Setup Windows Version

2 Working with a Desktop GeoDatabase

CS 1133, LAB 2: FUNCTIONS AND TESTING

PostgreSQL Functions By Example

Windows PowerShell Cookbook

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

ArcGIS for Server in the Cloud

Working with Temporal Data

Cookbook 23 September 2013 GIS Analysis Part 1 - A GIS is NOT a Map!

Vault Project - Plant Database Replication. Contents. Software Requirements: AutoCAD Plant 3D 2016 and AutoCAD P&ID 2016

ArcGIS Viewer for Silverlight An Introduction

Introduction to Web AppBuilder for ArcGIS: JavaScript Apps Made Easy

MatrixSSL Getting Started

Spatial data models (types) Not taught yet

A GP Service for Enterprise Printing. Kevin Shows, Anadarko Petroleum Kirk Kuykendall, Idea Integration 04/20/2011

Installation and initial configuration of UI View32, with PMap Server 7 and Precision Mapping Streets and Traveler 8.0 on Microsoft Vista

Subversion Integration


Transcription:

Using ArcObjects in Python Mark Cederholm UniSource Energy Services

Why Python? ArcGIS VBA support ends after 10.0 At 10.0, ArcMap and ArcCatalog include an integrated Python shell Python scripting objects provided by ESRI IDLE is a decent development and debugging environment Python scripts can use ArcObjects!

Geoprocessing objects Ready-to to-use geoprocessing objects are available for Python through arcgisscripting (9.3) and arcpy (10.0) At 9.3: additional functionality includes data access objects such as cursors At 10.0: additional functionality includes some map document automation Nonetheless, a great deal of functionality is only available through ArcObjects

COM interop: relative speed Benchmark = 500+K ShapeCopy operations (ArcGIS 9.3.1 with VS2008)

Demo: Standalone scripting

The comtypes package Available for download at: http://sourceforge.net/projects/comtypes/ Download and run installer; or else download zip file, unzip, and enter this line at the command prompt: python setup.py install See also this link for documentation: http://starship.python.net/crew/theller/comtypes/

Loading and importing modules def GetLibPath(): ##return "C:/Program Files/ArcGIS/com/" import _winreg keyesri = _winreg.openkey(_winreg.hkey_local_machine, \ "SOFTWARE\\ESRI\\ArcGIS") return _winreg.queryvalueex(keyesri, "InstallDir")[0] + "com\\" def GetModule(sModuleName): import comtypes from comtypes.client import GetModule slibpath = GetLibPath() GetModule(sLibPath + smodulename) GetModule("esriGeometry.olb") import comtypes.gen.esrigeometry as esrigeometry [or] from comtypes.gen.esrigeometry import Point, IPoint [import * is not recommended]

Creating and casting objects def NewObj(MyClass, MyInterface): from comtypes.client import CreateObject try: ptr = CreateObject(MyClass, interface=myinterface) return ptr except: return None def CType(obj, interface): try: newobj = obj.queryinterface(interface) return newobj except: return None def CLSID(MyClass): return str(myclass._reg_clsid_)

Standalone licensing pinit = NewObj(esriSystem.AoInitialize, \ esrisystem.iaoinitialize) eproduct = esrisystem.esrilicenseproductcodearceditor licensestatus = pinit.isproductcodeavailable(eproduct) if licensestatus == esrisystem.esrilicenseavailable: licensestatus = pinit.initialize(eproduct) return (licensestatus == esrisystem.esrilicensecheckedout) TIP: Use the geoprocessing object instead import arcgisscripting gp = arcgisscripting.create(9.3) gp.setproduct("arceditor")

Demo: Manipulating an existing ArcMap or ArcCatalog session

Retrieving an existing session from outside the application boundary if not (app == "ArcMap" or app == "ArcCatalog"): return None papprot = NewObj(esriFramework.AppROT, esriframework.iapprot) icount = papprot.count if icount == 0: return None for i in range(icount): papp = papprot.item(i) if app == "ArcCatalog": if CType(pApp, esricatalogui.igxapplication): return papp continue if CType(pApp, esriarcmapui.imxapplication): return papp return None

Getting a selected feature papp = GetApp()... pdoc = papp.document pmxdoc = CType(pDoc, esriarcmapui.imxdocument) pmap = pmxdoc.focusmap pfeatsel = pmap.featureselection penumfeat = CType(pFeatSel, esrigeodatabase.ienumfeature) penumfeat.reset() pfeat = penumfeat.next() if not pfeat: print "No selection found." return pshape = pfeat.shapecopy etype = pshape.geometrytype if etype == esrigeometry.esrigeometrypoint: print "Geometry type = Point"...

Creating session objects with IObjectFactory If manipulating a session from outside the application boundary, use IObjectFactory to create new session objects: papp = GetApp() pfact = CType(pApp, esriframework.iobjectfactory) punk = pfact.create(clsid(esricarto.textelement)) ptextelement = CType(pUnk, esricarto.itextelement) TIP: At 10.0, you can run a script within the session's Python shell and create objects normally; use AppRef to get the app handle papp = NewObj(esriFramework.AppRef, esriframework.iapplication)

UIDs and Enumerations papp = GetApp()... pid = NewObj(esriSystem.UID, esrisystem.iuid) pid.value = CLSID(esriEditor.Editor) pext = papp.findextensionbyclsid(pid) peditor = CType(pExt, esrieditor.ieditor) if peditor.editstate == esrieditor.esristateediting: pws = peditor.editworkspace pds = CType(pWS, esrigeodatabase.idataset) print "Workspace name: " + pds.browsename print "Workspace category: " + pds.category Multiple Return Values iedgeeid, breverse, oweight = pforwardstar.queryadjacentedge(i)

Nothing, IsNull, and None Supply None as an argument representing Nothing: iopt = esricarto.esriviewgraphics + \ esricarto.esriviewgraphicselection pactiveview.partialrefresh(iopt, None, None) Use boolean testing to check for a null pointer, and is None to check for a null DB value: pcursor = ptab.search(pqf, True) prow = pcursor.nextrow() if not prow: print "Query returned no rows" return Val = prow.value(ptab.findfield(sfieldname)) if Val is None: print "Null value"

WriteOnly and indexed properties pnewfield = NewObj(esriGeoDatabase.Field, \ esrigeodatabase.ifield) pfieldedit = CType(pNewField, esrigeodatabase.ifieldedit) pfieldedit._name = "LUMBERJACK" pfieldedit._type = esrigeodatabase.esrifieldtypestring pfieldedit._length = 50 pfieldsedit._field[1] = pnewfield pouttable = pfws.createtable(stablename, poutfields, \ None, None, "") ifield = pouttable.findfield("lumberjack") print "'LUMBERJACK' field index = ", ifield prow = pouttable.createrow() prow.value[ifield] = "I sleep all night and I work all day" prow.store() TIP: Use geoprocessing tools to create tables and add fields

Demo: Extending ArcGIS Desktop

Creating a COM object 1. Create an IDL file defining the object and its interfaces 2. Compile with the MIDL compiler (part of the Windows SDK download) to produce a TLB file: midl DemoTool.idl 3. Implement the class and category registration in a Python module 4. Register the com object: python DemoTool.py regserver WARNING: The file/module name in step 4 is case sensitive!

Some final tips: When in doubt, check the wrapper code: Python25/Lib/site-packages/comtypes/gen Avoid intensive use of fine-grained ArcObjects in Python For best performance, use C++ to create coarse- grained COM objects Use geoprocessing objects and tools to simplify supported tasks watch for performance, though Read the desktop help to check out available functionality in arcgisscripting (and arcpy at 10.0)

Questions? Mark Cederholm mcederholm@uesaz.com This presentation and sample code may be downloaded at: http://www.pierssen.com/arcgis/misc.htm