Extending Python Doing more than just interfacing with the geoprocessor Free example utility for NZ...

Post on 27-Dec-2015

213 views 0 download

Transcript of Extending Python Doing more than just interfacing with the geoprocessor Free example utility for NZ...

Extending Python

Extending Python

Doing more than just interfacing with the geoprocessor

Free example utility for NZ Projections

Kim Ollivier

Extending Python

Gluing it all togetherstrong -> weak

• Your own Python modules for utilities

• Other Python modules eg ODBC, XML

• Other COM interfaces eg Excel

• ArcGIS toolbox for forms interface

• ESRI Geoprocessor COM object

• VBA for ArcMap graphical interaction

Extending Python

Helpers and Hints

• Good idea to use Python 2.4

• Python extensions do anything

• Early binding for COM (Makepy)

• Direct Python interfaces are best

• Samples toolbox in 9.1 are a start

• Maybe contribute your own?

Extending Python

Python Modules

• import mymodule – that’s all there is to it!• Would contain your own pythonic functions

and classes, documentation, test code• Port your arc-only aml utilities• My example module is called corax (etools

has gone)• Do not depend on toolbox validation

Extending Python

Creating an NZ Grid Index

1. Decide on the series scale2. Get the limits for the index: eg Prime name or

coordinates or interactive. For interactive input you have to use VBA, UITool, save results to a file.

3. Repeatedly call a function to get [name,extent]…for each sheet in the list

4. Output polys direct or Create a text file

5. Add as a layer or Create the index

Extending Python

Step 1 Make a list of sheets

• Get limits [xmin,ymin,xmax,ymax]

• Find the sheets at lower left and top right• [name,bound] = corax.nztmsheet(scale,x,y)

• Create a [list] of [name,x1,y1,x2,y2]…

• Name has 4 parts: prime-scale_col.row

Extending Python

Step 2 Get extents of sheets

• Use the module with custom functions

• import corax• [name,bounds] = corax.nztmsheet(scale,x,y)

• Add to [list] of [name,x1,y1,x2,y2]…

Extending Python

Step 3 Plan A : Write a text file

(If only points, load as XY events, export)

For lines and polygons :Two files, sample tool to load generate-like file, add

attributes.txt, join tables, export. Watch for extra spaces!!!

• Easier to code, but risks ms-click RSI

• For a one-off this is convenient enough

Extending Python

Step 3 Plan B : Create directly using gp…

• Delete the last featureclass!

• Define a projection object

• (NZTM or NZMG)

• Create a featureclass (shapefile)

• Add fields to hold sheet name parts

Extending Python

Step 4 Load the sheet rectangles

• ESRI sample adds layer, but no names

• Own module adds names but no layer

• Note names have “/” replaced with “_” to work with MapBook

• Don’t forget the projection definition

• What is a projection definition?

Extending Python

Review

• If you can create a text file, then use samples tool for a one-off project.

• No graphic interaction which means you will have to stay with ArcPlot amls.

• Projection definitions are obscure.

• Could not integrate it with MapBook.

• Could not call sample from another script.

Extending Python

Enhancement Requests

• Upgrade standard install to Python 2.4+

• Fix documentation errors and omissions

• Performance, usability, functionality

• Add COM error messages

• More support for Win32 Python

• Interaction with a map

• An ArcMap COM interface

Extending Python

References

• The bible for COM interfaces and Python

• “Python Programming on Win32” - Mark Hammond, O’Reilly Press

• www.ollivier.co.nz/support