Python Workshop Guido van Rossum First impressions of

  • Slides: 17
Download presentation
Python Workshop Guido van Rossum First impressions of using Python with Arc. GIS OLLIVIER

Python Workshop Guido van Rossum First impressions of using Python with Arc. GIS OLLIVIER & CO Python Workshop

Workshop Steps 1 Why (not) Python? 2 Python and Arc. GIS 3 Examples 4

Workshop Steps 1 Why (not) Python? 2 Python and Arc. GIS 3 Examples 4 Extensions 5 Discussion OLLIVIER & CO Python Workshop

Why (not) Python? Python or Visual Basic for Applications (VBA)? Pro’s • You are

Why (not) Python? Python or Visual Basic for Applications (VBA)? Pro’s • You are comfortable with AML and Avenue • ESRI’s choice for scripting models and servers • Open source, extendable, system utilities, batch • Cross platform, modern, elegant, version 2. 3 • Compiled code, COM objects • Microsoft free environment OLLIVIER & CO Python Workshop

Why (not) Python? Python or Visual Basic for Applications (VBA)? Con’s • You are

Why (not) Python? Python or Visual Basic for Applications (VBA)? Con’s • You are comfortable with Visual Basic, C++, VBA and GIS ? ? • Need full access to Arc. Object Developer Kit • Need to write Arc. Map GUI Desktop custom applications that interact with the map objects and get coordinates • There may be a Microsoft only policy restriction at your site • You prefer repetitively clicking a mouse on nested forms OLLIVIER & CO Python Workshop

Python can be entirely independent of Arc. GIS • Operating system scripting • Hundreds

Python can be entirely independent of Arc. GIS • Operating system scripting • Hundreds of modules to use • Compile to a COM DLL with Win 32 module • Excellent Web, XML, Database modules • IDE - IDLE or Python. Win • Debugging, error handling, ‘reasonable defaults’, classes, threads - are all excellent • Web based support OLLIVIER & CO Python Workshop

buffer. py Two python scripts # first two lines required to use ESRI interface

buffer. py Two python scripts # first two lines required to use ESRI interface import win 32 com. client gp = win 32 com. client. Dispatch("esri. Geoprocessing. Gp. Dispatch. 1") gp. Workspace = “D: /workshop/wellington. mdb” # Buffer the road feature class by 100 metres. gp. Buffer_analysis (”Road", "Roadbuf", 100) spreadsheet. py # first line required to use windows system functions import win 32 com. client ex = win 32 com. client. Dispatch("Excel. Application") ex. Visible = 1 ex. Workbooks. Add() # for office 97 ex. Cells(1, 1). Value = "Hello World" OLLIVIER & CO Python Workshop

Python and Arc. GIS geoprocessor, describe, cursor, enumeration, other • • “Gp. Dispatch” interface

Python and Arc. GIS geoprocessor, describe, cursor, enumeration, other • • “Gp. Dispatch” interface Non graphics "tools" only, similar to Arc: commands Few tools for Arc. View, useful with Arc. Editor+ Similar language level to AML or Avenue Create form interfaces in Arc. Toolbox Dot-zero release, sparse documentation ESRI training course ready now! OLLIVIER & CO Python Workshop

Python Arc. GIS Scripts • • • Toolbox macros Geoprocessing operators Model Builder output

Python Arc. GIS Scripts • • • Toolbox macros Geoprocessing operators Model Builder output Table cursors Database maintenance OLLIVIER & CO Python Workshop

Other Python Applications • • • Database laundering Calculations System maintenance Web services XML

Other Python Applications • • • Database laundering Calculations System maintenance Web services XML parsing COM objects OLLIVIER & CO Python Workshop

Reformat shapefile attributes • Task: - Reformat road names for labeling with Title Case

Reformat shapefile attributes • Task: - Reformat road names for labeling with Title Case in a shapefile • Open an existing ODBC connection • Use a database cursor • Use capwords() • 12 lines of code • no COM interface OLLIVIER & CO # caproad. py import string, dbi, odbc myconn = odbc('trackrd') mycursor = myconn. cursor() mycursor. execute('SELECT shortname, rcl_id FROM trackrd') mydata = mycursor. fetchall() stmt = "UPDATE trackrd SET shortname = ? WHERE rcl_id = ? " for rec in mydata : if rec[0] != None : sname = string. capwords(rec[0]) mycursor. execute(stmt, [sname, int(rec[1])]) mycursor. close() myconn. close() Python Workshop

Calculations • Find a sheet index extent • Converted an AML that does the

Calculations • Find a sheet index extent • Converted an AML that does the same thing OLLIVIER & CO Python Workshop

Data Preparation • Define a projection for a selected directory of shapefiles ( extra

Data Preparation • Define a projection for a selected directory of shapefiles ( extra script download for Arc 9. 0) • Create a toolbox interface • Unzip a directory of zip files (2. 3 only) OLLIVIER & CO Python Workshop

Web Programming • Standard library modules • Projection Questionnaire OLLIVIER & CO Python Workshop

Web Programming • Standard library modules • Projection Questionnaire OLLIVIER & CO Python Workshop

XML Programming • Extract image extent from Arc. XML metadata text file to make

XML Programming • Extract image extent from Arc. XML metadata text file to make a Mr. SID catalog • Used SAX with a call back (A Describe tool is now provided at 9. 0) OLLIVIER & CO Python Workshop

Advanced Programming • Make a module (just name a file containing scripts) • Create

Advanced Programming • Make a module (just name a file containing scripts) • Create a COM object DLL to use in Arc. Map • Interface to SWIG (Simplified Wrapper and Interface Generator) OLLIVIER & CO Python Workshop

Summary • Very elegant and readable, easy • Powerful, cross platform, open, free. •

Summary • Very elegant and readable, easy • Powerful, cross platform, open, free. • Will become very widely used, outside GIS as well • Books, online manuals, courses • Get the latest version (2. 3) • Take it with you OLLIVIER & CO Python Workshop

Discussion OLLIVIER & CO Python Workshop

Discussion OLLIVIER & CO Python Workshop