Using ArcObjects and .NET in Python

date

2011-04-17 13:59

author

admin

category

arcobjects, programming, python

slug

using-arcobjects-and-net-in-python

status

published

image

This question was posed on the GIS Stack Exchange, and the replies include a number of interesting approaches. Below is another one.

Why not just use .NET?

I have several years worth of DLLs built using VB.NET and ArcObjects. Recently I’ve been using more and more open-source libraries, the majority of which can be manipulated using Python.

A major advantage of using a interpreted language (rather than one that needs to compile) like Python is that you can easily change paths and variables in a script.

You can deliver a folder of .NET ArcObjects DLLs, a sample Python script and an end user can automate their workflow themselves. This negates the requirement to build a UI on top of your code as a (Python familiar) user can script it themselves.

DLLs still have their place as ArcObjects includes many functions not available in the ESRI Python libraries. It is also easier to write and test ArcObjects code in Visual Studio with all the advantages of error checking and auto-completion.

Options

Python is a programming language, but it can and has been implemented many times using different underlying technologies. The “default” version of Python is implemented in C (and therefore named CPython). Jython is implemented in Java, and IronPython is implemented in C#.

IronPython therefore seems perfect to use if the majority of your code imageis based around the .NET framework, however as it is separate from standard Python many third-party libraries are incompatible.

This may no longer be true thanks to the `CPython extensions <http://www.voidspace.org.uk/ironpython/cpython_extensions.shtml>`__ to IronPython, which embeds a CPython interpreter into IronPython ?” it sounds crazy but apparently works.

Embedding CPython into IronPython relies upon a library called Python for .NET, which also allows .NET libraries to be used in standard CPython. The library can be used deparately from IronPython, so the solution I ended up using was CPython and the Python for .NET library.

Installation

  1. Go the download page for the Python for .Net project at http://sourceforge.net/projects/pythonnet/files/, and download the appropriate zip file for your existing Python installation (2.5, 2.6 or 2.7).

  2. Unzip the file to a folder of your choice. Ideally this should be somewhere on the Python path, but in the sample code below you can add a reference to this folder by changing the path to the pythonnet-2.0-alpha2-clr2.0_131_py27_UCS2 folder.

  3. That’s all you need to do!

The sample script below shows how to add references to your own DLLs and to the ESRI ArcObject DLLs, open ArcMap, and create classes.

As this approach is not ArcObjects specific you can use classes in any of your other .NET DLLs, and even classes in .NET .exes.

I can now script all the classes in my .NET DLLs through Python ?” instantly removing the need for hastily put together user interfaces, and fully automating previously manual tasks. It also allows easy passing of data between the ESRI and open source stacks. I’ve only scripted a few processes so far, but I’ve had no issues yet, and at the moment it feels like one of the rare programming occasions where getting things to work together was actually easier than expected.

orphan


Comments

http://www.gravatar.com/avatar/1f3083754b79e77c7d06f353c2f7cbd9?s=55&d=identicon&r=g

1. Ruben Berenguel **

A well written post, I say. I’m also happy at discovering ‘Gephi’, it looks gorgeous.

Reply
http://www.gravatar.com/avatar/a00ac6ca365712a2e87acee41e8f8e4e?s=55&d=identicon&r=g

2. Mathieu Bastian **

Hi,

A new Gephi plugin has just been released: GeoLayout. It allows to use latitude/longitude coordinates to set nodes’ position.

You can download it from this page (http://gephi.org/plugins/geolayout/) or install directly from Gephi (Go To Tools > Plugins).

The author is looking for feedbacks, don’t hesitate to come to the forum!

Reply
http://www.gravatar.com/avatar/7ae23bfa8ce5821731be235d83ae5f0c?s=55&d=identicon&r=g

3. Donte **

You are so awesome for helping me solve this myretsy.

Reply
Add Comment